我是Elastic Search的新手,我想问一下如何根据某些条件提升查询的某些结果。具体来说,Elastic Search中的项目具有以下格式:
"name":"item1",
"desc":"desc1",
"brand":"brandname"
我想使某些商品(带有"brand" = "nike"
的商品)的得分低于其他品牌。有没有办法用Java进行 Elasticsearch 的QueryBuilders类来做到这一点?
最佳答案
Boosting查询也许可以解决问题。另外,您也可以使用function_score queries以获得更大的灵活性。
提升查询的示例。
(1)索引少量文件
curl -XPOST localhost:9200/idx/doc/1 -d '{"content": "foo"}' --header "Content-type:application/json"
curl -XPOST localhost:9200/idx/doc/2 -d '{"content": "bar"}' --header "Content-type:application/json"
curl -XPOST localhost:9200/idx/doc/3 -d '{"content": "foo fred bar"}' --header "Content-type:application/json"
(2)查询bar
为负数以降低bar
的分数 curl -XPOST localhost:9200/idx/_search?pretty -d '{
"query": {
"boosting": {
"positive": {
"match": {
"content": "foo bar fred"
}
},
"negative": {
"match": {
"content": "bar"
}
} , "negative_boost": 0.001
}
}
}' --header "Content-type:application/json"
{
"took" : 59,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 3,
"relation" : "eq"
},
"max_score" : 0.5619609,
"hits" : [
{
"_index" : "idx",
"_type" : "doc",
"_id" : "1",
"_score" : 0.5619609,
"_source" : {
"content" : "foo"
}
},
{
"_index" : "idx",
"_type" : "doc",
"_id" : "3",
"_score" : 0.0014472058,
"_source" : {
"content" : "foo fred bar"
}
},
{
"_index" : "idx",
"_type" : "doc",
"_id" : "2",
"_score" : 5.619609E-4,
"_source" : {
"content" : "bar"
}
}
]
}
}
关于java - Java中的Elasticsearch条件评分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62882613/