格式描述:
term 格式:
"term": {
"FIELD": {"value": "VALUE"}
terms 格式:
"terms": {
"FIELD": [
"VALUE1",
"VALUE2"
]
}
对于 terms,如果和 SQL 语句联系起来的话,那么就相当于 in
准备数据:
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、为帖子字段增加 tag 字段
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"]} }
2、搜索 articleID 为 KDKE-B-9947-#kL5 或 QQPX-R-3956-#aD8 的帖子
GET /forum/_search
{
"query": {
"constant_score": {
"filter": {
"terms": {
"articleID": [
"KDKE-B-9947-#kL5",
"QQPX-R-3956-#aD8"
]
}
}
}
}
}
输出:
{
"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"
]
}
}
]
}
}
3、搜索 tag 中包含 java 的帖子
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"
]
}
}
]
}
}
4、优化搜索结果,仅仅搜索 tag 中只包含 java 的帖子
更新数据增加 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
}
}
]
}
}