Elasticsearch数组索引查询 [英] Elasticsearch query on array index
问题描述
如何在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"
}
}
}
}
}
}
因此要检查第二个字段是否存在,就像检查该字段值是否大于或等于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;"
}
}
}
}
}
这篇关于Elasticsearch数组索引查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!