arrays - 通过数组过滤器长度查询ElasticSearch

标签 arrays elasticsearch filter

我有一个包含整数数组的 Prop :

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

another post我知道我可以使用以下范围进行过滤:
{
  "query": {
    "bool": {
      "must": {
        "match_all": {}   
      },  
      "filter": {
        "range": {
          "counts": {
            "gte": 10,
            "lte": 20
          }
        }
      }
    }
  }
}

但是,我还需要知道范围匹配计数是否大于某个数字。例如,我只希望返回的记录少于10个和20个之间的counts少于3个。

最佳答案

使用的映射:

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

这些是我索引的文档:
{
  "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
          ]
        }
      }
    ]
  }
}

现在尝试以下查询:
    {
          "query": {
            "bool": {
              "must": {
                "match_all": {}   
              },  
              "filter": [
                        {"script" : { "script" : "doc['counts'].values.size() < 4" }},
                         {"range": { "counts": { "gte": 10, "lte": 20 } }}
                        ]
                }
          }
 }

结果:仅返回文档ID 2和3。 Doc 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
          ]
        }
      }
    ]
  }
}

这是您要做什么?

关于arrays - 通过数组过滤器长度查询ElasticSearch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39595665/

相关文章:

javascript - 将数组内对象内的数组映射到单个数组

javascript - 勾选随机开始

ruby-on-rails - Elasticsearch 无法以索引开头且别名需要唯一错误

elasticsearch - 使用 elasticsearch 作为数据存储来搜索和排序数据的有效方法

javascript - 如何过滤/搜索嵌套的 Backbone 集合?

javascript - 如何将嵌套的对象数组推送到数组?

ios - 遍历数组并在 Swift 中增加计数器

elasticsearch - 查询 ElasticSearch - 在不同时间匹配的多个术语

java - 如何从 Log4j 中过滤特定的异常?

python - 使用另一个字典过滤Python字典