ES之多条件、范围查询

ES之多条件、范围查询

一、多条件查询
1.条件“且”,即查询"title"为"test6",且"num"为5的数据
【GET】请求:http://127.0.0.1:9200/test-index-1/_search,参数如下

{
    "query":{
        "bool":{
            "must":[
                {
                    "match":{
                        "title": "test6"
                    }
                },
                {
                    "match":{
                        "num": 5
                    }
                }
            ]
        }
    }
}

结果如下

{
    "took": 16,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 2.7917595,
        "hits": [
            {
                "_index": "test-index-1",
                "_type": "_doc",
                "_id": "s_Hyw30BJJ5e1YHwWWcU",
                "_score": 2.7917595,
                "_source": {
                    "title": "test6",
                    "num": 5,
                    "date": "20211213"
                }
            }
        ]
    }
}

2.条件“或”,即查询"title"为"test6",或"title"为"test8"的数据
【GET】请求:http://127.0.0.1:9200/test-index-1/_search,参数如下

{
    "query":{
        "bool":{
            "should":[
                {
                    "match":{
                        "title": "test6"
                    }
                },
                {
                    "match":{
                        "title": "test8"
                    }
                }
            ]
        }
    }
}

结果如下

{
    "took": 6,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 2,
            "relation": "eq"
        },
        "max_score": 1.7917595,
        "hits": [
            {
                "_index": "test-index-1",
                "_type": "_doc",
                "_id": "s_Hyw30BJJ5e1YHwWWcU",
                "_score": 1.7917595,
                "_source": {
                    "title": "test6",
                    "num": 5,
                    "date": "20211213"
                }
            },
            {
                "_index": "test-index-1",
                "_type": "_doc",
                "_id": "tfHyw30BJJ5e1YHwbmfT",
                "_score": 1.7917595,
                "_source": {
                    "title": "test8",
                    "num": 5,
                    "date": "20211213"
                }
            }
        ]
    }
}

二、范围查询
查询“num”小于4的数据
gt:大于
gte:大于等于
lt:小于
lte:小于等于
【GET】请求:http://127.0.0.1:9200/test-index-1/_search,参数如下

{
    "query":{
        "bool":{
            "filter":{
                "range":{
                    "num":{
                        "lt":4
                    }
                }
            }
        }
    }
}

结果如下

{
    "took": 2,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 3,
            "relation": "eq"
        },
        "max_score": 0.0,
        "hits": [
            {
                "_index": "test-index-1",
                "_type": "_doc",
                "_id": "rvHxw30BJJ5e1YHw6meH",
                "_score": 0.0,
                "_source": {
                    "title": "test1",
                    "num": 1,
                    "date": "20211213"
                }
            },
            {
                "_index": "test-index-1",
                "_type": "_doc",
                "_id": "r_Hxw30BJJ5e1YHw_2fX",
                "_score": 0.0,
                "_source": {
                    "title": "test2",
                    "num": 2,
                    "date": "20211213"
                }
            },
            {
                "_index": "test-index-1",
                "_type": "_doc",
                "_id": "sPHyw30BJJ5e1YHwEGfB",
                "_score": 0.0,
                "_source": {
                    "title": "test3",
                    "num": 3,
                    "date": "20211213"
                }
            }
        ]
    }
}

