zoukankan      html  css  js  c++  java
  • ES基础使用

    文章来自
    Elasticsearch操作索引

    前提

    • 下载好Kibana,在Kibana打开后的页面里输入下面的内容进行测试,实际上Kibana的测试功能就是一个postman,但是比postman强大得多的是,可视化的数据分析,图标分析等等

    image.png

    ES的数据结构
    索引(indices)------Databases 表
    文档(Document)--------Row 行
    字段(Field)----Columns 列

    基础使用

    // 新增数据
    POST /heima/goods/
    {
        "title":"小米手机",
        "images":"http://image.leyou.com/12479122.jpg",
        "price":2699.00
    }
    
    // 查看数据
    GET /heima/_search
    {
        "query":{
            "match_all": {}
        }
    }
    // 查看数据的返回结果,id是随机生成的
    {
      "_index": "heima",
      "_type": "goods",
      "_id": "r9c1KGMBIhaxtY5rlRKv",
      "_version": 1,
      "_score": 1,
      "_source": {
        "title": "小米手机",
        "images": "http://image.leyou.com/12479122.jpg",
        "price": 2699
      }
    }
    
    // 查看数据库结构
    GET /heima/_mapping
    {
      "heima": {
        "mappings": {
          "goods": {
            "properties": {
              "images": {
                "type": "keyword",
                "index": false
              },
              "price": {
                "type": "float"
              },
              "title": {
                "type": "text",
                "analyzer": "ik_max_word"
              }
            }
          }
        }
      }
    }
    
    // 自定义id存入
    POST /heima/goods/2
    {
        "title":"大米手机",
        "images":"http://image.leyou.com/12479122.jpg",
        "price":2899.00
    }
    
    // 以id的方式再次存入就是修改,随机id需要先获取id
    POST /heima/goods/2
    {
        "title":"小米手机",
        "images":"http://image.leyou.com/12479122.jpg",
        "price":1899.00
    }
    
    // 删除数据
    DELETE /索引库名/类型名/id值
    
    // 模糊搜索,会使用分词
    GET /heima/_search
    {
        "query":{
            "match":{
                "title":"小米电视"
            }
        }
    }
    
    // 模糊搜索,不使用
    GET /heima/_search
    {
        "query":{
            "match": {
              "title": {
                "query": "小米电视",
                "operator": "and"
              }
            }
        }
    }
    
    // 组合查询
    GET /heima/_search
    {
        "query":{
            "multi_match": {
                "query":    "小米",
                "fields":   [ "title", "subTitle" ]
            }
    	}
    }
    
    // 多词条精确匹配
    GET /heima/_search
    {
        "query":{
            "terms":{
                "price":[2699.00,2899.00,3899.00]
            }
        }
    }
    
    // 只要指定的字段就行
    GET /heima/_search
    {
      "_source": {
        "includes":["title","price"]
      },
      "query": {
        "term": {
          "price": 2699
        }
      }
    }
    
    // 排除这个字段
    GET /heima/_search
    {
      "_source": {
         "excludes": ["images"]
      },
      "query": {
        "term": {
          "price": 2699
        }
      }
    }
    

    高级查询

    // 过滤
    GET /heima/_search
    {
        "query":{
            "constant_score":   {
                "filter": {
                	 "range":{"price":{"gt":2000.00,"lt":3000.00}}
                }
            }
    }
    
    // 排序
    GET /heima/_search
    {
      "query": {
        "match": {
          "title": "小米手机"
        }
      },
      "sort": [
        {
          "price": {
            "order": "desc"
          }
        }
      ]
    }
    
    // 多字段排序
    GET /goods/_search
    {
        "query":{
            "bool":{
            	"must":{ "match": { "title": "小米手机" }},
            	"filter":{
                    "range":{"price":{"gt":200000,"lt":300000}}
            	}
            }
        },
        "sort": [
          { "price": { "order": "desc" }},
          { "_score": { "order": "desc" }}
        ]
    }
    

    聚合aggregations,就是sql的分组

    // 用color字段作为分组
    // size: 查询条数,这里设置为0,因为我们不关心搜索到的数据,只关心聚合结果,提高效率
    // aggs:声明这是一个聚合查询,是aggregations的缩写
    // popular_colors:给这次聚合起一个名字,任意。
    // terms:划分桶的方式,这里是根据词条划分
    // field:划分桶的字段
    GET /cars/_search
    {
        "size" : 0,
        "aggs" : { 
            "popular_colors" : { 
                "terms" : { 
                  "field" : "color"
                }
            }
        }
    }
    
    // 用颜色做分组后还要计算价格
    GET /cars/_search
    {
        "size" : 0,
        "aggs" : { 
            "popular_colors" : { 
                "terms" : { 
                  "field" : "color"
                },
                "aggs":{
                    "avg_price": { 
                       "avg": {
                          "field": "price" 
                       }
                    }
                }
            }
        }
    }
    

    更多高端操作查看最上面的原文

  • 相关阅读:
    326周日去找书
    新视野大学英语-Book1
    预编译头文件来自编译器的早期版本,或者预编译头为 C++ 而在 C 中使用它(或相反)
    自定义GRUB主题
    Linux安装CMake
    Linux编译安装Apache
    @Scheduled注解
    熵权可拓物元模型
    Linux更新Python3.8
    Linux下更新GCC
  • 原文地址:https://www.cnblogs.com/pengdt/p/13062353.html
Copyright © 2011-2022 走看看