2016-09-07 60 views
0

我試圖編寫一個Elasticsearch查詢,它將返回具有嵌套字段的元素。不過,我顯然遇到了很多困難。我給這家字段映射通常看起來如下:篩選存在嵌套字段的Elaticsearch查詢

{ "myType": { "properties": { "hello": { "type": "nested", "properties": { "foo": {"type": "string", "index": "not_analyzed"}, "bar": {"type": "string", "index": "not_analyzed"}, } } } } }

我的查詢一般如下: { "query": { "filtered": { "filter": { "exists": { "field": "hello.foo" } } } } }

該查詢返回0匹配的文件,即使我知道有匹配的文件。

我也嘗試在nested查詢中使用exists查詢,但給出了有關nested查詢不支持exists查詢的錯誤消息。

我對Elasticsearch 2.3進行測試任何幫助將不勝感激!

回答

1

我希望這將有助於

{ 
    "query": { 
    "bool": { 
     "must": [ 
     { 
      "nested": { 
      "path": "hello", 
      "filter": { 
       "term": {  // replace term to "match" in case of fulltext 
       "hello.foo": "value to be searched" 
       } 
      } 
      } 
     } 
     ] 
    } 
    }, 
    "from": 0, 
    "size": 50 
} 
+0

哈哈,我正要後,我會找到我的答案從舊的博客文章(http://joelabrahamsson.com/elasticsearch-nested-mapping-和過濾器/)。但是這正是它所說的(在嵌套過濾器中使用另一個過濾器,然後使用exists過濾器)。我將編輯你的'term'過濾器作爲'存在'過濾器並且標記你的答案是正確的!謝謝您的幫助! – user114241