Django對接elasticsearch實(shí)現(xiàn)全文檢索的示例代碼
前言
說到搜索,第一時(shí)間想到的是mysql數(shù)據(jù)庫的like語句
但是,假如你的數(shù)據(jù)庫有幾千萬條數(shù)據(jù),name字段沒有索引,可能查詢需要十幾分鐘,用戶可能會等你?那為什么不給name字段增加索引?數(shù)據(jù)表不僅僅是用來查詢,也會經(jīng)常修改數(shù)據(jù),新增刪除數(shù)據(jù)等。建立索引后,做增刪改操作時(shí)也會大大占用數(shù)據(jù)庫資源。所以應(yīng)該怎么解決呢?
Elasticsearch!
一個(gè)強(qiáng)大的基于Lucene的全文搜索服務(wù)器!維基百科、Stack Overflow、Github都在用。
如果想詳細(xì)了解其原理的話,可以參考:https://www.elastic.co/guide/index.html
第一步:首先安裝相關(guān)的依賴包
pip install drf-haystack pip install elasticsearch pip install djangorestframework
第二步:在django項(xiàng)目配置文件settings.py中注冊應(yīng)用
INSTALLED_APPS = [ 'app.apps.AppConfig', 'haystack', 'rest_framework' ]
第三步:在django項(xiàng)目配置文件settings.py中指定搜索的后端
HAYSTACK_CONNECTIONS = {
'default': {
'ENGINE':'haystack.backends.elasticsearch_backend.ElasticsearchSearchEngine',
'URL': 'http://127.0.0.1:9200/', # 此處為elasticsearch運(yùn)行的服務(wù)器ip地址,端口號固定為9200
'INDEX_NAME': 'test', # 指定elasticsearch建立的索引庫的名稱
},
}
# 當(dāng)添加、修改、刪除數(shù)據(jù)時(shí),自動生成索引
HAYSTACK_SIGNAL_PROCESSOR = 'haystack.signals.RealtimeSignalProcessor'
# 指定搜索結(jié)果每頁的條數(shù)
# HAYSTACK_SEARCH_RESULTS_PER_PAGE = 1
第四步:創(chuàng)建索引類
在此之前要先創(chuàng)建model類,并插入數(shù)據(jù)
from django.db import models
class Es(models.Model):
name=models.CharField(max_length=32)
desc=models.CharField(max_length=32)
在需要進(jìn)行索引的應(yīng)用的目錄下創(chuàng)建文件search_indexes.py, 在該文件內(nèi)創(chuàng)建該索引類
我在app應(yīng)用下創(chuàng)建:search_indexes.py
# 索引模型類的名稱必須是 模型類名稱 + Index
from haystack import indexes
from .models import Es
class EsIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.CharField(document=True, use_template=True)
def get_model(self):
"""返回建立索引的模型類"""
return Es
def index_queryset(self, using=None):
"""返回要建立索引的數(shù)據(jù)查詢集"""
return self.get_model().objects.all()
第五步:在templates目錄中創(chuàng)建text字段使用的模板文件
創(chuàng)建文件templates/search/indexes/app/es_text.txt文件中定義
{{ object.name }}
{{ object.desc }}
第六步:手動更新索引
python manage.py rebuild_index #數(shù)據(jù)庫有多少條數(shù)據(jù),全部會被同步到es中
第七步:創(chuàng)建haystack序列化器
from drf_haystack.serializers
import HaystackSerializer
from rest_framework.serializers
import ModelSerializer from app
import models
from app.search_indexes import EsIndex
class EsSerializer(ModelSerializer):
class Meta:
model=models.Es
fields='__all__'
class EsIndexSerializer(HaystackSerializer):
object = EsSerializer(read_only=True) # 只讀,不可以進(jìn)行反序列化
class Meta:
index_classes = [EsIndex]# 索引類的名稱
fields = ('text', 'object')# text 由索引類進(jìn)行返回, object 由序列化類進(jìn)行返回,第一個(gè)參數(shù)必須是text
第八步:創(chuàng)建視圖類
from drf_haystack.viewsets
import HaystackViewSet
from app.models import Book
from app.serializers import EsIndexSerializer
class EsSearchView(HaystackViewSet):
index_models = [Es]
serializer_class = EsIndexSerializer
第九步:添加路由
from django.conf.urls
import url from django.contrib
import admin
from rest_framework import routers
from app.views import EsSearchView
router = routers.DefaultRouter()
router.register("book/search", EsSearchView, base_name="book-search")
urlpatterns = [ url(r'^admin/', admin.site.urls), ]
urlpatterns += router.urls
第十步:結(jié)果
http://127.0.0.1:8000/?text=測試
到此這篇關(guān)于Django對接elasticsearch實(shí)現(xiàn)全文檢索的示例代碼的文章就介紹到這了,更多相關(guān)Django elasticsearch實(shí)現(xiàn)全文檢索內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- golang操作elasticsearch的實(shí)現(xiàn)
- Django利用elasticsearch(搜索引擎)實(shí)現(xiàn)搜索功能
- django使用haystack調(diào)用Elasticsearch實(shí)現(xiàn)索引搜索
- Django項(xiàng)目之Elasticsearch搜索引擎的實(shí)例
- 在Django中使用ElasticSearch
- go語言實(shí)現(xiàn)Elasticsearches批量修改查詢及發(fā)送MQ操作示例
- Go語言Elasticsearch數(shù)據(jù)清理工具思路詳解
- GO語言操作Elasticsearch示例分享
相關(guān)文章
深入掌握Python模塊創(chuàng)建導(dǎo)入和使用
這篇文章主要為大家介紹了深入掌握Python模塊創(chuàng)建導(dǎo)入和使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10
Python合并2個(gè)字典成1個(gè)新字典的方法(9種)
這篇文章主要介紹了Python合并2個(gè)字典成1個(gè)新字典的方法,本文通過實(shí)例代碼給大家分享9中方法,需要的朋友可以參考下2019-12-12
詳解sklearn?Preprocessing?數(shù)據(jù)預(yù)處理功能
這篇文章主要介紹了sklearn?Preprocessing?數(shù)據(jù)預(yù)處理功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-08-08
Python使用matplotlib繪制多個(gè)圖形單獨(dú)顯示的方法示例
這篇文章主要介紹了Python使用matplotlib繪制多個(gè)圖形單獨(dú)顯示的方法,結(jié)合實(shí)例形式分析了matplotlib實(shí)現(xiàn)繪制多個(gè)圖形單獨(dú)顯示的具體操作技巧與注意事項(xiàng),代碼備有較為詳盡的注釋便于理解,需要的朋友可以參考下2018-03-03
python實(shí)現(xiàn)的分層隨機(jī)抽樣案例
這篇文章主要介紹了python實(shí)現(xiàn)的分層隨機(jī)抽樣案例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-02-02
Python + selenium自動化環(huán)境搭建的完整步驟
這篇文章主要給大家介紹了關(guān)于Python + selenium自動化環(huán)境搭建的相關(guān)資料,文中通過圖文將實(shí)現(xiàn)的步驟一步步介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧2018-05-05
python多進(jìn)程提取處理大量文本的關(guān)鍵詞方法
今天小編就為大家分享一篇python多進(jìn)程提取處理大量文本的關(guān)鍵詞方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-06-06

