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
    评论
Elasticsearch 聚合结果带字段可以使用 Sub Aggregation 来实现。Sub Aggregation 是在一个 Aggregation 嵌套另一个 Aggregation,可以对上一层聚合结果进行进一步的分析,包括对字段进行统计、排序等操作。 以下是一个简单的示例代码,实现按照某个字段进行分组,并统计每个分组一个字段的平均值: ```java SearchRequest searchRequest = new SearchRequest("index_name"); SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); // 构建聚合条件,按照字段1分组 TermsAggregationBuilder aggregationBuilder1 = AggregationBuilders.terms("agg_name1").field("field1"); // 构建子聚合条件,统计字段2的平均值 AvgAggregationBuilder aggregationBuilder2 = AggregationBuilders.avg("agg_name2").field("field2"); // 将子聚合条件添加到父聚合条件 aggregationBuilder1.subAggregation(aggregationBuilder2); // 将聚合条件添加到搜索请求 searchSourceBuilder.aggregation(aggregationBuilder1); searchRequest.source(searchSourceBuilder); SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT); // 获取聚合结果 Terms aggResult1 = searchResponse.getAggregations().get("agg_name1"); // 遍历每个分组,获取子聚合结果 for (Terms.Bucket bucket : aggResult1.getBuckets()) { Avg aggResult2 = bucket.getAggregations().get("agg_name2"); double avg = aggResult2.getValue(); System.out.println("Field1: " + bucket.getKeyAsString() + ", Avg of Field2: " + avg); } ``` 需要注意的是,Sub Aggregation 可以嵌套多层,可以根据具体需求进行扩展。同时还可以使用 Bucket Sort Aggregation 对分组结果进行排序,或使用 Bucket Selector Aggregation 对分组结果进行筛选。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值