Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use ElasticSearch in Django

2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces how to use ElasticSearch in Django related knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe you will have a harvest after reading this article on how to use ElasticSearch in Django, let's take a look at it.

What is Elasticsearch?

Elasticsearch is a search engine based on Lucene library. It provides a distributed, multi-tenant full-text search engine with HTTP Web interface and modeless JSON documents.

Elasticsearch is developed in Java.

What is the purpose of Elasticsearch?

Elasticsearch allows us to store, search and analyze large amounts of data quickly and in near real time, and give responses in milliseconds. The reason for the fast search response is that it can search the index directly instead of searching the text directly.

Some basic concepts of Elasticsearch-

Index-A collection of different types of documents and document properties. For example, a document set can contain data for a social networking application.

Type / Mapping-A collection of documents that share a set of common fields that exist in the same index. For example, the index contains data from social networking applications; for user profile data, there can be one specific type, another type for messaging data, and another type for annotation data.

Document-A collection of fields defined in JSON format in a specific way. Each document belongs to a type and is located in the index. Each document is associated with a unique identifier called UID.

Field-the Elasticsearch field can contain multiple values of the same type (essentially a list). In SQL, on the other hand, a column can contain exactly one value of the type.

Using Elasticsearch in Django

Install and configure, install Django Elasticsearch DSL:

$pip install django-elasticsearch-dsl

Then add django_elasticsearch_dsl to INSTALLED_APPS

ELASTICSEARCH_DSL must be defined in the django settings.

For example:

ELASTICSEARCH_DSL= {'default': {' hosts': 'localhost:9200'},}

Declare the data to be indexed, then create a model:

"`python

Models.pyclass Category (models.Model): name = models.CharField (max_length=30) desc = models.CharField (max_length=100, blank=True) def str (self): return'% s'% (self.name)

To use this model with Elasticsearch, create a subclass of django_elasticsearch_dsl.Document, create an Index class in the Document class to define our Elasticsearch index, name, settings, etc., and finally register the class with the Registry.register_document decorator. It needs to define the Document class in the documents.py in the application directory.

Documents.pyfrom django_elasticsearch_dsl import Documentfrom django_elasticsearch_dsl.registries import registryfrom .models import Category@registry.register_documentclass CategoryDocument (Document): class Index:name = 'category'settings = {' number_of_shards': 1, numbered numbers of identical replicas: 0} class Django:model = Categoryfields = ['name','desc',]

Fill:

To create and populate Elasticsearch indexes and maps, use the search_index command:

Pythonmanage.pysearch _ index-rebuildpythonmanage.pysearch

For more help, use the command: python manage.py search_index-help

Now, when doing the following:

Category = Category (name= "Computer and Accessories", desc= "abc desc") category.save ()

The object will also be saved in Elasticsearch (using a signal handler).

Search for:

To get an elasticsearch-dsl-py search instance, use:

S = CategoryDocument.search () .filter ("term", name= "computer")

Or

S = CategoryDocument.search () .query ("match", description= "abc") for hit in s:print ("Category name: {}, description {}" .format (hit.name, hit.desc))

To convert the elastic search results into a real set of Django queries, note that it takes a SQL request to retrieve the model instance with the ID returned by the Elasticsearch query.

S = CategoryDocument.search (). Filter ("term", name= "computer") [: 30] qs = s.to_queryset () for cat in qs:print (cat.name) this article on "how to use ElasticSearch in Django" ends here, thank you for reading! I believe you all have a certain understanding of "how to use ElasticSearch in Django". If you want to learn more, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report