Complete e-commerce project--(6) Commodity module (6): Product search Elasticsearch

Elasticsearch

Introduction

  • Elasticsearch is implemented in Java, an open source search engine that implements full-text search.
  • It can quickly store, search and analyze massive amounts of data. Wikipedia, Stack Overflow, Github, etc. all adopt it
  • The bottom layer of Elasticsearch is the open source library Lucene. However, you cannot use Lucene directly, you must write your own code to call its interface.
  • Elasticsearch does not support Chinese word segmentation and indexing. You need to expand elasticsearch-analysis-ik to implement Chinese word segmentation processing.

Installation: docker

# 从仓库拉取镜像
$ sudo docker image pull delron/elasticsearch-ik:2.4.6-1.0

配置 Elasticsearch-ik

  • Modify the 54th line of /home/python/elasticsearc-2.4.6/config/elasticsearch.yml.
    Change the ip address to the real ip address of the machine

Use docker to run Elasticsearch-ik

sudo docker run -dti --name=elasticsearch --network=host -v /home/python/elasticsearch-2.4.6/config:/usr/share/elasticsearch/config delron/elasticsearch-ik:2.4.6-1.0
  • After that, you can view the image through sudo docker image ls

Haystack

Introduction

  • Usage background: The bottom layer of Elasticsearch is the open source library Lucene . But you can't use Lucene directly, you have to write your own code to call its interface.
  • Haystack is a framework for docking search engines in Django, building a communication bridge between users and search engines
  • We can call Elasticsearch search engine by using Haystack in Django
  • Haystack can use different search backends (such as Elasticsearch, Whoosh, Solr, etc.) without modifying the code

installation

$ pip install django-haystack
$ pip install elasticsearch==2.4.1

Register application and routing in django

INSTALLED_APPS = [
    'haystack', # 全文检索
]

url(r'^search/', include('haystack.urls')),

Haystack placement

# Haystack
HAYSTACK_CONNECTIONS = {
    
    
    'default': {
    
    
        'ENGINE': 'haystack.backends.elasticsearch_backend.ElasticsearchSearchEngine',
        'URL': 'http://192.168.103.158:9200/', # Elasticsearch服务器ip地址,端口号固定为9200
        'INDEX_NAME': 'meiduo_mall', # Elasticsearch建立的索引库的名称
    },
}

# 当添加、修改、删除数据时,自动生成索引
HAYSTACK_SIGNAL_PROCESSOR = 'haystack.signals.RealtimeSignalProcessor'
  • HAYSTACK_SIGNAL_PROCESSOR configuration item : to ensure that after Django is running, when new data is generated, Haystack can still let Elasticsearch generate an index of new data in real time

Haystack builds data index

1. Create an index class

  • By creating an index class, you can specify which fields the search engine can index, that is, the keywords of which fields can be used to retrieve data.
  • In this project, SKU information is searched in full text, so a new search_indexes.py file is created in the goods application to store index classes.
from haystack import indexes

from .models import SKU


class SKUIndex(indexes.SearchIndex, indexes.Indexable):
    """SKU索引数据模型类"""
    text = indexes.CharField(document=True, use_template=True)

    def get_model(self):
        """返回建立索引的模型类"""
        return SKU

    def index_queryset(self, using=None):
        """返回要建立索引的数据查询集"""
        return self.get_model().objects.filter(is_launched=True)
  • Index SKUIndex description
    • The fields established in SKUIndex can be queried by the Elasticsearch search engine with the help of Haystack.
    • Among them, the text field is declared as document=True, and the field of the table name is the main field for keyword query.
    • The index value of the text field can be composed of multiple database model type fields. Specifically, which model type fields are composed of, we use use_template=True to indicate the subsequent use of the template

Create a text field index value template file

  • Create a template file used by the text field in the templates directory
  • Defined in the templates/search/indexes/goods/sku_text.txt file
{
    
    {
    
     object.id }}
{
    
    {
    
     object.name }}
{
    
    {
    
     object.caption }}
  • Template file description: When the keyword is passed through the text parameter name, you can
    • This template specifies the id, name, and caption of the SKU as the index value of the text field for keyword index query

3. Manually generate the initial index

$ python manage.py rebuild_index
  • Finally write the view:
    • Request address: /search/ (view address defined by yourself)
  • Then display the search results (such as pagination, rendering templates).

Guess you like

Origin blog.csdn.net/pythonstrat/article/details/108168610