[英]Combining filter and query whitin nested document in elasticsearch
[英]combining a filter and a query in elasticsearch
我正在嘗試使用elasticsearch 5.1.1進行搜索,以便在任何地方查找“構造”一詞,並在“階段”字段上使用phase1或phase2過濾結果。 我嘗試過許多都失敗的事情。 這是一個明智的一個:
curl -XPOST "http://localhost:9200/books/_search?pretty=true" -d '{
"query": {
"filtered": {"filter": {"terms": {"phase": ["phase1", "phase2"]}}, "query": "construction"}
}
}'
錯誤:
parsing_exception
no [query] registered for [filtered]
另請注意,相位字段可能具有類似“Phase1 / Phase2”的值,因此我需要查看它是否包含Phase1或Phase2。 我怎樣才能進行這樣的搜索?
更新
@volodymyr,以下作品:
curl -XPOST "http://localhost:9200/books/_search?pretty=true" -d '{
"query":{
"bool":{
"must":{
"multi_match":{
"query":"construction",
"fields":["title"]
}
}
}
}
}'
結果像
{
"_index" : "books",
"_type" : "gov",
"_id" : "2296112",
"_score" : 5.742423,
"_source" : {
"description" : "",
"countries" : [
"United States"
],
"phase" : "Phase 2",
"completion_date" : "2018-01-01",
"references" : [ ],
"keywords" : [ ],
"id" : "2296112",
"title" : "Mainland construction"
}
然后我嘗試添加階段,它沒有失敗,但沒有返回結果,所以這仍然是不好的:
curl -XPOST "http://localhost:9200/books/_search?pretty=true" -d '{
"query":{
"bool":{
"must":{
"multi_match":{
"query":"construction",
"fields":["title"]
}
},
"filter":{
"terms":{
"phase":["phase1","phase2"]
}
}
}
}
}'
還嘗試過:
curl -XPOST "http://localhost:9200/books/_search?pretty=true" -d '
{
"query": {
"bool" : {
"must" : {
"term" : { "title" : "construction" }
},
"should" : [
{ "term" : { "phase" : "Phase 1" } },
{ "term" : { "phase" : "Phase 2" } }
]
}
}
}'
它不返回任何結果。
我的錯,我錯過了你使用5.1,在那個版本過濾查詢現在被刪除,你應該使用bool
如果您想搜索構造,那么請使用multi_match
curl -XPOST "http://localhost:9200/books/_search?pretty=true" -d '{
"query":{
"bool":{
"must":{
"multi_match":{
"query":"construction",
"fields":[
"field1",
"OTHER_FIELD_IF_YOU_NEED"
]
}
},
"filter":{
"terms":{
"phase":[
"phase1",
"phase2"
]
}
}
}
}
}'
對於值“Phase1 / Phase2”,它也應該工作,因為如果你在相位字段上使用默認令牌器,它將被標記化為[phase1,phase2],但如果你將提供“階段1 /階段2”,那么從那時起它將不會它會被標記為[階段,1,2]
更新后。 問題是你寫了phase1,phase2但實際上你有階段2,這意味着ES會將階段2標記為[階段2],你試圖搜索“階段2”,這意味着沒有匹配。 您可以選擇將相位字段設為關鍵字 ,然后您可以搜索“階段1”,“階段2”但是如果您有“階段1 /階段2”,那么ES將期望您搜索完全相同的但是您可以將該字段拆分為數組,當您編制索引時,您將發送[“階段1”,“階段2”]
過濾器需要在bool范圍之外:
curl -XPOST "http://localhost:9200/books/_search?pretty=true" -d '
{ "query":
{ "bool":
{ "must":
{ "multi_match":
{ "query":"construction", "fields":
[ "field1",
"OTHER_FIELD_IF_YOU_NEED"
]
}
}
}
},
"filter":
{ "terms":
{ "phase":
[ "phase1", "phase2" ]
}
}
}'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.