Elasticsearch数组索引查询 [英] Elasticsearch query on array index

查看:168
本文介绍了Elasticsearch数组索引查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Elasticsearch中按数组索引查询/过滤?

How do I query/filter by index of an array in elasticsearch?

我有一个像这样的文档:-

I have a document like this:-

PUT /edi832/record/1
{
    "LIN": [ "UP", "123456789" ]
}

我要搜索 LIN [0] 是 UP还是 LIN [1] 存在。

I want to search if LIN[0] is "UP" and LIN[1] exists.

谢谢。

推荐答案

这看起来像是hack,但是那么它将肯定起作用。
首先,我们将令牌计数类型与多字段一起应用以捕获作为字段的令牌数量。
因此映射将如下所示:

This might look like a hack , but then it will work for sure. First we apply token count type along with multi field to capture the the number of tokens as a field. So the mapping will look like this -

{
    "record" : {
        "properties" : {
            "LIN" : {
                "type" : "string",
                "fields" : {
                    "word_count": {
                        "type" : "token_count",
                        "store" : "yes",
                        "analyzer" : "standard"
                    }
                }
            }
        }
    }
}

LINK- http://www.elasticsearch.org/guide/en/elasticsearch/reference/current /mapping-core-types.html#token_count

因此要检查第二个字段是否存在,就像检查该字段值是否大于或等于2。
接下来,我们可以使用令牌过滤器来检查令牌是否向上 ist位置0。
我们可以使用脚本过滤器进行检查。
因此,下面的查询应该可以正常工作-

So to check if the second field exists , its as easy as checking if this field value is more than or equal to 2. Next we can use the token filter to check if the token "up" exists in position 0. We can use the scripted filter to check this. Hence a query like below should work -

{
  "query": {
    "filtered": {
      "query": {
        "range": {
          "LIN.word_count": {
            "gte": 2
          }
        }
      },
      "filter": {
        "script": {
          "script": "for(pos : _index['LIN'].get('up',_POSITIONS)){ if(pos.position == 0) { return true}};return false;"
        }
      }
    }
  }
}

高级脚本- http:// www.elasticsearch.org/guide/zh-cn/elasticsearch/reference/current/modules-advanced-scripting.html

脚本过滤器- http://www.elasticsearch.org/guide/zh-CN elasticsearch / reference / current / query-dsl-script-filter.html

这篇关于Elasticsearch数组索引查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