如何将elasticsearch与Djang集成

2024-06-26 14:19:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我不熟悉elasticsearch。我想将我的MySQL Data存储到elasticsearch中,以供我的Django App。但我不知道从哪里开始。我看过Haystack教程,并将数据编入elasticsearch中,但如何查询这些数据?在

在模型.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)

西奇_索引.py在

^{pr2}$

然后我使用:python manage.py rebuild_index将数据索引到elasticsearch中

当我试着:

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)...'
]

想用我的网站作为我的后端搜索。所以我需要根据productDesc,MRP,price等执行不同类型的查询

我该怎么做?在


Tags: fromimageimporttrueurladminmodelsproduct
1条回答
网友
1楼 · 发布于 2024-06-26 14:19:22

由于SearchQuerySet().all()正在返回结果,看来您已经完成了大部分工作。现在你只需要添加一个过滤器来获得你想要的结果。在

试试这个:

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

看看你能不能得到这个标题的结果。在

有关详细信息,请查看文档:searchqueryset_api。在

相关问题 更多 >