elasticsearch - ElasticSearch嵌套NumericRangeQuery使用列表中的最小值进行比较

标签 elasticsearch

我有以下数据:

[{
    "id": "1",
    "listItems": [
        {
            "key": "li1",
            "value": 100
        },
        {
            "key": "li2",
            "value": 5000
        }
    ]
},
{
    "id": "2",
    "listItems": [
        {
            "key": "li3",
            "value": 200
        },
        {
            "key": "li2",
            "value": 2000
        }
    ]
}]
我正在尝试做一个NumericRangeQuery过滤器,以便每个文档的listItems中的MIN值在一个范围之间匹配。举例来说,我的范围是150到15000。
我知道如何编写此代码的唯一方法是使用脚本查询,但是它似乎无法正常工作,因为代码似乎仍会在listItems下获取任何值以尝试与范围匹配,而不是像我告诉的那样获取MIN它来。这是我的查询:
{
"track_total_hits": true,
"from": 0,
"min_score": 0.0,
"query": {
    "bool": {
        "must": [
            {
                "nested": {
                    "path": "listItems",
                    "query": {
                        "script": {
                            "script": "double minVal = 0; minVal = doc['listItems.value'][0]; for (wp in doc['listItems.value']) {if (wp < minVal) { minVal = wp;}} return minVal >= 150 && minVal <= 15000"
                        }
                    }
                }
            }
        ]
    }
}}
有人看到我没看到的东西吗?

最佳答案

搜索查询执行以下聚合:
id字段上的

  • Terms aggregation
  • 上的
  • Min aggregation
  • Bucket Selector aggregation是父管道聚合,执行一个脚本,该脚本确定当前存储桶是否将保留在父多存储桶聚合中。

  • 添加带有索引映射,索引数据,搜索查询和搜索结果的工作示例
    索引映射:
    {
      "mappings": {
        "properties": {
          "listItems": {
            "type": "nested" 
          },
          "id":{
            "type":"text",
            "fielddata":"true"
          }
        }
      }
    }
    
    索引数据:
    {
        "id" : "1",
        "listItems" : 
            [
                {
                    "key" : "li1",
                    "value" : 100
                },
                {
                    "key" : "li2",
                    "value" : 5000
                }
            ]
    }
    {
        "id" : "2",
        "listItems" : 
            [
                {
                    "key" : "li3",
                    "value" : 200
                },
                {
                    "key" : "li2",
                    "value" : 2000
                }
            ]
    }
    
    搜索查询:
    {
        "size": 0,
        "aggs": {
            "id_terms": {
                "terms": {
                    "field": "id"
                },
                "aggs": {
                    "nested_entries": {
                        "nested": {
                            "path": "listItems"
                        },
                        "aggs": {
                            "min_position": {
                                "min": {
                                    "field": "listItems.value"
                                }
                            }
                        }
                    },
                    "value_range": {
                        "bucket_selector": {
                            "buckets_path": {
                                "totalValues": "nested_entries>min_position"
                            },
                            "script": "params.totalValues >= 150 && params.totalValues < 15000"
                        }
                    }
                }
            }
        }
    }
    
    搜索结果:
    "aggregations": {
        "id_terms": {
          "doc_count_error_upper_bound": 0,
          "sum_other_doc_count": 0,
          "buckets": [
            {
              "key": "2",
              "doc_count": 1,
              "nested_entries": {
                "doc_count": 2,
                "min_position": {
                  "value": 200.0
                }
              }
            }
          ]
        }
      }
    

    关于elasticsearch - ElasticSearch嵌套NumericRangeQuery使用列表中的最小值进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64070854/

    相关文章:

    elasticsearch - Elasticsearch :匹配其数组包含此字段的文档

    elasticsearch - 使用 Elasticsearch 的简单 AND 查询

    java - Snowball search_analyzer 不适用于 multi_match 查询

    elasticsearch - Elasticsearch建议功能的前缀为like

    java - Elasticsearch中的字符串字典范围查询

    amazon-web-services - Elasticsearch AWS Connect 失败?

    json - 来自 Haskell 的查询请求,包含 "same data"的两倍

    python - Elasticsearch必须具有多个条件

    elasticsearch - kibana 4.0 中比率的趋势

    elasticsearch - 如何解释elasticsearch中的query