簡體   English   中英

如果搜索查詢包含彈性中的某個術語/文本,如何提升某些文檔

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM