term格式:java
"term": { "FIELD": { "value": "VALUE" }
terms格式:elasticsearch
"terms": { "FIELD": [ "VALUE1", "VALUE2" ] }
對於terms,若是和SQL語句聯繫起來的話,那麼就至關於inoop
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" }
POST /forum/_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/_search { "query": { "constant_score": { "filter": { "terms": { "articleID": [ "KDKE-B-9947-#kL5", "QQPX-R-3956-#aD8" ] } } } } }
輸出:post
{ "took" : 179, "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" : "2", "_score" : 1.0, "_source" : { "articleID" : "KDKE-B-9947-#kL5", "userID" : 1, "hidden" : false, "postDate" : "2017-01-02", "tag" : [ "java" ] } }, { "_index" : "forum", "_type" : "_doc", "_id" : "4", "_score" : 1.0, "_source" : { "articleID" : "QQPX-R-3956-#aD8", "userID" : 2, "hidden" : true, "postDate" : "2017-01-02", "tag" : [ "java", "elasticsearch" ] } } ] } }
GET /forum/_search { "query": { "constant_score": { "filter": { "terms": { "tag": [ "java" ] } } } } }
{ "took" : 0, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 3, "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", "tag" : [ "java", "hadoop" ] } }, { "_index" : "forum", "_type" : "_doc", "_id" : "2", "_score" : 1.0, "_source" : { "articleID" : "KDKE-B-9947-#kL5", "userID" : 1, "hidden" : false, "postDate" : "2017-01-02", "tag" : [ "java" ] } }, { "_index" : "forum", "_type" : "_doc", "_id" : "4", "_score" : 1.0, "_source" : { "articleID" : "QQPX-R-3956-#aD8", "userID" : 2, "hidden" : true, "postDate" : "2017-01-02", "tag" : [ "java", "elasticsearch" ] } } ] } }
更新數據增長tag字段包含的標籤數量優化
POST /forum/_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/_search { "query": { "constant_score": { "filter": { "bool": { "must": [ { "terms": { "tag": ["java"] } }, { "term": { "tag_cnt": 1 } } ] } } } } }
{ "took" : 192, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 1, "relation" : "eq" }, "max_score" : 1.0, "hits" : [ { "_index" : "forum", "_type" : "_doc", "_id" : "2", "_score" : 1.0, "_source" : { "articleID" : "KDKE-B-9947-#kL5", "userID" : 1, "hidden" : false, "postDate" : "2017-01-02", "tag" : [ "java" ], "tag_cnt" : 1 } } ] } }