0

我在嘗試根據關鍵字搜索產品。我haystack指標如下:如何從django-haystack和Elasticsearch中排除無關的搜索結果?

class ProductIndex(indexes.SearchIndex, indexes.Indexable): 
    name= indexes.CharField(model_attr='name') 
    text = indexes.CharField(document=True) # contains keywords associated with the product 

在這種情況下,該領域「文本」包括一組與產品相關的關鍵詞。舉例來說,這裏是一個示例產品指標:

name: "Tide Detergent" 
text: "laundry household shopping cleaning supplies" 

當我搜索laundryTide Detergent出現在搜索,但這樣做其他不相關的結果,如必須在textlawnlaugh產品。所以它看起來像elasticsearch不僅搜索laundry,但也是該詞的變體。

這裏是我的搜索查詢是什麼樣子:

qs = SearchQuerySet().models(Product).filter(content__exact='laundry') 

我的問題是:我怎麼能強迫haystackelasticsearch嚴格尋找我輸入的關鍵詞,而忽略他們的變化?換句話說,如何確保乾草堆僅搜索laundry並排除任何其他條款?

回答

0

我找到了答案。我使用原始elasticsearch查詢,而不是直接通過haystack。在那個查詢中,我使用了一個constant_score查詢,它將搜索確切的條款,而不是模糊條款。

{ 
    "query":{ 
     "query":{ 
      "constant_score":{ 
       "filter":{ 
        "bool":{ 
         "must":[ 
          {"bool":{ 
            "must":[ 
             { 
              "term": { 
               "text":"laundry" 
              } 
             } 
            ] 
           } 
          } 
         ] 
        } 
       } 
      } 
     } 
    } 
} 

以下是關於constant_score查詢更多信息:https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-constant-score-query.html

相關問題