2013-07-22 89 views
3

我正在用Elasticsearch父/子用fun-with-elasticsearch-s-children-and-nested-documents/的一些簡單示例進行實驗。我能夠通過在博客中運行查詢來查詢子元素ElasticSearch has_parent查詢

curl -XPOST localhost:9200/authors/bare_author/_search -d '{ 

但是,我無法調整has_parent查詢的示例。有人可以指出我做錯了什麼,因爲我一直得到0結果。

這是我試過

#Returns 0 hits  
curl -XPOST localhost:9200/authors/book/_search -d '{ 
    "query": { 
    "has_parent": { 
     "type": "bare_author", 
     "query" : { 
     "filtered": { 
      "query": { "match_all": {}}, 
      "filter" : {"term": { "name": "Alastair Reynolds"}}    
      } 
     } 
     } 
    } 
    }' 


#did not work either 
curl -XPOST localhost:9200/authors/book/_search -d '{ 
"query": { 
    "has_parent" : { 
     "type" : "bare_author", 
     "query" : { 
     "term" : { 
       "name" : "Alastair Reynolds" 
      } 
     } 
    } 
} 
}' 

這適用於比賽,但它只是匹配的第一個名字

#works but matches just first name 
curl -XPOST localhost:9200/authors/book/_search -d '{ 
"query": { 
    "has_parent" : { 
     "type" : "bare_author", 
     "query" : { 
     "match" : {"name": "Alastair"} 
     } 
    } 
    } 
}' 
+0

您應該首先嚐試在bare_author類型上直接運行「term」查詢或過濾器,以查看它是否返回一些匹配項。你會看到問題來自'has_parent'還是'term'查詢。 – mguillermin

回答

3

我想你使用的是默認的映射,從而分析使用的名稱字段standard analyzer。另一方面,詞條查詢和詞條過濾器不支持文本分析,因此您在索引中搜索標記Alastair Reynolds,而將alastairreynolds標記爲兩個不同的標記和小寫。

匹配查詢返回結果,因爲它已被分析,因此在lowercased下方,它找到匹配項。您可以更改term query並將其設置爲match query,即使使用多個詞語,它也會找到匹配項,因爲在這種情況下,它將在空格上標記,並會根據所提供的不同條件生成布爾值或dismax查詢。

+0

你是對的。爲了解決這個問題,我刪除了索引並用not_analyzed重新創建。 – BSingh