简体   繁体   English

按数组过滤器长度查询 ElasticSearch

[英]Query ElasticSearch by array filter length

I have a prop containing an array of integers:我有一个包含整数数组的道具:

_source: {
  counts: [
    11,
    7,
    18,
    3,
    22
  ]
}

From another post I know that I can filter by a range using:另一篇文章我知道我可以使用以下范围过滤:

{
  "query": {
    "bool": {
      "must": {
        "match_all": {}   
      },  
      "filter": {
        "range": {
          "counts": {
            "gte": 10,
            "lte": 20
          }
        }
      }
    }
  }
}

However, I need to additionally know if the range match count is greater than a certain number.但是,我还需要知道范围匹配计数是否大于某个数字。 For instance, I only want records back which have less than 3 counts matching between 10 and 20.例如,我只想要返回在 10 到 20 之间匹配的counts少于 3 的记录。

Mapping used:使用的映射:

{
  "properties" : {
    "counts" : {
      "type" :    "integer"
    }
  }
}

These are the docs I indexed:这些是我索引的文档:

{
  "took": 4,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 3,
    "max_score": 1,
    "hits": [
      {
        "_index": "test_index",
        "_type": "test",
        "_id": "2",
        "_score": 1,
        "_source": {
          "counts": [
            13,
            17
          ]
        }
      },
      {
        "_index": "test_index",
        "_type": "test",
        "_id": "1",
        "_score": 1,
        "_source": {
          "counts": [
            11,
            7,
            18,
            3,
            22
          ]
        }
      },
      {
        "_index": "test_index",
        "_type": "test",
        "_id": "3",
        "_score": 1,
        "_source": {
          "counts": [
            11,
            19
          ]
        }
      }
    ]
  }
}

Now try this query:现在试试这个查询:

    {
          "query": {
            "bool": {
              "must": {
                "match_all": {}   
              },  
              "filter": [
                        {"script" : { "script" : "doc['counts'].values.size() < 4" }},
                         {"range": { "counts": { "gte": 10, "lte": 20 } }}
                        ]
                }
          }
 }

Results: Only doc id 2 and 3 are returned.结果:仅返回文档 ID 2 和 3。 Doc 1 is not returned.文档 1 没有返回。

{
  "took": 29,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 1,
    "hits": [
      {
        "_index": "test_index",
        "_type": "test",
        "_id": "2",
        "_score": 1,
        "_source": {
          "counts": [
            13,
            17
          ]
        }
      },
      {
        "_index": "test_index",
        "_type": "test",
        "_id": "3",
        "_score": 1,
        "_source": {
          "counts": [
            11,
            19
          ]
        }
      }
    ]
  }
}

Is this what you are trying to do?这是你想要做的吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM