Elasticsearch 查询数组索引
Elasticsearch query on array index
如何在 elasticsearch 中通过数组索引 query/filter?
我有这样一个文件:-
PUT /edi832/record/1
{
"LIN": [ "UP", "123456789" ]
}
我想搜索 LIN[0] 是 "UP" 并且 LIN[1] 是否存在。
谢谢。
这可能看起来像一个 hack,但它肯定会起作用。
首先,我们应用令牌计数类型和多字段来捕获令牌的数量作为一个字段。
所以映射看起来像这样 -
{
"record" : {
"properties" : {
"LIN" : {
"type" : "string",
"fields" : {
"word_count": {
"type" : "token_count",
"store" : "yes",
"analyzer" : "standard"
}
}
}
}
}
}
所以检查第二个字段是否存在,就像检查这个字段值是否大于等于2一样简单。
接下来我们可以使用令牌过滤器来检查令牌 "up" 是否存在于位置 0。
我们可以使用脚本过滤器来检查这一点。
因此,像下面这样的查询应该有效 -
{
"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/en/elasticsearch/reference/current/query-dsl-script-filter.html
如何在 elasticsearch 中通过数组索引 query/filter?
我有这样一个文件:-
PUT /edi832/record/1
{
"LIN": [ "UP", "123456789" ]
}
我想搜索 LIN[0] 是 "UP" 并且 LIN[1] 是否存在。
谢谢。
这可能看起来像一个 hack,但它肯定会起作用。 首先,我们应用令牌计数类型和多字段来捕获令牌的数量作为一个字段。 所以映射看起来像这样 -
{
"record" : {
"properties" : {
"LIN" : {
"type" : "string",
"fields" : {
"word_count": {
"type" : "token_count",
"store" : "yes",
"analyzer" : "standard"
}
}
}
}
}
}
所以检查第二个字段是否存在,就像检查这个字段值是否大于等于2一样简单。 接下来我们可以使用令牌过滤器来检查令牌 "up" 是否存在于位置 0。 我们可以使用脚本过滤器来检查这一点。 因此,像下面这样的查询应该有效 -
{
"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/en/elasticsearch/reference/current/query-dsl-script-filter.html