• (27)ElasticSearch 复合查询


      在实际应用当中,有时候搜索条件复杂,我们可以使用复合查询,也叫组合查询。可以使用bool查询。  

      1、使用bool查询

      接收以下参数:

      must:文档必须匹配这些条件才能被包含进来。

      must_not:文档必须不匹配这些条件才能被包含进来。

      should:如果满足这些语句中的任意语句,将增加_score,否则无任何影响。它们主要用于修正每个文档的相关性得分。

      filter:必须匹配,但它以不评分、过滤模式来进行。这些语句对评分没有贡献,只是根据过滤标准来排除或包含文档。

      相关性得分是如何组合的。每一个子查询都独自地计算文档的相关性得分。一旦他们的得分被计算出来,bool查询就将这些得分进行合并并且返回一个代表整个布尔操作的得分。

      2、准备数据

    PUT /lib
    {
        "settings":{
            "number_of_shards":3,
            "number_of_replicas":0
          },
            "mappings":{
                "user":{
                    "properties":{
                        "name":{"type":"text"},
                        "address":{"type":"text"},
                        "age":{"type":"integer"},
                        "interests":{"type":"text"},
                        "birthday":{"type":"date"}
                    }
                }
            }
    }
    put /lib/user/1
    {
        "name":"zhaoliu",
        "address":"hei long jiang sheng tie ling shi",
        "age":50,
        "birthday":"1970-12-12",
        "interests":"xi huang hejiu,duanlian,lvyou"
    }
    
    put /lib/user/2
    {
        "name":"zhaoming",
        "address":"bei jing hai dian qu qing he zhen",
        "age":20,
        "birthday":"1998-10-12",
        "interests":"xi huan hejiu,duanlian,changge"
    }
    
    put /lib/user/3
    {
        "name":"lisi",
        "address":"bei jing hai dian qu qing he zhen",
        "age":23,
        "birthday":"1998-10-12",
        "interests":"xi huan hejiu,duanlian,changge"
    }
    
    put /lib/user/4
    {
        "name":"wangwu",
        "address":"bei jing hai dian qu qing he zhen",
        "age":26,
        "birthday":"1998-10-12",
        "interests":"xi huan biancheng,tingyinyue,lvyou"
    }
    
    put /lib/user/5
    {
        "name":"zhangsan",
        "address":"bei jing chao yang qu",
        "age":29,
        "birthday":"1988-10-12",
        "interests":"xi huan tingyinyue,changge,tiaowu"
    }

      3、操作演示

      1)兴趣必须包含changge不能包含lvyou 或者 地址包含bei jing并且生日大于1996-01-01的

    GET lib/user/_search
    {
      "query":{
        "bool": {
          "must":{"match":{"interests":"changge"}},
          "must_not":{"match":{"interests":"ivyou"}},
          "should": [
            {"match": {"address": "bei jing"}},
            {"range": {"birthday": {"gte":"1996-01-01" }}}
          ]
        }
      }
    }

      查询结果

    {
      "took": 141,
      "timed_out": false,
      "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
      },
      "hits": {
        "total": 3,
        "max_score": 3.0426373,
        "hits": [
          {
            "_index": "lib",
            "_type": "user",
            "_id": "3",
            "_score": 3.0426373,
            "_source": {
              "name": "lisi",
              "address": "bei jing hai dian qu qing he zhen",
              "age": 23,
              "birthday": "1998-10-12",
              "interests": "xi huan hejiu,duanlian,changge"
            }
          },
          {
            "_index": "lib",
            "_type": "user",
            "_id": "2",
            "_score": 1.7223206,
            "_source": {
              "name": "zhaoming",
              "address": "bei jing hai dian qu qing he zhen",
              "age": 20,
              "birthday": "1998-10-12",
              "interests": "xi huan hejiu,duanlian,changge"
            }
          },
          {
            "_index": "lib",
            "_type": "user",
            "_id": "5",
            "_score": 0.77241296,
            "_source": {
              "name": "zhangsan",
              "address": "bei jing chao yang qu",
              "age": 29,
              "birthday": "1988-10-12",
              "interests": "xi huan tingyinyue,changge,tiaowu"
            }
          }
        ]
      }
    }

      2)兴趣必须包含changge不能包含lvyou 或者 地址包含bei jing,并且生日大于1996-01-01的,即所有的生日大于1996-01-01

    GET lib/user/_search
    {
      "query":{
        "bool": {
          "must":{"match":{"interests":"changge"}},
          "must_not":{"match":{"interests":"ivyou"}},
          "should": [
            {"match": {"address": "bei jing"}}
          ],
          "filter": {
             "range": {"birthday": {"gte":"1996-01-01" }}
          }
        }
      }
    }

      查询结果

    {
      "took": 11,
      "timed_out": false,
      "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
      },
      "hits": {
        "total": 2,
        "max_score": 2.0426373,
        "hits": [
          {
            "_index": "lib",
            "_type": "user",
            "_id": "3",
            "_score": 2.0426373,
            "_source": {
              "name": "lisi",
              "address": "bei jing hai dian qu qing he zhen",
              "age": 23,
              "birthday": "1998-10-12",
              "interests": "xi huan hejiu,duanlian,changge"
            }
          },
          {
            "_index": "lib",
            "_type": "user",
            "_id": "2",
            "_score": 0.7223206,
            "_source": {
              "name": "zhaoming",
              "address": "bei jing hai dian qu qing he zhen",
              "age": 20,
              "birthday": "1998-10-12",
              "interests": "xi huan hejiu,duanlian,changge"
            }
          }
        ]
      }
    }

      3)自己领略含义,过滤中可以使用bool

    GET lib/user/_search
    {
      "query":{
        "bool": {
          "must":{"match":{"interests":"changge"}},
          "must_not":{"match":{"interests":"ivyou"}},
          "should": [
            {"match": {"address": "bei jing"}}
          ],
          "filter": {
            "bool": {
              "must":[
                {"range":{"birthday":{"gte":"1990-01-01"}}},
                {"range":{"age":{"lte":30}}}
                ],
              "must_not":[
                {"term":{"age":"29"}}
                ]
            }
          }
        }
      }
    }

      查询结果

    {
      "took": 7,
      "timed_out": false,
      "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
      },
      "hits": {
        "total": 2,
        "max_score": 2.0426373,
        "hits": [
          {
            "_index": "lib",
            "_type": "user",
            "_id": "3",
            "_score": 2.0426373,
            "_source": {
              "name": "lisi",
              "address": "bei jing hai dian qu qing he zhen",
              "age": 23,
              "birthday": "1998-10-12",
              "interests": "xi huan hejiu,duanlian,changge"
            }
          },
          {
            "_index": "lib",
            "_type": "user",
            "_id": "2",
            "_score": 0.7223206,
            "_source": {
              "name": "zhaoming",
              "address": "bei jing hai dian qu qing he zhen",
              "age": 20,
              "birthday": "1998-10-12",
              "interests": "xi huan hejiu,duanlian,changge"
            }
          }
        ]
      }
    }

      4、constant_score查询

      它将一个不变的常量评分应用于所有匹配的文档。它被经常用于你只需要执行一个filter而没有其他查询(例如,评分查询)的情况下。

      例如:查询兴趣包含changge,但是不评分:

    GET lib/user/_search
    {
      "query": {
        "constant_score": {
          "filter": {"term": {
            "interests": "changge"
          }}
        }
      }
    }

      查询结果

    {
      "took": 8,
      "timed_out": false,
      "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
      },
      "hits": {
        "total": 3,
        "max_score": 1,
        "hits": [
          {
            "_index": "lib",
            "_type": "user",
            "_id": "2",
            "_score": 1,
            "_source": {
              "name": "zhaoming",
              "address": "bei jing hai dian qu qing he zhen",
              "age": 20,
              "birthday": "1998-10-12",
              "interests": "xi huan hejiu,duanlian,changge"
            }
          },
          {
            "_index": "lib",
            "_type": "user",
            "_id": "5",
            "_score": 1,
            "_source": {
              "name": "zhangsan",
              "address": "bei jing chao yang qu",
              "age": 29,
              "birthday": "1988-10-12",
              "interests": "xi huan tingyinyue,changge,tiaowu"
            }
          },
          {
            "_index": "lib",
            "_type": "user",
            "_id": "3",
            "_score": 1,
            "_source": {
              "name": "lisi",
              "address": "bei jing hai dian qu qing he zhen",
              "age": 23,
              "birthday": "1998-10-12",
              "interests": "xi huan hejiu,duanlian,changge"
            }
          }
        ]
      }
    }

      

      

      

  • 相关阅读:
    echarts + timeline 显示多个options
    微信如何获取unionid 并且打通微信公众号和小程序
    枚举
    十三、springboot集成定时任务(Scheduling Tasks)
    十二、springboot之web开发之静态资源处理
    十一、springboot之web开发之Filter
    十、springboot之web开发打包生产
    九、springboot整合redis二之缓冲配置
    RedisTemplate使用
    八、springboot整合redis
  • 原文地址:https://www.cnblogs.com/javasl/p/12559427.html
Copyright © 2020-2023  润新知