注意:should与must或filter在同一层级直接使用时,should会失效,需要加入参数"minimum_should_match":1,或者should当做子层级
共用时参考 https://blog.csdn.net/andy_5826_liu/article/details/103161654

  • 1
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Elasticsearch条件查询是指在搜索时,同时使用多个条件进行过滤和匹配,以获得更精确的搜索结果。这些条件可以包括文本匹配、范围匹配、布尔逻辑、聚合等。在 Elasticsearch 中,可以使用 Query DSL 或者简单查询字符串语法来实现多条件查询。同时,还可以使用过滤器来缓存查询结果,提高查询性能。 ### 回答2: Elasticsearch是一个分布式全文搜索和分析引擎,可以通过多条件查询来实现更精确的搜索。多条件查询通常涉及到复合查询和布尔查询等概念,以下将介绍这些查询的用法和实例。 一、复合查询 复合查询指的是将多个查询条件组合在一起的查询方式,目的是筛选出符合多个查询条件的文档。Elasticsearch提供了多种复合查询的方式,包括bool查询、dis_max查询、constant_score + bool查询等。 1.boole查询 bool查询是最常见和最灵活的复合查询,它可以组合多个查询条件,包括match、term、range等查询类型。bool查询包括must、should、must_not和filter四种子查询方式。 -must查询表示必须匹配的条件,相当于“且”的关系。 -should查询表示可选匹配的条件,相当于“或”的关系。 -must_not查询表示必须不匹配的条件,相当于“非”的关系。 -filter查询和must查询的作用类似,但是不计算相关性得分,可以提高查询的性能。 例如,下面的bool查询语句同时执行了must和filter查询: { "query": { "bool": { "must": [ { "match": { "title": "Elasticsearch" } }, { "match": { "content": "搜索引擎" } } ], "filter": [ { "range": { "published_date": { "lte": "2019-01-01" } } } ] } } } 上面的查询语句可以筛选出标题包含“Elasticsearch”和内容包含“搜索引擎”,并且发布日期早于2019年的文档。 2.dis_max查询 dis_max查询是另一种复合查询方式,可以用来查询多个查询条件中的最佳匹配。当某个查询条件与文档匹配时,其得分会计入总得分结果中。dis_max查询的语法如下: { "dis_max": { "tie_breaker": 0.7, "boost": 1.2, "queries": [ { "match": { "title": "Elasticsearch" } }, { "match": { "content": "搜索引擎" } } ] } } 其中,tie_breaker表示当两个查询条件得分相同时,如何进行区分的参数。boost表示整个查询的得分增益。相比于bool查询,dis_max查询更加适合用于多个查询条件中找到最佳匹配的场景,如搜索引擎建议结果的查询。 3.constant_score + bool查询 constant_score + bool查询是使用filter查询来建立常量评分(单一得分)的bool查询。这种查询方式可以优化查询性能,尤其适合用于极端情形下的查询语句,例如查询所有文档或者特别大的文档集合。 常用的constant_score + bool查询语句如下: { "query": { "constant_score": { "filter": { "bool": { "should": [ { "term": { "type": "image" } }, { "term": { "type": "video" } } ] } }, "boost": 1.2 } } } 二、布尔查询 布尔查询是一种对字段进行逻辑运算的查询方式,可以对多个查询结果进行合并,计算出最终的匹配结果。这种查询方式提供了and、or、not等运算符,可以灵活处理查询条件之间的关系。例如: - and运算符:用于匹配包含所有条件的文档,语法如下: { "query": { "bool": { "must": [ { "match": { "title": "Elasticsearch" } }, { "match": { "content": "搜索引擎" } } ] } } } 该查询语句可以匹配标题中包含“Elasticsearch”和内容中包含“搜索引擎”的文档。 - or运算符:用于匹配符合任意一个条件的文档,语法如下: { "query": { "bool": { "should": [ { "match": { "title": "Elasticsearch" } }, { "match": { "content": "搜索引擎" } } ] } } } 该查询语句可以匹配标题中包含“Elasticsearch”或内容中包含“搜索引擎”的文档。 - not运算符:用于排除符合某个条件的文档,语法如下: { "query": { "bool": { "must_not": [ { "match": { "title": "Elasticsearch" } } ] } } } 该查询语句可以匹配不包含“Elasticsearch”关键词的文档。 以上是Elasticsearch条件查询的基本概念和实例,当然Elasticsearch还有更多高级查询方式,例如聚合查询、模糊查询、跨字段查询等,可以根据具体的查询需求选择合适的方式。 ### 回答3: Elasticsearch是一个开源搜索和分析引擎,提供了全文搜索、实时分析和灵活的数据可视化等功能。多条件查询Elasticsearch常用的查询方式,其基本思路是通过多个查询条件来组合查询,以便更准确地获取所需的结果。 多条件查询分为两种类型:bool查询和filtered查询。 bool查询是基于布尔运算来组合多个查询条件查询方式。可以通过must、should、must\_not和filter等布尔运算子句来定义查询条件。其中must子句表示必须符合的查询条件,should子句表示可能符合的查询条件,must\_not子句表示不符合的查询条件,filter子句表示过滤查询条件。 例如,以下代码展示了如何使用bool查询来组合多个查询条件: ``` { "query": { "bool": { "must": [ { "match": { "title": "Elasticsearch" } }, { "match": { "content": "搜索引擎" } } ], "should": [ { "match": { "category": "IT" } }, { "match": { "category": "互联网" } } ], "must_not": [ { "match": { "source": "百度" } } ], "filter": [ { "range": { "timestamp": { "gte": "2020-01-01", "lte": "2020-12-31" } } } ] } } } ``` 在以上示例中,bool查询将must、should、must\_not和filter等子句组合在一起,实现了同时满足title为“Elasticsearch”和content为“搜索引擎”,并且category为“IT”或“互联网”、并且source不为“百度”、并且timestamp在2020年1月1日至2020年12月31日之间的查询条件。 filtered查询是基于过滤器来过滤查询条件查询方式。可以通过一个filter子句来指定过滤器,将查询和过滤器进行组合来获取所需的结果。filtered查询主要用于优化查询速度、减少查询结果的数量。 例如,以下代码展示了如何使用filtered查询来过滤查询条件: ``` { "query": { "filtered": { "query": { "match": { "content": "搜索引擎" } }, "filter": [ { "range": { "timestamp": { "gte": "2020-01-01", "lte": "2020-12-31" } } }, { "term": { "category": "IT" } } ] } } } ``` 在以上示例中,filtered查询将query子句和filter子句组合在一起,实现了content为“搜索引擎”、并且timestamp在2020年1月1日至2020年12月31日之间、并且category为“IT”的查询条件。 综上所述,多条件查询Elasticsearch的常用查询方式。通过,bool查询和filtered查询两种方式,您可以轻松地实现多个查询条件的组合查询,以便更准确地获取所需的结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值