首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >按数组过滤长度查询ElasticSearch

问按数组过滤长度查询ElasticSearch
EN

Stack Overflow用户
提问于 2016-09-20 21:37:32
回答 1查看 5.1K关注 0票数 1

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

代码语言:javascript
复制
_source: {
  counts: [
    11,
    7,
    18,
    3,
    22
  ]
}

在another post中,我知道可以使用以下命令按范围进行过滤:

代码语言:javascript
复制
{
  "query": {
    "bool": {
      "must": {
        "match_all": {}   
      },  
      "filter": {
        "range": {
          "counts": {
            "gte": 10,
            "lte": 20
          }
        }
      }
    }
  }
}

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-09-21 01:48:53

使用的映射:

代码语言:javascript
复制
{
  "properties" : {
    "counts" : {
      "type" :    "integer"
    }
  }
}

以下是我索引的文档:

代码语言:javascript
复制
{
  "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
          ]
        }
      }
    ]
  }
}

现在尝试这个查询:

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

结果:只返回文档id 2和3。没有退还单据%1。

代码语言:javascript
复制
{
  "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
          ]
        }
      }
    ]
  }
}

这就是你想要做的吗?

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39595665

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档