按数组过滤器长度查询ElasticSearch

时间:2016-09-20 13:37:32

标签: arrays elasticsearch filter

我有一个包含整数数组的道具:

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

来自another post我知道我可以使用以下方式按范围进行过滤:

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

但是,我还需要知道范围匹配计数是否大于某个数字。例如,我只想要记录在10到20之间的匹配小于3 counts

1 个答案:

答案 0 :(得分:1)

使用的映射:

Foundation.h

这些是我编入索引的文档:

AppKit.h

现在尝试此查询:

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

结果:仅返回doc id 2和3。文档1不会被退回。

{
  "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
          ]
        }
      }
    ]
  }
}

这是你想要做的吗?