【问题标题】:How to use search with Elasticsearch如何在 Elasticsearch 中使用搜索
【发布时间】:2026-01-03 04:40:01
【问题描述】:

我使用 python 2.7.11 和 djnago 1.10.2。我创建了产品模型并在我的数据库中保存了 1000 个产品。(postgrelsql)实际上,我使用了 Django elasticsearch,但它不起作用。它的搜索仅基于产品名称,如果搜索类别、颜色等,我需要。然后显示相关产品。我尝试了示例。

from haystack import indexes
from product.models import Product

class ProductIndex(indexes.SearchIndex, indexes.Indexable):
    text = indexes.CharField(document=True, use_template=True)
    product_name = indexes.CharField(model_attr='product_name')
    product_colour = indexes.CharField(model_attr='product_colour')

    def get_model(self):
        return Product

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

我创建了 ProductColour 模型并在产品模型中使用了 product_colour 外键。如果我搜索 product_colour 然后显示所有相关数据。

遵循一些步骤:-

  • 安装 django-haystack。
  • INSTALLED_APPS settings.py 文件中添加了 haystack。
  • 修改settings.py文件。

    HAYSTACK_CONNECTIONS = {
        'default': {
            'ENGINE': 'haystack.backends.simple_backend.SimpleEngine',
        },
    }
    
  • 在 urls.py 中添加了 url。

    urlpatterns = patterns('',
        url(r'^/search/?$', MySearchView.as_view(), name='search_view'),
    )
    
  • 产品型号。

    class Product(models.Model):
        product_name = models.CharField(max_length=100)
        product_description = models.TextField(default=None, blank=True, null=True)
        product_colour = models.ManyToManyField(ProductColour, blank=True, default=None)
        .......
        .......
        .......
    
  • 搜索.html。

    <form method="get" action=".">
        <table>
            {{ form.as_table }}
            <tr>
                <td>&nbsp;</td>
                <td>
                    <input type="submit" value="Search">
                </td>
            </tr>
        </table>
    </form>
    

【问题讨论】:

  • 请同时发布您的搜索索引模板和产品模型的相关部分。
  • @trixn 请检查一下先生,新的更新。

标签: python django django-haystack


【解决方案1】:

我使用了 Django elasticsearch,但它不起作用。

根据您的 haystack 设置,您没有使用 Elasticsearch。您使用的是SimpleEngine,它根本不是真正的搜索引擎。要使用 Elasticsearch,您的设置必须包含以下内容:

HAYSTACK_CONNECTIONS = {
    'default': {
        'ENGINE': 'haystack.backends.elasticsearch_backend.ElasticsearchSearchEngine',
        'URL': 'http://127.0.0.1:9200/',
        'INDEX_NAME': 'haystack',
    },
}

请注意,haystack 本身并不是搜索引擎。它只是一个从 django 应用程序内部使用多个搜索引擎的工具。你已经installed elasticsearch了吗?

我猜目前它不起作用,因为SimpleEngine 无法正确处理您的Many2ManyField

在您的 ProductIndex 中,您将 product_colour 定义为 CharField。但是您从 ProductColour 模型中引用了整个相关模型实例。使用MultiValueField 来执行此操作:

from haystack import indexes
from product.models import Product

class ProductIndex(indexes.SearchIndex, indexes.Indexable):
    text = indexes.CharField(document=True, use_template=True)
    product_name = indexes.CharField(model_attr='product_name')

    # use a MultiValueField for your m2m relation
    product_colours = indexes.MultiValueField()

    # define this method that returns all the values for your product_colours index field
    # it must be called "prepare_{your field name}"
    def prepare_product_colours(self, object):
        return [colour.name for color in object.product_colour.all()]

然后您将需要一个用于搜索索引的模板,其中将生成text 字段的内容as described in the haystack documentation

【讨论】: