目录

查询

创建

删除请求

1. 删除索引

数据类型

一、创建索引

1.1 创建book_test索引

1.2 查看索引映射

1.3 添加文档记录

1.4 查询文档

1.6 删除文档中某一条数据

二、查询语法

2.1 term&terms查询

2.2 match查询属于高层查询,他会根据你查询的字段类型不一样,采用不同的查询方式,

3.1 分词效果分析

4.1    其他查询

4.1.1 根据单个id查询

4.1.2    # 查询多个id返回结果

4.2.1 prefix查询

4.3 fuzzy查询   模糊查询,我们输入字符的大概,ES就可以根据输入的内容大概去匹配结果。但是查询结果不太稳定

4.4 wildcard 查询

4.5 range范围查询

4.6    正则查询,通过你编写的正则表达式去匹配内容

4.7 scroll 深分页

4.8 delete-by-query 根据查询删除

4.9 复合查询,

4.9 boosting查询

4.10 filter查询

4.11 高亮查询

4.12 聚合查询,多种多样的聚合


查询

GET请求 ,参数只能在路径上
    POST请求,也是 查询,参数可以放在请求体中,也可以更新文档,指定type=update

创建

PUT请求
    创建索引,需要在请求体中指定分片信息,索引信息
    
    http://ip:port/index/type/_mapping 创建索引时,指定索引文档的属性

删除请求

DELETE
    http://ip:port/index  删除整个文档,==删除库

1. 删除索引

DELETE /user_index

数据类型

https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-types.html
    feild 
        String 
            text:分词,可以放一段话
            keyword: 不会被分词 ,比如地区--北京,上海
        时间类型
            date :指定格式
        数值类型

布尔类型
            Boolean
        二进制类型
        
        范围类型
            
        经纬度
            "type": "geo_point"

一、创建索引

1.1 创建book_test索引

PUT /book_test
        {
          "settings": {
            "number_of_replicas": 1,
            "number_of_shards": 1
          },
          "mappings": {
            "noval":{                            # 执行文档类型   在es8以上没有这个了。需要删掉,否则会出错
              "properties":{                    # 指定文档属性
                "name":{                        # 指定属性名
                  "type":"text",
                  "index":true,
                  "analyzer": "ik_smart",
                  "store": true
                },
                "author":{
                  "type":"keyword"
                },
                "count":{
                  "type":"long"
                },
                "on_sale":{
                  "type":"date",
                  "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
                },
                "desc":{
                  "type":"text",
                  "analyzer":"ik_max_word"
                }
              }
            }
          }
        }

1.1 创建成功响应结果
        {
          "acknowledged" : true,
          "shards_acknowledged" : true,
          "index" : "book_test"
        }

1.2 查看索引映射

GET /book_test/_mapping
    1.2 响应结果
        {
          "book_test" : {
            "mappings" : {
              "properties" : {
                "author" : {
                  "type" : "keyword"
                },
                "count" : {
                  "type" : "long"
                },
                "desc" : {
                  "type" : "text",
                  "analyzer" : "ik_max_word"
                },
                "name" : {
                  "type" : "text",
                  "store" : true,
                  "analyzer" : "ik_smart"
                },
                "on_sale" : {
                  "type" : "date",
                  "format" : "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
                }
              }
            }
          }
        }

1.3 添加文档记录

POST /book_test/_doc/2       # 这是指定id,没有/2自动生成id。带有id多次操作是修改
        {
          "name":"西游记",
          "author":"张三2",
          "count":"1565",
          "on_sale":"2022-08-30"
          ,"desc":"了接口直接裂开房价高了几十个"
        }
        
        响应  带有id去更新操作
        {
          "_index" : "book_test",
          "_type" : "_doc",
          "_id" : "2",
          "_version" : 2,
          "result" : "updated",
          "_shards" : {
            "total" : 2,
            "successful" : 1,
            "failed" : 0
          },
          "_seq_no" : 2,
          "_primary_term" : 1
        }

1.4 查询文档

GET /book_test/_search
        
        响应
        
        {
          "took" : 0,   # 查询耗时
          "timed_out" : false, # 是否超时,这里没超时
          "_shards" : {
            "total" : 1,
            "successful" : 1,
            "skipped" : 0,
            "failed" : 0
          },
          "hits" : {
            "total" : {
              "value" : 2,
              "relation" : "eq"
            },
            "max_score" : 1.0,
            "hits" : [
              {
                "_index" : "book_test",
                "_type" : "_doc",
                "_id" : "23dxtoMBUPIMnS01cm85",
                "_score" : 1.0,
                "_source" : {
                  "name" : "我的小说",
                  "author" : "张三",
                  "count" : "125",
                  "on_sale" : "2022-09-30",
                  "desc" : "家乐鸡粉了垃圾的垃圾费附件覅额计入还让他了客户是"
                }
              },
              {
                "_index" : "book_test",
                "_type" : "_doc",
                "_id" : "2",
                "_score" : 1.0,
                "_source" : {
                  "name" : "西游记",
                  "author" : "张三2",
                  "count" : "1565",
                  "on_sale" : "2022-08-30",
                  "desc" : "了接口直接裂开房价高了几十个"
                }
              }
            ]
          }
        }

