本文共 2968 字,大约阅读时间需要 9 分钟。
概述
es 中如何实现 sql 中的in,使用terms实现
语法如下:
term: { "field": "value"}terms: { "field": ["value1", "value2"]}
select * from tbl where col in ("value1", "value2")
POST /forum/article/_bulk{ "update": { "_id": "1"} }{ "doc" : { "tag" : ["java", "hadoop"]} }{ "update": { "_id": "2"} }{ "doc" : { "tag" : ["java"]} }{ "update": { "_id": "3"} }{ "doc" : { "tag" : ["hadoop"]} }{ "update": { "_id": "4"} }{ "doc" : { "tag" : ["java", "elasticsearch"]} }
GET /forum/article/_search { "query": { "constant_score": { "filter": { "terms": { "articleID": [ "KDKE-B-9947-#kL5", "QQPX-R-3956-#aD8" ] } } } }}
搜索tag中包含java的帖子
GET /forum/article/_search{ "query" : { "constant_score" : { "filter" : { "terms" : { "tag" : ["java"] } } } }}
查询返回结果
"took": 2, "timed_out": false, "_shards": { "total": 5, "successful": 5, "failed": 0 }, "hits": { "total": 3, "max_score": 1, "hits": [ { "_index": "forum", "_type": "article", "_id": "2", "_score": 1, "_source": { "articleID": "KDKE-B-9947-#kL5", "userID": 1, "hidden": false, "postDate": "2017-01-02", "tag": [ "java" ] } }, { "_index": "forum", "_type": "article", "_id": "4", "_score": 1, "_source": { "articleID": "QQPX-R-3956-#aD8", "userID": 2, "hidden": true, "postDate": "2017-01-02", "tag": [ "java", "elasticsearch" ] } }, { "_index": "forum", "_type": "article", "_id": "1", "_score": 1, "_source": { "articleID": "XHDK-A-1293-#fJ3", "userID": 1, "hidden": false, "postDate": "2017-01-01", "tag": [ "java", "hadoop" ] } } ] }}
POST /forum/article/_bulk{ "update": { "_id": "1"} }{ "doc" : { "tag_cnt" : 2} }{ "update": { "_id": "2"} }{ "doc" : { "tag_cnt" : 1} }{ "update": { "_id": "3"} }{ "doc" : { "tag_cnt" : 1} }{ "update": { "_id": "4"} }{ "doc" : { "tag_cnt" : 2} }
查询条件:
GET /forum/article/_search{ "query": { "constant_score": { "filter": { "bool": { "must": [ { "term": { "tag_cnt": 1 } }, { "terms": { "tag": ["java"] } } ] } } } }}
转载地址:http://weonn.baihongyu.com/