term: {"field": "value"}
terms: {"field": ["value1", "value2"]}
sql中的in
select * from tbl where col in ("value1", "value2")
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"]} }
{ "update": { "_id": "5"} }
{ "doc" : {"tag" : ["C#", "spark", "flink"]} }
{ "update": { "_id": "6"} }
{ "doc" : {"tag" : ["java", "flink"]} }
{ "update": { "_id": "7"} }
{ "doc" : {"tag" : ["hadoop", "kafak", "strom"]} }
{ "update": { "_id": "8"} }
{ "doc" : {"tag" : ["spark", "elasticsearch", "flink"]} }
2、搜索articleID
为KDKE-B-9947-#kL5
或QQPX-R-3956-#aD8
的帖子,搜索tag
中包含java
的帖子
GET /forum/_search
{
"query": {
"constant_score": {
"filter": {
"terms": {
"articleID": [
"KDKE-B-9947-#kL5",
"QQPX-R-3956-#aD8"
]
}
}
}
}
}
GET /forum/_search
{
"query" : {
"constant_score" : {
"filter" : {
"terms" : {
"tag" : ["java"]
}
}
}
}
}
3、优化搜索结果,仅仅搜索tag只包含java的帖子
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": [
{
"term": {
"tag_cnt": 1
}
},
{
"terms": {
"tag": ["java"]
}
}
]
}
}
}
}
}
["java", "hadoop", "elasticsearch"]
4、学到的知识点梳理
(1)terms
多值搜索
(2)优化terms
多值搜索的结果
(3)相当于SQL
中的in
语句