2017-02-01 85 views
0

我使用python 2.7.11和djnago 1.10.2。我创建了产品模型并在我的数据库中保存了1000个产品。(postgrelsql)实际上,我使用了Django elasticsearch,但它不起作用。其搜索只基于产品名称,我需要如果搜索类别,颜色等,然后​​显示相关的产品。我试了一下例子。如何使用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() 

我在Product moedls中创建了ProductColour模型并使用了product_colour foreignkey。如果我搜索product_colour,然后显示所有相关的数据。

以下一些步骤: -

  • 安装Django,干草堆。
  • INSTALLED_APPS settings.py文件中添加干草堆。
  • 修改settings.py文件。

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

    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) 
        ....... 
        ....... 
        ....... 
    
  • search.html。

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

请张贴您的搜索索引模板和您的产品型号的相关部分。 – trixn

+0

@trixn plz检查它,先生,新的更新。 –

回答

0

我用Django的elasticsearch但它不工作。

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

HAYSTACK_CONNECTIONS = { 
    'default': { 
     'ENGINE': 'haystack.backends.elasticsearch_backend.ElasticsearchSearchEngine', 
     'URL': 'http://127.0.0.1:9200/', 
     'INDEX_NAME': '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您的搜索索引。

相关问题