1.5 修改某个字段的值,根据id查询结果,修改一条

POST /book_test/_update_by_query
        {
          "script": {
            "source": "ctx._source.name=params.name",
            "params": {
              "name":"修改西游记"
            }, 
            "lang": "painless"
          }
          , "query": {
            "term": {
              "_id": 2
            }
          }
        }
        修改某个字段的值,根据字段查询结果,修改多条
        POST /book_test/_update_by_query
        {
          "script": {
            "source": "ctx._source.name=params.name",
            "params": {
              "name":"修改西游记2"
            }, 
            "lang": "painless"
          }
          , "query": {
            "term": {
             "author" : "张三2"
            }
          }
        }
        文档方式修改单个字段
        POST /book_test/_doc/2/_update
        {
          "doc":{
            "count":"1222"
          }
        }

1.6 删除文档中某一条数据

删除id=_update的数据
        DELETE /book_test/_doc/_update
        响应结果
        {
          "_index" : "book_test",
          "_type" : "_doc",
          "_id" : "_update",
          "_version" : 2,
          "result" : "deleted",
          "_shards" : {
            "total" : 2,
            "successful" : 1,
            "failed" : 0
          },
          "_seq_no" : 11,
          "_primary_term" : 1
        }

二、查询语法

2.1 term&terms查询

虽然查询的词不会分词,但是搜索的时候,是用词,到分词库中匹配,匹配到就返回响应的数据,比如:一条数据的长文本里有奋斗的时代,分词后会产生奋斗、时代。我们用奋斗term查询的时候就会匹配到这条数据返回。
    2.1.1 term查询
        term 的查询代表的是完全匹配,搜索之前不会对你搜索的关键字进行分词,对你的关键字去文档分词库中去匹配内容。
        
        查询语法
        POST /book_test/_search
        {
          "from": 0,
          "size": 20,
          "query": {
            "term": {
              "author": {
                "value": "李四"
              }
            }
          }
        }
        
        响应结果
        {
          "took" : 402,                # 耗时
          "timed_out" : false,        # 是否超时
          "_shards" : {
            "total" : 1,
            "successful" : 1,
            "skipped" : 0,
            "failed" : 0
          },
          "hits" : {
            "total" : {
              "value" : 1,
              "relation" : "eq"
            },
            "max_score" : 1.3862942,
            "hits" : [
              {
                "_index" : "book_test",
                "_type" : "_doc",
                "_id" : "4",
                "_score" : 1.3862942,
                "_source" : {
                  "name" : "西游记2",
                  "author" : "李四",
                  "count" : "1565",
                  "on_sale" : "2022-08-31",
                  "desc" : "发根深蒂固"
                }
              }
            ]
          }
        }
    2.1.2     terms 也不会分词,针对某个字段,多个值进行匹配
        查询语法
        POST /book_test/_search
        {
          "from": 0,
          "size": 20,
          "query": {
            "terms": {
              "author": [
                "张三",
                "李四"
              ]
            }
          }
        }
        
        响应结果
        {
          "took" : 0,
          "timed_out" : false,
          "_shards" : {
            "total" : 1,
            "successful" : 1,
            "skipped" : 0,
            "failed" : 0
          },
          "hits" : {
            "total" : {
              "value" : 2,
              "relation" : "eq"
            },
            "max_score" : 1.0,
            "hits" : [
              {
                "_index" : "book_test",
                "_type" : "_doc",
                "_id" : "23dxtoMBUPIMnS01cm85",
                "_score" : 1.0,
                "_source" : {
                  "name" : "我的小说",
                  "author" : "张三",
                  "count" : "125",
                  "on_sale" : "2022-09-30",
                  "desc" : "家乐鸡粉了垃圾的垃圾费附件覅额计入还让他了客户是"
                }
              },
              {
                "_index" : "book_test",
                "_type" : "_doc",
                "_id" : "4",
                "_score" : 1.0,
                "_source" : {
                  "name" : "西游记2",
                  "author" : "李四",
                  "count" : "1565",
                  "on_sale" : "2022-08-31",
                  "desc" : "发根深蒂固"
                }
              }
            ]
          }
        }

2.2 match查询属于高层查询,他会根据你查询的字段类型不一样,采用不同的查询方式,

查询日期或者是数据类型,他会将你基于字符串查询的内推转换为日期或者数值
        如果查询的内容是一个不能被分词的内容(keyword),match查询不会对你指定的查询关键字进行分词
        
        如果查询的内容是一个可以被分词的内容(text),match会将你指定的查询内容根据一定的方式去分词,去分词库中匹配指定的内容
        
        总之,match查询,实际底层就是多个term查询,将多个term查询的结果给封装到一起,返回。
        
        ES默认只查询10条数据,要想查询更多,需要指定size。
        
    2.2.1 查询语法 分词匹配
        POST /book_test/_search
        {
          "query": {
            "match": {
              "name": "西游记"
            }
          }
        }
        
        响应
        {
          "took" : 0,
          "timed_out" : false,
          "_shards" : {
            "total" : 1,
            "successful" : 1,
            "skipped" : 0,
            "failed" : 0
          },
          "hits" : {
            "total" : {
              "value" : 3,
              "relation" : "eq"
            },
            "max_score" : 0.48120394,
            "hits" : [
              {
                "_index" : "book_test",
                "_type" : "_doc",
                "_id" : "4",
                "_score" : 0.48120394,
                "_source" : {
                  "name" : "西游记2",
                  "author" : "李四",
                  "count" : "1565",
                  "on_sale" : "2022-08-31",
                  "desc" : "发根深蒂固"
                }
              },
              {
                "_index" : "book_test",
                "_type" : "_doc",
                "_id" : "3",
                "_score" : 0.40841687,
                "_source" : {
                  "author" : "张三2",
                  "name" : "修改西游记2",
                  "count" : "1565",
                  "on_sale" : "2022-08-31",
                  "desc" : "fsgdfgafad"
                }
              },
              {
                "_index" : "book_test",
                "_type" : "_doc",
                "_id" : "2",
                "_score" : 0.40841687,
                "_source" : {
                  "author" : "张三2",
                  "name" : "修改西游记2",
                  "count" : "1222",
                  "on_sale" : "2022-08-30",
                  "desc" : "了接口直接裂开房价高了几十个"
                }
              }
            ]
          }
        }
        
        2.2.2 布尔match查询
        查询一个字段,可以匹配多个字,关系可以是or 或者 and
        POST /book_test/_search
        {
          "query": {
            "match": {
              "desc": {
                "query": "京东 大家",
                "operator": "or"
              }
            }
          }
        }
        
        响应结果,评分越高,匹配度越高
        {
          "took" : 275,
          "timed_out" : false,
          "_shards" : {
            "total" : 1,
            "successful" : 1,
            "skipped" : 0,
            "failed" : 0
          },
          "hits" : {
            "total" : {
              "value" : 2,
              "relation" : "eq"
            },
            "max_score" : 3.179408,
            "hits" : [
              {
                "_index" : "book_test",
                "_type" : "_doc",
                "_id" : "5",
                "_score" : 3.179408,
                "_source" : {
                  "name" : "大话西游",
                  "author" : "李大四",
                  "count" : "1565",
                  "on_sale" : "2022-08-31",
                  "desc" : "京东卡拉拉LDA; 大家都发"
                }
              },
              {
                "_index" : "book_test",
                "_type" : "_doc",
                "_id" : "6",
                "_score" : 1.5683775,
                "_source" : {
                  "name" : "大话济公",
                  "author" : "小四",
                  "count" : "1565",
                  "on_sale" : "2022-08-31",
                  "desc" : "大家都发财"
                }
              }
            ]
          }
        }
        
        2.3 multi_match查询
        match针对一个feild做检索,multi_match针对多个feild进行检索,多个feild对应一个text。

3.1 分词效果分析

POST _analyze
        {
          "analyzer": "ik_max_word",
          "text": ["笔记本电脑"]
        }

4.1    其他查询

4.1.1 根据单个id查询

GET /book_test/_doc/2
        GET /book_test/_search?q=_id:3.

4.1.2    # 查询多个id返回结果

GET /user_index/_doc/_mget
        {
          "ids":[1,2]
        }
        
        POST /book_test/_search
        {
          "query": {
            "ids": {
              "values": ["3","2"]
            }
          }
        }

4.2.1 prefix查询

4.2.1.1 前缀查询,下面author类型是keyword,如果用match或者term匹配不到,用前缀查询就可以匹配到
        POST /book_test/_search
        {
          "query": {
            "prefix": {
              "author": {
                "value": "李"
              }
            }
          }
        }
        
        前缀查询响应
        {
          "took" : 0,
          "timed_out" : false,
          "_shards" : {
            "total" : 1,
            "successful" : 1,
            "skipped" : 0,
            "failed" : 0
          },
          "hits" : {
            "total" : {
              "value" : 2,
              "relation" : "eq"
            },
            "max_score" : 1.0,
            "hits" : [
              {
                "_index" : "book_test",
                "_type" : "_doc",
                "_id" : "4",
                "_score" : 1.0,
                "_source" : {
                  "name" : "西游记2",
                  "author" : "李四",
                  "count" : "1565",
                  "on_sale" : "2022-08-31",
                  "desc" : "发根深蒂固"
                }
              },
              {
                "_index" : "book_test",
                "_type" : "_doc",
                "_id" : "5",
                "_score" : 1.0,
                "_source" : {
                  "name" : "大话西游",
                  "author" : "李大四",
                  "count" : "1565",
                  "on_sale" : "2022-08-31",
                  "desc" : "京东卡拉拉LDA; 大家都发"
                }
              }
            ]
          }
        }
    
        4.2.1.2 前缀匹配text类型的字段查询,匹配的是分词库--比如name
        POST /book_test/_search
        {
          "query": {
            "prefix": {
              "name": {
                "value": "西游"
              }
            }
          }
        }
        
        分词前缀响应
        {
          "took" : 0,
          "timed_out" : false,
          "_shards" : {
            "total" : 1,
            "successful" : 1,
            "skipped" : 0,
            "failed" : 0
          },
          "hits" : {
            "total" : {
              "value" : 3,
              "relation" : "eq"
            },
            "max_score" : 1.0,
            "hits" : [
              {
                "_index" : "book_test",
                "_type" : "_doc",
                "_id" : "3",
                "_score" : 1.0,
                "_source" : {
                  "author" : "张三2",
                  "name" : "修改西游记2",
                  "count" : "1565",
                  "on_sale" : "2022-08-31",
                  "desc" : "fsgdfgafad"
                }
              },
              {
                "_index" : "book_test",
                "_type" : "_doc",
                "_id" : "2",
                "_score" : 1.0,
                "_source" : {
                  "author" : "张三2",
                  "name" : "修改西游记2",
                  "count" : "1222",
                  "on_sale" : "2022-08-30",
                  "desc" : "了接口直接裂开房价高了几十个"
                }
              },
              {
                "_index" : "book_test",
                "_type" : "_doc",
                "_id" : "4",
                "_score" : 1.0,
                "_source" : {
                  "name" : "西游记2",
                  "author" : "李四",
                  "count" : "1565",
                  "on_sale" : "2022-08-31",
                  "desc" : "发根深蒂固"
                }
              }
            ]
          }
        }

4.3 fuzzy查询   模糊查询,我们输入字符的大概,ES就可以根据输入的内容大概去匹配结果。但是查询结果不太稳定

4.3.1 模糊查询也是---去分词库查
        POST /book_test/_search
        {
          "query": {
           "fuzzy": {                    # 模糊查询
             "desc": {                    # 模糊字段
               "value": "大家",
               "prefix_length": 1        # 前边几个字不可以错的
             }
           }
          }
        }
        
        模糊响应
        {
          "took" : 0,
          "timed_out" : false,
          "_shards" : {
            "total" : 1,
            "successful" : 1,
            "skipped" : 0,
            "failed" : 0
          },
          "hits" : {
            "total" : {
              "value" : 2,
              "relation" : "eq"
            },
            "max_score" : 1.691406,
            "hits" : [
              {
                "_index" : "book_test",
                "_type" : "_doc",
                "_id" : "6",
                "_score" : 1.691406,
                "_source" : {
                  "name" : "大话济公",
                  "author" : "小四",
                  "count" : "1565",
                  "on_sale" : "2022-08-31",
                  "desc" : "大家都发财"
                }
              },
              {
                "_index" : "book_test",
                "_type" : "_doc",
                "_id" : "5",
                "_score" : 1.386699,
                "_source" : {
                  "name" : "大话西游",
                  "author" : "李大四",
                  "count" : "1565",
                  "on_sale" : "2022-08-31",
                  "desc" : "京东卡拉拉LDA; 大家都发"
                }
              }
            ]
          }
        }

4.4 wildcard 查询

通配符查询,和mysql中的like是一样,可以在查询时,在字符串中指定通配符*和占位符?
        POST /book_test/_search
        {
          "query": {
           "wildcard": {
             "desc": {
               "value": "*f*"
             }
           }
          }
        }
        
        响应
        {
          "took" : 0,
          "timed_out" : false,
          "_shards" : {
            "total" : 1,
            "successful" : 1,
            "skipped" : 0,
            "failed" : 0
          },
          "hits" : {
            "total" : {
              "value" : 1,
              "relation" : "eq"
            },
            "max_score" : 1.0,
            "hits" : [
              {
                "_index" : "book_test",
                "_type" : "_doc",
                "_id" : "3",
                "_score" : 1.0,
                "_source" : {
                  "author" : "张三2",
                  "name" : "修改西游记2",
                  "count" : "1565",
                  "on_sale" : "2022-08-31",
                  "desc" : "fsgdfgafad"
                }
              }
            ]
          }
        }
        
        第二个测试
        POST /book_test/_search
        {
          "query": {
           "wildcard": {
             "author": {
               "value": "李??"
             }
           }
          }
        }

4.5 range范围查询

范围查询,只针对数值类型,对某一个feild进行大于或者小于的范围指定
        大于gt;  小于lt
        
        POST /book_test/_search
        {
          "query": {
            "range": {
              "count": {
                "gte": 10,
                "lte": 200
              }
            }
          }
        }

4.6    正则查询,通过你编写的正则表达式去匹配内容

ps:prefix,fuzzy,wildcard,regexp查询效率相对比较低,要求效率比较高时,避免去使用。
        POST /book_test/_search
        {
          "query": {
            "regexp": {
              "name": "*[0-9]{1}"
            }
          }
        }

4.7 scroll 深分页

