gpt4 book ai didi

elasticsearch - 在Elasticsearch中组合过滤器和查询

转载 作者:行者123 更新时间:2023-12-03 00:10:09 25 4
gpt4 key购买 nike

我正在尝试使用elasticsearch 5.1.1进行搜索,以在任何地方查找“construction”一词,并在“phase”字段中使用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,现在该版本的filtered query已删除,您应该使用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”,它也应该起作用,因为如果您在相位字段上使用默认 token 解析器,则它将被标记为[phase1,phase2],但是如果您将提供“Phase 1 / Phase 2”,则此后将不再有效它将被标记为[phase,1,2]

更新后。
问题是您编写了阶段1,阶段2,但实际上您拥有阶段2,这意味着ES会将阶段2标记为[阶段2],并且您尝试搜索“阶段2”,这意味着没有匹配项。您可以选择将相位字段设为 as keyword,然后可以搜索“Phase 1”,“Phase 2”,但是如果您拥有“Phase 1 / Phase 2”,则ES会希望您搜索完全相同的词,但是可以将该字段拆分为一个数组,当您建立索引时,您将发送[“Phase 1”,“Phase 2”]

关于elasticsearch - 在Elasticsearch中组合过滤器和查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41539713/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com