2016-01-01 77 views
2

我有以下結構:搜索特定的字段作爲一個文檔

{ 
    "mappings": { 
     "document": { 
      "properties": { 
       "title": { 
        "type": "string" 
       }, 
       "paragraphs": { 
        "type": "nested", 
        "properties": { 
         "paragraph": { 
          "type" : "object", 
          "properties" : { 
           "content": { "type": "string"}, 
           "number":{"type":"integer"} 
          } 
         } 
        } 
       } 
      } 
     } 
    } 
} 

有了這些樣本文件

{ 
    "title":"Dubai seeks cause of massive hotel fire at New Year", 
    "paragraphs":[ 
    {"paragraph": {"number": "1", "content":"Firefighters managed to subdue the blaze, but part of the Address Downtown Hotel is still smouldering."}}, 
    {"paragraph": {"number": "2", "content":"A BBC reporter says a significant fire is still visible on the 20th floor, where the blaze apparently started."}}, 
    {"paragraph": {"number": "3", "content":"The tower was evacuated and 16 people were hurt. But a fireworks show went ahead at the Burj Khalifa tower nearby."}}, 
    {"paragraph": {"number": "4", "content":"The Burj Khalifa is the world's tallest building and an iconic symbol of the United Arab Emirates (UAE)."}}] 
} 

{ 
    "title":"Munich not under imminent IS threat", 
    "paragraphs":[{"paragraph": {"number": "1", "content":"German officials say there is no sign of any imminent terror attack, after an alert that shut down two Munich railway stations on New Year's Eve."}}] 
} 

我現在可以搜索使用每個段落

{ 
    "query": { 
     "nested": { 
      "path": "paragraphs", "query": { 
       "query_string": { 
        "default_field": "paragraphs.paragraph.content", 
        "query": "Firefighters AND still" 
       } 
      } 
     } 
    } 
} 

問題:我怎樣才能查詢一個查詢幾個p aragraphs但只有內容字段?

這工作,但搜索所有領域

{ 
    "query": { 
    "query_string": { 
     "query": "Firefighters AND apparently AND 1" 
    } 
    } 
} 

它匹配消防隊員從第1款和顯然第2款的,我想。然而我不想因爲它不是一個內容字段而被匹配。

澄清:第一個搜索按照我想要的段落進行搜索。不過,我也希望能夠搜索整個文檔(所有段落)。

解決方案 我補充說:「include_in_parent」:真實的,因爲它在https://www.elastic.co/guide/en/elasticsearch/reference/1.7/mapping-nested-type.html

+0

你的第一個查詢有什麼問題?是不是經歷了所有段落? – ChintanShah25

+0

是。但我也希望選擇同時搜索所有段落,以便「消防員顯然」會返回文檔,即使它們處於不同的段落中 – user568327

+0

您可以嘗試編寫腳本。 – Ashalynd

回答

1

提到要查詢的方式是錯誤的,因爲nested documents分別索引。請參閱doc的最後一段。

您的查詢

{ 
    "query": { 
    "nested": { 
     "path": "paragraphs", 
     "query": { 
     "query_string": { 
      "default_field": "paragraphs.paragraph.content", 
      "query": "Firefighters AND apparently" 
     } 
     } 
    } 
    } 
} 

相同對尋找這兩個詞,所以你沒有得到結果。你需要像這樣分別查詢它們

{ 
    "query": { 
    "bool": { 
     "must": [ 
     { 
      "nested": { 
      "path": "paragraphs", 
      "query": { 
       "match": { 
       "paragraphs.paragraph.content": "firefighters" 
       } 
      } 
      } 
     }, 
     { 
      "nested": { 
      "path": "paragraphs", 
      "query": { 
       "match": { 
       "paragraphs.paragraph.content": "apparently" 
       } 
      } 
      } 
     } 
     ] 
    } 
    } 
} 

這會給你正確的結果。

作爲一個方面的說明,我不認爲你需要object datatype內段。以下也可以正常工作

"paragraphs": { 
     "type": "nested", 
     "properties": { 
      "content": { 
       "type": "string" 
      }, 
      "number": { 
       "type": "integer" 
      } 
     } 
    } 

希望這有助於!

+0

謝謝,我通過頁面去了,我需要添加\t \t \t \t \t「include_in_parent」:真要得到它的工作 – user568327

+0

高興能幫上忙,沒去「include_in_parent」,因爲它可能會增加索引的大小大大,但它肯定會奏效 – ChintanShah25

相關問題