ES对from+size是有限制的,from和size二者纸盒不能超过1万
        原理:
        from+size 的ES查询数据的方式
            1、 先将用户指定的关键字进行分词
            2、 将词汇去分词库中进行检索,得到多个文档的id
            3、 去各个分片中去拉取指定的数据    # 这个过程有点慢的
            4、 将数据分局score进行排序            # 这个慢
            5、 根据from的值,将查询到的数据舍弃一部分
            6、 返回结果
            
        scroll 的ES查询数据的方式
            1、 先将用户指定的关键字进行分词
            2、 将词汇去分词库中进行检索,得到多个文档的id
            3、 将文档的id存放在一个ES的上下文中
            4、 根据你指定的size去ES中检索指定的数据,拿完数据的文档id,会从上下文移除。
            5、 如果需要下一页的数据,直接去ES的上下文中,找后续内容。
            6、 返回结果
            ps:不适合做实时查询,因为是从内存中查的。
            查询结果没有评分了,按照id排序或者是自己定义排序
            
            POST /book_test/_search?scroll=1m        # 指定scroll查询的id在内存中存1分钟
            {
              
              "query": {
                "match": {
                  "name": "西游记"
                }
              }
              , "size": 2                        # 查询条数
              , "sort": [                        # 自定义排序
                {
                  "count": {
                    "order": "desc"
                  }
                }
              ]
            }
            
            深分页响应
            {
              "_scroll_id" : "FGluY2x1ZGVfY29udGV4dF91dWlkDXF1ZXJ5QW5kRmV0Y2gBFlZ4ZGplLVZ4VGFlcVdWT1ljQmstTHcAAAAAAIMYSRZDMWpuSWFZa1JFQ2ZReGRRRHdzcm53",
              "took" : 0,
              "timed_out" : false,
              "_shards" : {
                "total" : 1,
                "successful" : 1,
                "skipped" : 0,
                "failed" : 0
              },
              "hits" : {
                "total" : {
                  "value" : 3,
                  "relation" : "eq"
                },
                "max_score" : null,
                "hits" : [
                  {
                    "_index" : "book_test",
                    "_type" : "_doc",
                    "_id" : "3",
                    "_score" : null,
                    "_source" : {
                      "author" : "张三2",
                      "name" : "修改西游记2",
                      "count" : "1565",
                      "on_sale" : "2022-08-31",
                      "desc" : "fsgdfgafad"
                    },
                    "sort" : [
                      1565
                    ]
                  },
                  {
                    "_index" : "book_test",
                    "_type" : "_doc",
                    "_id" : "4",
                    "_score" : null,
                    "_source" : {
                      "name" : "西游记2",
                      "author" : "李四",
                      "count" : "1565",
                      "on_sale" : "2022-08-31",
                      "desc" : "发根深蒂固"
                    },
                    "sort" : [
                      1565
                    ]
                  }
                ]
              }
            }
        下一次scroll查询
            POST /_search/scroll
            {
              "scroll_id":"FGluY2x1ZGVfY29udGV4dF91dWlkDXF1ZXJ5QW5kRmV0Y2gBFlZ4ZGplLVZ4VGFlcVdWT1ljQmstTHcAAAAAAIMZghZDMWpuSWFZa1JFQ2ZReGRRRHdzcm53"
              ,"scroll":"1m"
            }
            
        删除scroll
            DELETE /_search/scroll/FGluY2x1ZGVfY29udGV4dF91dWlkDXF1ZXJ5QW5kRmV0Y2gBFlZ4ZGplLVZ4VGFlcVdWT1ljQmstTHcAAAAAAIMZghZDMWpuSWFZa1JFQ2ZReGRRRHdzcm53

4.8 delete-by-query 根据查询删除

如果删除文档数量是大部分文档,不建议用这种方式,这种方式,比较耗费性能,一条条删除的
        
        POST /book_test/_delete_by_query
        {
          "query": {
            "match": {
              "name": "西游记"
            }
          }
        }

4.9 复合查询,

复合过滤器,将你的多个查询条件,以一定的逻辑组合在一起
        must :所有的条件,用must组合在一起,表示and意思
        must_not : 将must_not中的条件,全部都不能匹配,表示not的意思
        should : 所有的条件,用should组合在一起,表示or的意思
        
        POST /book_test/_search
        {
          "query": {
           "bool": {
             "must": [
               {
                 "match": {
                   "name": "西游记"
                 }
               },
               {
                 "range": {
                   "on_sale": {
                     "gte": "2022-08-31",
                     "lte":  "2022-08-31"
                   }
                 }
               }
             ],
             "should": [
               {
                 "term": {
                   "author": {
                     "value": "李四"
                   }
                 }
               }
             ],
             "must_not": [
               {
                 "range": {
                   "count": {
                     "gte": 10,
                     "lte": 20
                   }
                 }
               }
             ]
           }
          }
        }
        
        响应结果
        {
          "took" : 0,
          "timed_out" : false,
          "_shards" : {
            "total" : 1,
            "successful" : 1,
            "skipped" : 0,
            "failed" : 0
          },
          "hits" : {
            "total" : {
              "value" : 2,
              "relation" : "eq"
            },
            "max_score" : 3.5179136,
            "hits" : [
              {
                "_index" : "book_test",
                "_type" : "_doc",
                "_id" : "4",
                "_score" : 3.5179136,
                "_source" : {
                  "name" : "西游记2",
                  "author" : "李四",
                  "count" : "1565",
                  "on_sale" : "2022-08-31",
                  "desc" : "发根深蒂固"
                }
              },
              {
                "_index" : "book_test",
                "_type" : "_doc",
                "_id" : "3",
                "_score" : 1.6099696,
                "_source" : {
                  "author" : "张三2",
                  "name" : "修改西游记2",
                  "count" : "1565",
                  "on_sale" : "2022-08-31",
                  "desc" : "fsgdfgafad"
                }
              }
            ]
          }
        }

