在Django中使用ElasticSearch
什么是Elasticsearch?
Elasticsearch
是基于Lucene
庫的搜索引擎。它提供了具有HTTP Web界面和無模式JSON文檔的分布式,多租戶功能的全文本搜索引擎。
Elasticsearch是用Java開發(fā)的。
Elasticsearch的用途是什么?
Elasticsearch
可以使我們快速,近乎實時地存儲,搜索和分析大量數(shù)據(jù),并在幾毫秒內(nèi)給出答復。之所以能夠獲得快速的搜索響應,是因為它可以直接搜索索引,而不是直接搜索文本。
Elasticsearch-一些基本概念
索引—不同類型的文檔和文檔屬性的集合。例如,文檔集可以包含社交網(wǎng)絡(luò)應用程序的數(shù)據(jù)。
類型/映射-共享共享同一索引中存在的一組公共字段的文檔集合。例如,索引包含社交網(wǎng)絡(luò)應用程序的數(shù)據(jù);對于用戶個人資料數(shù)據(jù),可以有一種特定的類型,對于消息傳遞數(shù)據(jù),可以有另一種類型,對于注釋數(shù)據(jù),可以有另一種類型。
文檔-以特定方式以JSON格式定義的字段的集合。每個文檔都屬于一種類型,并且位于索引內(nèi)。每個文檔都與唯一的標識符(稱為UID)相關(guān)聯(lián)。
字段-Elasticsearch字段可以包含多個相同類型的值(本質(zhì)上是一個列表)。另一方面,在SQL中,一列可以恰好包含所述類型的一個值。
在Django中使用Elasticsearch
安裝和配置,安裝Django Elasticsearch DSL:
$ pip install django-elasticsearch-dsl
然后將django_elasticsearch_dsl
添加到INSTALLED_APPS
必須在django
設(shè)置中定義ELASTICSEARCH_DSL
。
例如:
ELASTICSEARCH_DSL={ ? ? 'default': { ? ? ? ? 'hosts': 'localhost:9200' ? ? }, }
聲明要索引的數(shù)據(jù),然后創(chuàng)建model:
“`python
models.py
class Category(models.Model): name = models.CharField(max_length=30) desc = models.CharField(max_length=100, blank=True) def str(self): return ‘%s' % (self.name)
要使該模型與Elasticsearch
一起使用,請創(chuàng)建django_elasticsearch_dsl.Document
的子類,在Document類中創(chuàng)建一個Index類以定義我們的Elasticsearch索引,名稱,設(shè)置等,最后使用Registry.register_document
裝飾器注冊該類。它需要在應用目錄中的documents.py
中定義Document
類。
documents.py
from django_elasticsearch_dsl import Document from django_elasticsearch_dsl.registries import registry from .models import Category @registry.register_document class CategoryDocument(Document): class Index: name = ‘category' settings = { ‘number_of_shards': 1, ‘number_of_replicas': 0 } class Django: model = Category fields = [ ‘name', ‘desc', ]
填充:
要創(chuàng)建和填充Elasticsearch
索引和映射,請使用search_index
命令:python manage.py search_index — rebuildpythonmanage.pysearch
要獲得更多幫助,請使用命令:python manage.py search_index —help
現(xiàn)在,當執(zhí)行以下操作時:
category = Category( name=”Computer and Accessories”, desc=”abc desc” ) category.save()
該對象也將保存在Elasticsearch
中(使用信號處理程序)。
搜索:
要獲取elasticsearch-dsl-py
搜索實例,請使用:
s = CategoryDocument.search().filter(“term”, name=”computer”)
或者
s = CategoryDocument.search().query(“match”, description=”abc”) for hit in s: print( “Category name : {}, description {}”.format(hit.name, hit.desc) )
要將彈性搜索結(jié)果轉(zhuǎn)換為真實的Django
查詢集,請注意,這會花費一個SQL請求來檢索具有由Elasticsearch
查詢返回的ID的模型實例。
s = CategoryDocument.search().filter(“term”, name=”computer”)[:30] qs = s.to_queryset()
for cat in qs: print(cat.name)
到此這篇關(guān)于在Django中使用ElasticSearch的文章就介紹到這了,更多相關(guān)Django中使用ElasticSearch內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用Python一鍵提取PDF中的表格到Excel的方法詳解
從PDF文件獲取表格中的數(shù)據(jù),也是日常辦公容易涉及到的一項工作,一個一個復制吧,效率確實太低了,用Python從PDF文檔中提取表格數(shù)據(jù),并寫入Excel文件,灰常灰常高效,本文就給大家介紹一下如何使用Python一鍵提取PDF中的表格到Excel,需要的朋友可以參考下2023-08-08Python中函數(shù)的參數(shù)定義和可變參數(shù)用法實例分析
這篇文章主要介紹了Python中函數(shù)的參數(shù)定義和可變參數(shù)用法,以實例形式較為詳細的分析了Python中參數(shù)定義與可變參數(shù)的具體使用方法,需要的朋友可以參考下2015-06-06

Python利用Pandas進行數(shù)據(jù)分析的方法詳解

Python實現(xiàn)一個服務(wù)器監(jiān)聽多個客戶端請求