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.
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
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
classSKUIndex(indexes.SearchIndex, indexes.Indexable):"""SKU索引数据模型类"""
text = indexes.CharField(document=True, use_template=True)defget_model(self):"""返回建立索引的模型类"""return SKU
defindex_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