![](/img/trans.png)
[英]Elastic search apply boost only for certain documents based on their field value
[英]How to boost certain documents if the search query contains a certain term/text in elastic
如果搜索查询包含fruits
我想提升某个类别的产品?
{
"query": {
"bool": {
"should": [
{
"constant_score": {
"boost": 2,
"filter": {
"term": { "categories": "3" }
}
}
}
]
}
}
}
我有上面的查询,它为类别3
项目提供恒定分数,我想仅当搜索词中存在特定文本(例如fruits
)时才应用此恒定分数/提升/增加相关性。
示例弹性搜索文档
{
"id" : 1231,
"name" : {
"ar" : "Arabic fruit name",
"en" : "english fruit name"
}
"categories" : [3,1,3] // category ids because the same product is shown in multiple categories
}
我如何实现这一目标? 我使用弹性搜索 7.2
原答案:
{
"query": {
"bool": {
"should": [
{
"constant_score": {
"boost": 2,
"filter": {
"bool": {
"filter": [
{
"term": {
"categories": "3"
}
}
],
"should": [
{
"match": {
"name.ar": "fruit"
}
},
{
"match": {
"name.en": "fruit"
}
}
],
"minimum_should_match": 1
}
}
}
}
]
}
}
}
如果我正确理解你在找什么。 顺便说一句,如果您想完全匹配“水果名称”而不是“水果”或“名称”,我建议使用“match_phrase”而不是“match”
更新:(基于评论)在这种情况下,我建议按以下方式重新组织您的架构:
"item": {
"properties": {
"name": {
"type": ["string"]
},
"language": {
"type": ["string"]
}
}
}
所以你的样本会变成:
{
"id" : 1231,
"item" : [
{"name": "Arabic fruit name", "language": "ar"}
{"name": "english fruit name", "language": "en"}
],
"categories" : [3,1,3]
}
然后你可以匹配“item.name”
为什么? 因为 ElasticSearch 索引的方式(至少,默认情况下)是扁平化你的数组,所以在内部它看起来像 ["Arabic Fruit name", "english Fruit name"]
对于您的原始示例,架构中创建了两个不同的字段(name.ar 和 name.en),如果您需要扩展,这实际上不是一个很好的设计
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.