elasticsearch6.x 中单字段支持精确查询和全文检索、对字段中数据进行in过滤查询.

     业务需求需要对单个字段中内容进行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"
                    }
                }
            ]
        }
    }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值