业务需求需要对单个字段中内容进行in过滤查询,基于全文检索match api实现。废话不多说,直接看步骤:
创建测试索引:
curl -H "Content-Type: application/json" -H "Authorization: Basic ZWxhc3RpYzoxMjM0NTY=" -XPUT "http://172.16.1.119:9200/match_index" -d'
{
"mappings": {
"data": {
"properties": {
"name": {
"type": "keyword",
"index": "true",
"fields": {
"key": {
"type": "text"
}
}
}
}
}
}
}
'
构建测试数据:
curl -H "Authorization: Basic ZWxhc3RpYzoxMjM0NTY=" -X POST "http://172.16.1.119:9200/_bulk?pretty" -H 'Content-Type: application/json' -d'
{ "index" : { "_index" : "match_index", "_type" : "data", "_id" : "1" } }
{ "name" : "1 2 3 4 5"}
{ "index" : { "_index" : "match_index", "_type" : "data", "_id" : "2" } }
{ "name" : "1 2 3"}
{ "index" : { "_index" : "match_index", "_type" : "data", "_id" : "3" } }
{ "name" : "1 3 5"}
{ "index" : { "_index" : "match_index", "_type" : "data", "_id" : "4" } }
{ "name" : "4 5"}
{ "index" : { "_index" : "match_index", "_type" : "data", "_id" : "5" } }
{ "name" : "3 4 5"}
{ "index" : { "_index" : "match_index", "_type" : "data", "_id" : "6" } }
{ "name" : "1 2 5"}
{ "index" : { "_index" : "match_index", "_type" : "data", "_id" : "7" } }
{ "name" : "1 3 4 5"}
{ "index" : { "_index" : "match_index", "_type" : "data", "_id" : "8" } }
{ "name" : "1"}
{ "index" : { "_index" : "match_index", "_type" : "data", "_id" : "9" } }
{ "name" : "2 3"}
'
match查询(实现单个字段中in查询),返回name字段中包含2并且包含5的全部记录:
{
"query": {
"match" : {
"name.key" : {
"query" : "5 2",
"operator" : "and"
}
}
}
}
精准查询DSL,返回字段name等于"1 2 5"的全部数据:
{
"query": {
"bool": {
"should": [
{
"term": {
"name": "1 2 5"
}
}
]
}
}
}