如何将elasticsearch与Django集成

时间:2013-07-09 13:38:29

标签: python elasticsearch django-haystack

我是elasticsearch的新手。我想将MySQL Data存储到我elasticsearch的{​​{1}}。但我不知道从哪里开始。我查看了Haystack教程,并将数据编入索引Django App,但如何查询该数据?

models.py

elasticsearch

seach_indexes.py

import json
from django.db import models
from django.contrib import admin
#------------------------------------------------------------------------------ 


class scrapedData (models.Model):
    """ This a model for scraped data collected by eScraper"""

    productMRP = models.FloatField()                                      # Product MRP
    image_urls = models.TextField()                                       # Images URL's for image pipeline for downloading
    productSite = models.URLField()                                       # Product web-site URL
    productDesc = models.TextField()                                      # Product Description
    image_paths = models.TextField()                                      # Product images path on the local machine
    productImage = models.TextField()                                     # Product image URL's
    productTitle = models.TextField()                                     # Product title
    productPrice = models.FloatField()                                    # Product discounted price
    hasVariants = models.BooleanField()                                   # Product variants like : colors or sizes, True is if product has variants, False otherwise
    productCategory = models.TextField()                                  # Product category
    availability = models.BooleanField()                                  # Product availability ,True if product is in stock, False otherwise
    productSubCategory = models.TextField()                               # Product sub-category
    currency = models.CharField(max_length=3)                             # Product price currency
    productURL = models.URLField(max_length=500)                          # Product page URL
    updatedAt = models.DateTimeField(auto_now=True)                       # Time at which product is updated
    createdAt = models.DateTimeField(auto_now_add=True)                   # Time at which product is created


class scrapedDataAdmin(admin.ModelAdmin):
    """scrapedData admin class"""

    list_display = ('productTitle','productSite','updatedAt','createdAt',
                    'product_URL','product_Image','productMRP','productPrice','currency',
                    'productDesc','productCategory','availability',
                    'hasVariants','productSubCategory','image_paths','image_urls'
                    )

    ordering = ('productSite',)


admin.site.register(scrapedData,scrapedDataAdmin)

然后我使用:from haystack import indexes from eScraperInterfaceApp.models import scrapedData #------------------------------------------------------------------------------ class scrapedDataIndex(indexes.SearchIndex, indexes.Indexable): """ This is a index class for scrapedData model """ productMRP = indexes.CharField() productDesc = indexes.CharField() productTitle = indexes.CharField() productPrice = indexes.CharField() productCategory = indexes.CharField() productSubCategory = indexes.CharField() text = indexes.CharField(document=True, use_template=False) # This field is the primary field for searching within def get_model(self): """ This is a haystack method to get model name for the APP """ return scrapedData def index_queryset(self, using=None): """Used when the entire index for model is updated.""" return self.get_model().objects.filter()

将数据编入索引

当我尝试时:

python manage.py rebuild_index

输出是:

from pprint import pprint 
from haystack.query import SearchQuerySet


all_results = SearchQuerySet().all()
pprint(all_results)

我想将elasticsearch用作我网站的后端。所以我需要根据productDesc,MRP,价格等执行不同类型的查询。

我该怎么做?

1 个答案:

答案 0 :(得分:0)

由于SearchQuerySet().all()正在返回结果,看起来你大部分都在那里。现在,您只需添加一个过滤器即可获得所需的结果。

试试这个:

SearchQuerySet().filter(title="AN EXISTING TITLE")

看看你是否得到了带有该标题的结果。

有关详细信息,请查看文档:{​​{3}}。