我创建了一个带有字段title_auto
的索引:
class GameIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.CharField(document=True, model_attr='title')
title = indexes.CharField(model_attr='title')
title_auto = indexes.NgramField(model_attr='title')
弹性搜索设置如下所示:
ELASTICSEARCH_INDEX_SETTINGS = {
'settings': {
"analysis": {
"analyzer": {
"ngram_analyzer": {
"type": "custom",
"tokenizer": "lowercase",
"filter": ["haystack_ngram"],
"token_chars": ["letter", "digit"]
},
"edgengram_analyzer": {
"type": "custom",
"tokenizer": "lowercase",
"filter": ["haystack_edgengram"]
}
},
"tokenizer": {
"haystack_ngram_tokenizer": {
"type": "nGram",
"min_gram": 1,
"max_gram": 15,
},
"haystack_edgengram_tokenizer": {
"type": "edgeNGram",
"min_gram": 1,
"max_gram": 15,
"side": "front"
}
},
"filter": {
"haystack_ngram": {
"type": "nGram",
"min_gram": 1,
"max_gram": 15
},
"haystack_edgengram": {
"type": "edgeNGram",
"min_gram": 1,
"max_gram": 15
}
}
}
}
}
我尝试进行自动填充搜索,但是有效,但会返回太多不相关的结果:
qs = SearchQuerySet().models(Game).autocomplete(title_auto=search_phrase)
OR
qs = SearchQuerySet().models(Game).filter(title_auto=search_phrase)
它们都产生相同的输出。
如果search_phrase是"垄断",首先结果包含" Monopoly"然而,在他们的头衔中,因为只有2个相关的项目,它返回51.其他的与" Monopoly"一点都不。
所以我的问题是 - 如何更改结果的相关性?
答案 0 :(得分:4)
由于我还没有看到您的完整映射,因此很难确定,但我怀疑问题在于分析器(其中之一)正用于索引和搜索。因此,当您索引文档时,会创建许多ngram术语并将其编入索引。如果您搜索并且您的搜索文本也以相同的方式进行分析,则会生成大量搜索字词。由于你的最小ngram是一个字母,几乎任何查询都会匹配很多文档。
我们写了一篇关于使用ngrams进行自动填充的博客文章,您可能会发现这些内容很有帮助,http://blog.qbox.io/multi-field-partial-word-autocomplete-in-elasticsearch-using-ngrams。但是我会给你一个更简单的例子来说明我的意思。我对大海捞针不太熟悉,所以我可能无法帮助你,但我可以用Elasticsearch中的ngrams来解释这个问题。
首先,我设置一个使用ngram分析器进行索引和搜索的索引:
PUT /test_index
{
"settings": {
"number_of_shards": 1,
"analysis": {
"filter": {
"nGram_filter": {
"type": "nGram",
"min_gram": 1,
"max_gram": 15,
"token_chars": [
"letter",
"digit",
"punctuation",
"symbol"
]
}
},
"analyzer": {
"nGram_analyzer": {
"type": "custom",
"tokenizer": "whitespace",
"filter": [
"lowercase",
"asciifolding",
"nGram_filter"
]
}
}
}
},
"mappings": {
"doc": {
"properties": {
"title": {
"type": "string",
"analyzer": "nGram_analyzer"
}
}
}
}
}
并添加一些文档:
PUT /test_index/_bulk
{"index":{"_index":"test_index","_type":"doc","_id":1}}
{"title":"monopoly"}
{"index":{"_index":"test_index","_type":"doc","_id":2}}
{"title":"oligopoly"}
{"index":{"_index":"test_index","_type":"doc","_id":3}}
{"title":"plutocracy"}
{"index":{"_index":"test_index","_type":"doc","_id":4}}
{"title":"theocracy"}
{"index":{"_index":"test_index","_type":"doc","_id":5}}
{"title":"democracy"}
并对match
进行简单的"poly"
搜索:
POST /test_index/_search
{
"query": {
"match": {
"title": "poly"
}
}
}
它返回所有五个文件:
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": {
"total": 5,
"max_score": 4.729521,
"hits": [
{
"_index": "test_index",
"_type": "doc",
"_id": "2",
"_score": 4.729521,
"_source": {
"title": "oligopoly"
}
},
{
"_index": "test_index",
"_type": "doc",
"_id": "1",
"_score": 4.3608603,
"_source": {
"title": "monopoly"
}
},
{
"_index": "test_index",
"_type": "doc",
"_id": "3",
"_score": 1.0197333,
"_source": {
"title": "plutocracy"
}
},
{
"_index": "test_index",
"_type": "doc",
"_id": "4",
"_score": 0.31496215,
"_source": {
"title": "theocracy"
}
},
{
"_index": "test_index",
"_type": "doc",
"_id": "5",
"_score": 0.31496215,
"_source": {
"title": "democracy"
}
}
]
}
}
这是因为搜索字词"poly"
被标记为"p"
,"o"
,"l"
和"y"
这两个词,因为"title"
每个文档中的1}}字段被标记为单个字母的术语,匹配每个文档。
如果我们使用此映射重建索引(相同的分析器和文档):
"mappings": {
"doc": {
"properties": {
"title": {
"type": "string",
"index_analyzer": "nGram_analyzer",
"search_analyzer": "standard"
}
}
}
}
查询将返回我们期望的内容:
POST /test_index/_search
{
"query": {
"match": {
"title": "poly"
}
}
}
...
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 1.5108256,
"hits": [
{
"_index": "test_index",
"_type": "doc",
"_id": "1",
"_score": 1.5108256,
"_source": {
"title": "monopoly"
}
},
{
"_index": "test_index",
"_type": "doc",
"_id": "2",
"_score": 1.5108256,
"_source": {
"title": "oligopoly"
}
}
]
}
}
Edge ngrams的工作方式类似,只是只使用从单词开头开始的术语。
以下是我在此示例中使用的代码:
http://sense.qbox.io/gist/b24cbc531b483650c085a42963a49d6a23fa5579
答案 1 :(得分:1)
不幸的是,此时似乎没有办法(除了实现自定义后端)分别通过Django-Haystack配置搜索分析器和索引分析器。 如果Django-Haystack自动完成返回的结果太宽,您可以利用每个搜索结果提供的分数值来优化输出。
if search_query != "":
# Use autocomplete query or filter
# with results_filtered being a SearchQuerySet()
results_filtered = results_filtered.filter(text=search_query)
#Remove objects with a low score
for result in results_filtered:
if result.score < SEARCH_SCORE_THRESHOLD:
results_filtered = results_filtered.exclude(id=result.id)
在不必定义自己的后端和方案构建的情况下,它对我来说非常合理。