2015-01-13 75 views
4

如何通過elasticsearch中的數組索引來查詢/過濾?Elasticsearch數組索引查詢

我有一個文件是這樣的: -

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

我要搜索如果 LIN [0]是 「UP」 和 LIN [1]存在。

謝謝。

回答

3

這可能看起來像一個黑客,但它肯定會工作。 首先,我們應用令牌計數類型以及多字段來將令牌數作爲字段捕獲。 因此映射將這個樣子 -

{ 
    "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 接下來,我們可以使用令牌過濾器來檢查令牌「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/modules-advanced-scripting.html

腳本過濾器 - http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-script-filter.html

+0

看起來不錯。但是如果帖子的作者沒有改變索引映射的問題,我會說這種方法加上'bool'查詢更有效。 –

+0

我們如何確保令牌'up'在您推薦的方法中首先出現? –

+0

謝謝@Vineeth Mohan。我已經接受了這個答案。它提供了足夠的指針來建立這個。 – Thanigs