2017-10-18 135 views
0

我試圖在Elasticsearch查詢(ES 5.6.2)中的nested document內的字段上進行過濾。嵌套文檔本身是主文檔內部對象內的字段。映射是這樣的:如何查詢內部對象中的Elasticsearch嵌套文檔?

{ 
    "mappings": { 
     "container": { 
      "properties": { 
       "host": { 
        "properties": { 
         "tags_nested": { 
          "type": "nested", 
          "properties": { 
           "tag_key": { 
            "type": "keyword" 
           }, 
           "tag_val": { 
            "type": "keyword" 
           } 
          } 
         } 
        } 
       } 
      } 
     } 
    } 
} 

我想在host.tags_nested.tag_keys過濾器,但我無法找出正確的語法將host內部對象內訪問嵌套tags_nested文件。我想下面的查詢,不返回任何結果,當我知道有一些應符合:

{ 
    "query": { 
     "nested": { 
      "path": "host.tags_nested", 
      "query": { 
       "bool": { 
        "filter": [ 
         { 
          "term": { 
           "host.tags_nested.tag_key": "example_key" 
          } 
         } 
        ] 
       } 
      } 
     } 
    } 
} 

按照ES docs,你可以做一個nested查詢嵌套文檔內查詢,由傳遞一個path,它對應於嵌套文檔的字段名稱。但是當path位於內部對象內並且需要使用點符號訪問時,這似乎不起作用。

任何想法?

回答

0

試試這個。 Term Query搜索我們指定的確切單詞。所以,對於那個使用fieldname.keyword,因爲關鍵字存儲我們索引他們的確切文本。

{ 
    "query": { 
     "nested": { 
      "path": "host.tags_nested", 
      "query": { 
       "bool": { 
        "filter": [ 
         { 
          "term": { 
           "host.tags_nested.tag_key.keyword": "example_key" 
          } 
         } 
        ] 
       } 
      } 
     } 
    } 
}