内容简介:1、搜索发帖日期为2017-01-01,或者帖子ID为XHDK-A-1293-#fJ3的帖子,同时要求帖子的发帖日期不能为2017-01-02输出:must: 标识必须匹配
准备数据:
POST /forum/_bulk { "index": { "_id": 1 }} { "articleID" : "XHDK-A-1293-#fJ3", "userID" : 1, "hidden": false, "postDate": "2017-01-01" } { "index": { "_id": 2 }} { "articleID" : "KDKE-B-9947-#kL5", "userID" : 1, "hidden": false, "postDate": "2017-01-02" } { "index": { "_id": 3 }} { "articleID" : "JODL-X-1937-#pV7", "userID" : 2, "hidden": false, "postDate": "2017-01-01" } { "index": { "_id": 4 }} { "articleID" : "QQPX-R-3956-#aD8", "userID" : 2, "hidden": true, "postDate": "2017-01-02" }
需求
1、搜索发帖日期为2017-01-01,或者帖子ID为XHDK-A-1293-#fJ3的帖子,同时要求帖子的发帖日期不能为2017-01-02
select * from forum where (post_date = '2017-01-01' or article_id = 'XHDK-A-1293-#fJ3') and post_date != '2017-01-02'
GET /forum/_search { "query": { "constant_score": { "filter": { "bool": { "should": [ { "term": { "postDate": "2017-01-01" } }, { "term": { "articleID": "XHDK-A-1293-#fJ3" } } ], "must_not": [ { "term": { "postDate": "2017-01-02" } } ] } } } } }
输出:
{ "took" : 55, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 2, "relation" : "eq" }, "max_score" : 1.0, "hits" : [ { "_index" : "forum", "_type" : "_doc", "_id" : "1", "_score" : 1.0, "_source" : { "articleID" : "XHDK-A-1293-#fJ3", "userID" : 1, "hidden" : false, "postDate" : "2017-01-01" } }, { "_index" : "forum", "_type" : "_doc", "_id" : "3", "_score" : 1.0, "_source" : { "articleID" : "JODL-X-1937-#pV7", "userID" : 2, "hidden" : false, "postDate" : "2017-01-01" } } ] } }
must: 标识必须匹配
should: 可以匹配其中一个即可
must_not: 必须不匹配
2、搜索帖子ID为XHDK-A-1293-#fJ3, 或者是帖子ID为JODL-X-1937-#pV7而且发帖日期为2017-01-01的帖子
select * from forum where article_id = 'XHDK-A-1293-#fJ3' or (article_id = 'JODL-X-1937-#pV7' and post_date = '2017-01-01')
GET /forum/_search { "query": { "constant_score": { "filter": { "bool": { "should": [ { "term": { "articleID": "XHDK-A-1293-#fJ3" } }, { "bool": { "must": [ { "term": { "articleID": "JODL-X-1937-#pV7" } }, { "term": { "postDate": "2017-01-01" } } ] } } ] } } } } }
输出:
{ "took" : 7, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 2, "relation" : "eq" }, "max_score" : 1.0, "hits" : [ { "_index" : "forum", "_type" : "_doc", "_id" : "1", "_score" : 1.0, "_source" : { "articleID" : "XHDK-A-1293-#fJ3", "userID" : 1, "hidden" : false, "postDate" : "2017-01-01" } }, { "_index" : "forum", "_type" : "_doc", "_id" : "3", "_score" : 1.0, "_source" : { "articleID" : "JODL-X-1937-#pV7", "userID" : 2, "hidden" : false, "postDate" : "2017-01-01" } } ] } }
以上所述就是小编给大家介绍的《elasticsearch学习笔记高级篇(三)——在案例中实战基于bool组合多个filter条件搜索》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:- 暗云III v3.0 、Mykings、Mirai多个病毒家族结伴来袭的实战分析
- 监控多个Kubernetes集群
- 管理多个远端仓库
- Git 推送到多个仓库
- 运行多个 npm script
- Python:尝试反序列化文件中的多个JSON对象,每个对象跨越多个但一致的间隔数量的行
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。