2013-07-09 73 views
2

我是elasticsearch的新手。我想將我的MySQL Data存儲到我的Django Appelasticsearch中。但我不知道從哪裏開始。我查看了Haystack教程,並將數據編入elasticsearch,但是如何查詢這些數據?如何將elasticsearch與Django集成

models.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) 

seach_indexes.py

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() 

然後我索引的數據到elasticsearch使用:python manage.py rebuild_index

,當我試圖:

from pprint import pprint 
from haystack.query import SearchQuerySet 


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

輸出結果爲:

[ 
    <SearchResult: eScraperInterfaceApp.scrapeddata (pk=u'742')>, 
    <SearchResult: eScraperInterfaceApp.scrapeddata (pk=u'747')>, 
    <SearchResult: eScraperInterfaceApp.scrapeddata (pk=u'754')>, 
    <SearchResult: eScraperInterfaceApp.scrapeddata (pk=u'759')>, 
    <SearchResult: eScraperInterfaceApp.scrapeddata (pk=u'761')>, 
    <SearchResult: eScraperInterfaceApp.scrapeddata (pk=u'766')>, 
    <SearchResult: eScraperInterfaceApp.scrapeddata (pk=u'773')>, 
    <SearchResult: eScraperInterfaceApp.scrapeddata (pk=u'778')>, 
    <SearchResult: eScraperInterfaceApp.scrapeddata (pk=u'780')>, 
    <SearchResult: eScraperInterfaceApp.scrapeddata (pk=u'785')>, 
    <SearchResult: eScraperInterfaceApp.scrapeddata (pk=u'792')>, 
    <SearchResult: eScraperInterfaceApp.scrapeddata (pk=u'797')>, 
    <SearchResult: eScraperInterfaceApp.scrapeddata (pk=u'800')>, 
    <SearchResult: eScraperInterfaceApp.scrapeddata (pk=u'805')>, 
    <SearchResult: eScraperInterfaceApp.scrapeddata (pk=u'812')>, 
    <SearchResult: eScraperInterfaceApp.scrapeddata (pk=u'817')>, 
    <SearchResult: eScraperInterfaceApp.scrapeddata (pk=u'824')>, 
    <SearchResult: eScraperInterfaceApp.scrapeddata (pk=u'829')>, 
    <SearchResult: eScraperInterfaceApp.scrapeddata (pk=u'831')>, 
    '...(remaining elements truncated)...' 
] 

我想使用elasticsearch作爲我的網站的後端。所以我需要根據productDesc,MRP,價格等進行不同類型的查詢。

我該怎麼做?

+1

是什麼做的草垛或別的東西的最佳方式.... –

+0

有人可以告訴我如何設置彈性搜索和Django –

+0

嘿@ user2217267,我目前看這自己,不知道你是否我已經解決了它,但從我所能看到的乾草堆是一個很好的方法...如果你仍然卡住,我會在我完成我的研究後回答這個問題:)... – toofarsideways

回答

0

由於SearchQuerySet().all()正在返回結果,因此您似乎很喜歡這種方式。現在你只需要添加一個過濾器來獲得你想要的結果。

試試這個:

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

,看看你得到那個標題的結果。

欲瞭解更多信息,請查閱文檔:searchqueryset_api