ES14-组合查询

1.GET /my_index/my_type/_search
{
  "query": {
    "bool": {
      "must":     { "match": { "title": "quick" }},
      "must_not": { "match": { "title": "lazy"  }},
      "should": [
                  { "match": { "title": "brown" }},
                  { "match": { "title": "dog"   }}
      ]
    }
  }
}
注:与过滤器一样, bool 查询也可以接受 must 、 must_not 和 should 参数下的多个查询语句。以上的查询结果返回 title 字段包含词项 quick 但不包含 lazy 的任意文档。
目前为止,这与 bool 过滤器的工作方式非常相似。区别就在于两个 should 语句,也就是说:一个文档不必包含 brown 或 dog 这两个词项,但如果一旦包含,我们就认为它们 更相关 :

2.控制精度:就像我们能控制 match 查询的精度 一样,我们可以通过 minimum_should_match 参数控制需要匹配的 should 语句的数量,它既可以是一个绝对的数字,又可以是个百分比:
GET /my_index/my_type/_search
{
  "query": {
    "bool": {
      "should": [
        { "match": { "title": "brown" }},
        { "match": { "title": "fox"   }},
        { "match": { "title": "dog"   }}
      ],
      "minimum_should_match": 2 
    }
  }
}

3.等价查询:
{
    "match": { "title": "brown fox"}
}

{
  "bool": {
    "should": [
      { "term": { "title": "brown" }},
      { "term": { "title": "fox"   }}
    ]
  }
}

{
    "match": {
        "title": {
            "query":    "brown fox",
            "operator": "and"
        }
    }
}

{
  "bool": {
    "must": [
      { "term": { "title": "brown" }},
      { "term": { "title": "fox"   }}
    ]
  }
}


{
    "match": {
        "title": {
            "query":                "quick brown fox",
            "minimum_should_match": "75%"
        }
    }
}

{
  "bool": {
    "should": [
      { "term": { "title": "brown" }},
      { "term": { "title": "fox"   }},
      { "term": { "title": "quick" }}
    ],
    "minimum_should_match": 2 
  }
}

4.假设想要查询关于 “full-text search(全文搜索)” 的文档,但我们希望为提及 “Elasticsearch” 或 “Lucene” 的文档给予更高的 权重 ,
这里 更高权重 是指如果文档中出现 “Elasticsearch” 或 “Lucene” ,它们会比没有的出现这些词的文档获得更高的相关度评分 _score ,也就是说,它们会出现在结果集的更上面。
GET /_search
{
    "query": {
        "bool": {
            "must": {
                "match": {
                    "content": { 
                        "query":    "full text search",
                        "operator": "and"
                    }
                }
            },
            "should": [ 
                { "match": { "content": "Elasticsearch" }},
                { "match": { "content": "Lucene"        }}
            ]
        }
    }
}
    
content 字段必须包含 full 、 text 和 search 所有三个词。
如果 content 字段也包含 Elasticsearch 或 Lucene ,文档会获得更高的评分 _score 。

5.如果我们想让包含 Lucene 的有更高的权重,并且包含 Elasticsearch 的语句比 Lucene 的权重更高,该如何处理?

我们可以通过指定 boost 来控制任何查询语句的相对的权重, boost 的默认值为 1 ,大于 1 会提升一个语句的相对权重。所以下面重写之前的查询:
GET /_search
{
    "query": {
        "bool": {
            "must": {
                "match": {  
                    "content": {
                        "query":    "full text search",
                        "operator": "and"
                    }
                }
            },
            "should": [
                { "match": {
                    "content": {
                        "query": "Elasticsearch",
                        "boost": 3 
                    }
                }},
                { "match": {
                    "content": {
                        "query": "Lucene",
                        "boost": 2 
                    }
                }}
            ]
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ElasticSearch是一款基于Lucene的分布式搜索引擎,支持各种类型的查询。其中组合查询是指将多个查询条件组合起来进行查询,可以使用ElasticSearch的bool查询实现。 bool查询是一种复合查询,可以将多个查询条件组合起来,支持must、must_not、should、filter四种查询方式。 - must:表示必须匹配的查询条件,相当于“AND”的关系。 - must_not:表示必须不匹配的查询条件,相当于“NOT”的关系。 - should:表示可选匹配的查询条件,相当于“OR”的关系。 - filter:表示不评分的过滤条件,相当于must的变种,但它不会影响评分。 下面是一个使用bool查询实现组合查询的例子: ``` { "query": { "bool": { "must": [ { "match": { "title": "Elasticsearch" }}, { "match": { "content": "search" }} ], "must_not": [ { "match": { "content": "java" }} ], "should": [ { "match": { "author": "John" }}, { "match": { "tags": "elk" }} ], "filter": [ { "range": { "date": { "gte": "2022-01-01" }}} ] } } } ``` 上述查询语句中,我们使用bool查询将多个查询条件组合起来: - must条件表示title必须包含Elasticsearch,content必须包含search,相当于“Elasticsearch AND search”的关系。 - must_not条件表示content必须不包含java,相当于“NOT java”的关系。 - should条件表示author必须包含John,或tags必须包含elk,相当于“John OR elk”的关系。 - filter条件表示date必须大于等于2022-01-01,但不影响评分。 通过使用bool查询,我们可以实现各种复杂的组合查询
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值