Use elasticsearch index (with haystack)

1, image # pull from storage
$ Delron pull the sudo Docker Image / elasticsearch-IK: 2.4.6-1.0
2, download elasticsearc-2.4.6 directory are copied to the home directory.
Modify /home/python/elasticsearc-2.4.6/config/elasticsearch.yml line 54.
Real change ip address ip address for this machine.
3, the use of docker run elasticsearch
the sudo docker RUN -dti --name = elasticsearch --network = -v /home/python/elasticsearch-2.4.6/config:/usr/share/elasticsearch/config Delron Host / elasticsearch-IK: 2.4.6-1.0
. 4,
$ PIP-Django the install of haystack
$ PIP elasticsearch the install == 2.4.1

5.Haystack registration and routing applications (settings profile)
the INSTALLED_APPS = [
# text retrieval
'of haystack',
]
# the Haystack register
url (R & lt 'Search ^ /', the include ( 'haystack.urls')),

. 6, Haystack arranged in the configuration file to the search engine rear
# Haystack
= {HAYSTACK_CONNECTIONS
'default': {
'ENGINE': 'haystack.backends.elasticsearch_backend.ElasticsearchSearchEngine',
'the URL': 'http://172.16.238.128:9200/', # elasticsearch server ip address, port number 9200 is fixed to
'INDEX_NAME': 'meiduo_mall', the name of the index library # elasticsearch established
}
}
# when you add, modify, delete data, automatically generate an index
HAYSTACK_SIGNAL_PROCESSOR = 'haystack.signals.RealtimeSignalProcessor

7, Haystack establish a data index
1. create index class

by class to create an index to indicate that search engines index on which field, that is, can retrieve data fields by which the keyword.
SKU information on this project full-text search, so the new search_indexes.py file in goods applications, the class used to store the index.

from of haystack Import Indexes 

from .models Import SKUs 

class SKUIndex (indexes.SearchIndex, indexes.Indexable):
     "" " SKUs index data model class " "" 
    text = indexes.CharField (Document = True, use_template = True) 

    DEF get_model (Self ):
         "" " returns the model class indexing " "" 
        return SKU 

    DEF index_queryset (Self, a using = None):
         "" " return to set up a data query the index " "" 
        return self.get_model () objects.filter. (is_launched = True)


Index categories SKUIndex Description:
In the field SKUIndex established, can be queried by means of Haystack Elasticsearch search engine.
Text field in which we declared as document = True, the table name in this field is mainly field keyword query.
index value of the text field may consist of a plurality of model classes database fields, particularly fields which consists of model classes, we use_template = True represented by the subsequent template specified.

2. Create a text field index value of the template file
to create the template file text field for use in templates directory
specifically defined in the templates / search / indexes / goods / sku_text.txt file
{object.id} {}
{} {} object.name
{{object.caption}}
template file Description: when the keyword text parameter name is passed by the
id specified in the template of SKU, name, caption text as index values to the fields keyword index query.

8, manually generated index
Python manage.py rebuild_index

. 9, the test index
ready to test the form.
Request method: GET
Request Address: / search /
request parameters: Q
add the following code goods.views.py file:

class MySearchView(SearchView):
    '''重写SearchView类'''
    def create_response(self):
        page = self.request.GET.get('page')
        # 获取搜索结果
        context = self.get_context()
        data_list = []
        for sku in context['page'].object_list:
            data_list.append({
                'id':sku.object.id,
                'name':sku.object.name,
                'price':sku.object.price,
                'default_image_url':sku.object.default_image_url,
                'searchkey':context.get('query'),
                'page_size':context['page'].paginator.num_pages,
                'count':context['page'].paginator.count
            })
        # 拼接参数, 返回
        return http.JsonResponse(data_list, safe=False)


Routing configuration, no as_view ()
URL, (R & lt () '^ Search / $', views.MySearchView)

# dev.py can add the following code, the data for determining the number of pieces per page:
HAYSTACK_SEARCH_RESULTS_PER_PAGE. 5 =

Guess you like

Origin www.cnblogs.com/wjun0/p/11817194.html