带有 Elasticsearch 的 django-haystack:如何使模糊(单词相似性)搜索起作用?

时间:2021-08-01 12:42:05

标签: elasticsearch django-haystack

我在 AWS 上使用 Elasticsearch 实例 2.3 版将 \phpmyadmin\libraries\config.default.phpelasticsearch==2.4.1django-haystack==3.0 结合使用。

我正在尝试实现“您是说……?”使用相似性搜索。

我有这个模型:

Django==2.2

以下索引:

class EquipmentBrand(SafeDeleteModel):
    name = models.CharField(
        max_length=128,
        null=False,
        blank=False,
        unique=True,
    )

我是这样搜索的:

class EquipmentBrandIndex(SearchIndex, Indexable):
    text = fields.EdgeNgramField(document=True, model_attr="name")

    def index_queryset(self, using=None):
        return self.get_model().objects.all()

    def get_model(self):
        return EquipmentBrand

results = SearchQuerySet().models(EquipmentBrand).filter(content=AutoQuery(q)) 为“示例品牌”时,这些是我的实际结果:

name

我正在尝试使最后一个示例起作用,即如果单词相似,则查找该项目。

我的目标是在出现拼写错误时从数据库中推荐项目。

我缺少什么才能完成这项工作?

谢谢!

1 个答案:

答案 0 :(得分:0)

我认为您不想使用 EdgeNgramField。 “边缘”n-gram,来自Elasticsearch Docs

emits N-grams of each word where the start of the N-gram is anchored to the beginning of the word.

它用于自动完成。它只匹配作为目标前缀的字符串。因此,当目标文档包含“example”时,搜索工作将是“e”、“ex”、“exa”、“exam”、...

"Exmple" 不是这些字符串之一。尝试使用普通的 NgramField。

另外,请考虑升级。自 ES 2.4.1 以来已经修复和改进了很多