4.9 boosting查询

boosting查询可以帮助我们去影响查询后的score,
        positive:只有匹配上positive的查询的内容,才会被放到返回的结果中
        negative : 如果匹配上和positive并且也匹配上了negative,就可以降低这样的文档score。
        negative_boost : 指定系数,必须小于1.0
        
        关键查询时,分数是如何计算的:
        搜索的关键字在文档中出现的频次越高,分数就越高
        指定的文档内容越短,分数就越高
        我们在搜索时,指定的关键字也会分词,这个被分词的内容,被分词的个数越多,分数越高
        
        POST /book_test/_search
        {
          "query": {
            "boosting": {
              "positive": {
                "match": {
                  "name": "西游记"
                }
              },
              "negative": {
                "match": {
                  "author": "李四"
                }
              },
              "negative_boost": 0.5
            }
          }
        }
        
        响应
        {
          "took" : 6,
          "timed_out" : false,
          "_shards" : {
            "total" : 1,
            "successful" : 1,
            "skipped" : 0,
            "failed" : 0
          },
          "hits" : {
            "total" : {
              "value" : 3,
              "relation" : "eq"
            },
            "max_score" : 0.60996956,
            "hits" : [
              {
                "_index" : "book_test",
                "_type" : "_doc",
                "_id" : "3",
                "_score" : 0.60996956,
                "_source" : {
                  "author" : "张三2",
                  "name" : "修改西游记2",
                  "count" : "1565",
                  "on_sale" : "2022-08-31",
                  "desc" : "fsgdfgafad"
                }
              },
              {
                "_index" : "book_test",
                "_type" : "_doc",
                "_id" : "2",
                "_score" : 0.60996956,
                "_source" : {
                  "author" : "张三2",
                  "name" : "修改西游记2",
                  "count" : "1222",
                  "on_sale" : "2022-08-30",
                  "desc" : "了接口直接裂开房价高了几十个"
                }
              },
              {
                "_index" : "book_test",
                "_type" : "_doc",
                "_id" : "4",
                "_score" : 0.36307707,
                "_source" : {
                  "name" : "西游记2",
                  "author" : "李四",
                  "count" : "1565",
                  "on_sale" : "2022-08-31",
                  "desc" : "发根深蒂固"
                }
              }
            ]
          }
        }

4.10 filter查询

query查询,根据你的查询条件,去计算文档的匹配度,得到一个分数,并且根据分数进行排序,不会做缓存的。
        filter ,根据你的查询条件去查询文档,不去计算分数,不做排序,而且filter会对经常被过滤的数据进行缓存,查询效率相对query高
        POST /book_test/_search
        {
          "query": {
            "bool": {
              "filter": [
                {
                  "term": {                        # 第一个过滤条件
                    "author": "李四"
                  }
                },
                {
                  "range": {                    # 第二个过滤条件
                    "count": {
                      "gte": 10,
                      "lte": 20000
                    }
                  }
                }
              ]
            }
          }
        }
        响应结果没有分数
        {
          "took" : 0,
          "timed_out" : false,
          "_shards" : {
            "total" : 1,
            "successful" : 1,
            "skipped" : 0,
            "failed" : 0
          },
          "hits" : {
            "total" : {
              "value" : 1,
              "relation" : "eq"
            },
            "max_score" : 0.0,
            "hits" : [
              {
                "_index" : "book_test",
                "_type" : "_doc",
                "_id" : "4",
                "_score" : 0.0,
                "_source" : {
                  "name" : "西游记2",
                  "author" : "李四",
                  "count" : "1565",
                  "on_sale" : "2022-08-31",
                  "desc" : "发根深蒂固"
                }
              }
            ]
          }
        }

4.11 高亮查询

高亮查询就是你用户输入的关键字,以一定的特殊样式展示给用户,让用户知道为什么这个结果被检索出来。
        高亮展示的数据,本身就是稳定中的一个feild,单独将feild以highlight的形式返回给你。
        ES提供了一个highlight属性和query同级别的。
        fragment_size:指定高亮数据展示多少个字符回来。
        pre_tags:指定前缀标签<font color = "red">
        post_tags : 指定后缀标签</font>
        fields:指定哪个feild以高亮形式返回。
        
        POST /book_test/_search
        {
          "query": {
            "bool": {
              "filter": [
                {
                  "term": {
                    "desc": "大家"
                  }
                },
                {
                  "range": {
                    "count": {
                      "gte": 10,
                      "lte": 20000
                    }
                  }
                }
              ]
            }
          },
          "highlight": {
            "fields": {
              "desc": {}
            },
            "pre_tags": "<font color = 'red'>",
            "post_tags": "</font>"
            , "fragment_size":10
          }
        }
        
        高亮响应
        {
          "took" : 86,
          "timed_out" : false,
          "_shards" : {
            "total" : 1,
            "successful" : 1,
            "skipped" : 0,
            "failed" : 0
          },
          "hits" : {
            "total" : {
              "value" : 2,
              "relation" : "eq"
            },
            "max_score" : 0.0,
            "hits" : [
              {
                "_index" : "book_test",
                "_type" : "_doc",
                "_id" : "5",
                "_score" : 0.0,
                "_source" : {
                  "name" : "大话西游",
                  "author" : "李大四",
                  "count" : "1565",
                  "on_sale" : "2022-08-31",
                  "desc" : "京东卡拉拉LDA; 大家都发"
                },
                "highlight" : {
                  "desc" : [
                    "京东卡拉拉LDA; <font color = 'red'>大家</font>都发"
                  ]
                }
              },
              {
                "_index" : "book_test",
                "_type" : "_doc",
                "_id" : "6",
                "_score" : 0.0,
                "_source" : {
                  "name" : "大话济公",
                  "author" : "小四",
                  "count" : "1565",
                  "on_sale" : "2022-08-31",
                  "desc" : "大家都发财"
                },
                "highlight" : {
                  "desc" : [
                    "<font color = 'red'>大家</font>都发财"
                  ]
                }
              }
            ]
          }
        }

4.12 聚合查询,多种多样的聚合

ES的聚合查询和MySQL的聚合查询类似
        
        聚合查询的语法
        POST /book_test/_search
        {
            "aggs":{
                "名字(count_agg)":{
                    "聚合类型:cardinality , avg agg_type":{
                        "属性":"值"
                    }
                }
            }
        }
        
        4.12.1 去重计数查询
        去重计数,即cardinality,第一步先将返回的文档中的一个指定的feild进行去重,统计一共多少条。 去重的字段不能是text。会报错,可以是keyword、long等
        POST /mall_index_spu/_search
        {
          "aggs": {
            "aggs": {
              "cardinality": {
                "field": "teamId"
              }
            }
          }
        }
        
        响应最后
        {
            "aggregations" : {
                "aggs" : {
                  "value" : 4
            }
        }
        4.12.2 范围统计
            统计一定范围内出现的文档,比如,针对某一个feild的值在0-100,100-200之间
            范围统计可以针对普通的数值,针对时间类型,针对ip类型都可以做相应的统计
            range,date_range,ip_range
            
            POST /mall_index_spu/_search
            {
              "aggs": {
                "range_count": {
                  "range": {
                    "field": "saleNum",
                    "ranges": [
                      { 
                        "to": 10
                      },
                      {
                        "from": 10,                    # from 是包含的
                        "to": 50
                      },
                      {
                        "from": 50
                      }
                    ]
                  }
                }
              }
            }
            
        4.12.3 时间范围统计
            POST /book_test/_search
            {
              "aggs": {
                "range_count": {
                  "date_range": {
                    "field": "on_sale",
                    "format": "yyyy-MM", 
                    "ranges": [
                      { 
                        "from": "2021-10", 
                        "to": "2023-10"
                      }
                    ]
                  }
                }
              }
            }
            
            时间范围统计结果
            {
                "aggregations" : {
                "range_count" : {
                  "buckets" : [
                    {
                      "key" : "2021-10-2023-10",
                      "from" : 1.6330464E12,
                      "from_as_string" : "2021-10",
                      "to" : 1.6961184E12,
                      "to_as_string" : "2023-10",
                      "doc_count" : 7
                    }
                  ]
                }
              }
            }
        4.12.4 extended_stats统计聚合查询
            他可以帮你查询指定feild的最大值,最小值,平均值,平方和
            POST /book_test/_search
            {
              "aggs": {
                "range_count": {
                  "extended_stats": {
                    "field": "count"
                  }
                }
              }
            }
            
            响应结果
            "aggregations" : {
            "range_count" : {
              "count" : 7,
              "min" : 125.0,
              "max" : 1565.0,
              "avg" : 1310.2857142857142,
              "sum" : 9172.0,
              "sum_of_squares" : 1.3755034E7,
              "variance" : 248156.20408163272,
              "variance_population" : 248156.20408163272,
              "variance_sampling" : 289515.57142857154,
              "std_deviation" : 498.15279190388236,
              "std_deviation_population" : 498.15279190388236,
              "std_deviation_sampling" : 538.0665120861654,
              "std_deviation_bounds" : {
                "upper" : 2306.591298093479,
                "lower" : 313.9801304779495,
                "upper_population" : 2306.591298093479,
                "lower_population" : 313.9801304779495,
                "upper_sampling" : 2386.418738458045,
                "lower_sampling" : 234.15269011338341
              }
            }
          }
          
          
    4.12.5 ES 的地图检索方式  经纬度
        geo_distance: 直线距离检索方式
        geo_bounding_box: 以两个点确定一个矩形,获取在矩形内的全部数据
        geo_polygon: 以多个点,确定一个多边形,获取多边形内的全部数据

ES各种查询语法及响应结果相关推荐

  1. 【ES知识】ES基础查询语法一览

    大家好,我是老坛. 更多优质文章资源请关注同名公众号:老坛聊开发 Elasticsearch是一个分布式的RESTful 风格的搜索和数据分析引擎,它使用方便,查询速度快,因此也被越来越多的开发人员使 ...

  2. ES常用查询语法汇总

    总记录数 POST trade_prod_dy_*/_count {"query": {"range": {"created":{" ...

  3. es 查询语法_ES 在数据量很大的情况下(数十亿级别)如何提高查询效率啊?

    点击上方☝SpringForAll社区 轻松关注! 及时获取有趣有料的技术文章 本文来源:http://8rr.co/GFLb 面试官心理分析 这个问题是肯定要问的,说白了,就是看你有没有实际干过 e ...

  4. Elasticsearch(es) 查询语句语法详解

    Elasticsearch 查询语句采用基于 RESTful 风格的接口封装成 JSON 格式的对象,称之为 Query DSL.Elasticsearch 查询分类大致分为全文查询.词项查询.复合查 ...

  5. docker安装es+mac安装Kibana工具+es查询语法笔记

    一.docker安装es 1.下载镜像 docker pull elasticsearch:7.9.0 下载完后,查看镜像 docker images ​​ 2.启动镜像 docker network ...

  6. currenttimemillis 毫秒还是秒_Elasticsearch如何做到数十亿数据查询毫秒级响应?

    如果面试的时候碰到这样一个面试题:ES 在数据量很大的情况下(数十亿级别)如何提高查询效率? 这个问题说白了,就是看你有没有实际用过 ES,因为啥?其实 ES 性能并没有你想象中那么好的. 很多时候数 ...

  7. Elasticsearch如何做到数十亿数据查询毫秒级响应?

    如果面试的时候碰到这样一个面试题:ES 在数据量很大的情况下(数十亿级别)如何提高查询效率? 这个问题说白了,就是看你有没有实际用过 ES,因为啥?其实 ES 性能并没有你想象中那么好的. 很多时候数 ...

  8. java中使用es精准查询_使用ES简单查询语句须知

    查询样例 {"query": { //1 "bool": { ///2 "must": [{ //3 "query_string& ...

  9. Grafana教程(prometheus 基本查询语法,alerting报警)

    全栈工程师开发手册 (作者:栾鹏) 架构系列文章 prometheus原理可以参考:https://blog.csdn.net/luanpeng825485697/article/details/82 ...

最新文章

  1. C语言易错图形题--打印n行n列的空心正方形图案
  2. 33 个 JavaScript 核心概念系列(三): 显式 (名义) 与 隐式 (鸭子)类型转换
  3. python list去掉引号_最新的python面试题集170之三(基础性学习)
  4. 物理史2000年来最豪华阵容也是最为精彩的对决!
  5. 涂鸦智能dubbo-go亿级流量的实践与探索
  6. Linux下通过jstat命令查看jvm的GC情况
  7. 如何从文件名字符串中获取文件扩展名_Linux操作系统:文件系统的功能和命名...
  8. 除系统分区外未找到其它非系统分区导致软件无法运行解决方案
  9. dorado 刷新_dorado BDF常见问题
  10. 计算机的排除故障的方法,计算机产生故障的原因和排除故障的方法
  11. centos7.4下的KVM虚拟机安装使用
  12. 大文件(10G以上吧)的处理
  13. 计算机无法访问指定设备路径或文件怎么回事,window无法访问指定设备 路径或文件是怎么回事...
  14. Access仿Excel的RoundUp函数向上取整的方法。
  15. 既是计算机高手,也是情书高手,原来王小波才是最会撩妹的程序员
  16. Java泛型面试也能虐暴你
  17. KubernetesAPI审计日志方案
  18. 串口转HID键盘鼠标芯片沁恒微电子CH9329
  19. python代码画樱花教程-如何用Python代码实现樱花树效果
  20. java如何实现导出Excel(附源码)--文末送书

热门文章

  1. C语言 qq自动点赞程序,易语言实现QQ全自动批量点赞功能
  2. 群晖 kodi mysql_家庭影音必备系统Kodi虽然好用但总少了些什么?或许你需要一台群晖为 Kodi 注入多设备同步能力...
  3. C++之构造函数和析构函数
  4. 在Windows系统下下载biopython
  5. 深圳大学学科分析—通信工程
  6. VIVO y15t 线刷 救砖 注意事项 刷机失败的一定要看
  7. 【Blender】基础物体建模(5)
  8. Java的六种线程状态
  9. 自动化测试工具——Selenium详解
  10. golangORM框架gorm详解(超详细)