Django 连通Elasticsearch
来自CloudWiki
前置知识
本文档所用的ES索引就是这两个实训中建立起来的,因此在本文档中直接用。
实训步骤
创建项目
[root@localhost ~]# django-admin startproject tutorial
[root@localhost ~]# ls
anaconda-ks.cfg CentOS-Sources.repo jdk1.8.0_211 apache-maven-3.5.4-bin.tar.gz centos.tar jdk-8u211-linux-x64.tar.gz big_data CentOS-Vault.repo mongodb-linux-x86_64-rhel62-4.0.0.tgz CentOS-Base.repo docker-ce.repo Python-3.6.5 CentOS-CR.repo Dockerfile Python-3.6.5.tgz CentOS-Debuginfo.repo elasticsearch-5.5.1 tutorial CentOS-fasttrack.repo elasticsearch-5.5.1.zip CentOS-Media.repo HelloWorld
创建APP
[root@localhost ~]# cd tutorial
[root@localhost tutorial]# python3 manage.py startapp blog
配置APP
在tutorial/settings.py 中添加APP:
[root@localhost tutorial]# vi settings.py
INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'rest_framework', 'blog', ]
配置URL
在tutorial/urls.py中配置URL:
from django.contrib import admin from django.urls import path from django.conf.urls import include urlpatterns = [ path('admin/', admin.site.urls), path('blog/', include('blog.urls')), ]
编写内部路由文件:
blog/urls.py:
from django.conf.urls import url from . import views urlpatterns = [ ]
建立ES操作类
该类的作用是对ES进行增删查改操作。
blog/ElasticObj.py:
#coding:utf8 import os import time from os import walk import csv from datetime import datetime from elasticsearch import Elasticsearch from elasticsearch.helpers import bulk class ElasticObj: def __init__(self, index_name,index_type,ip ="127.0.0.1"): ''' :param index_name: 索引名称 :param index_type: 索引类型 ''' self.index_name =index_name self.index_type = index_type # 无用户名密码状态 self.es = Elasticsearch( ) #用户名密码状态 #self.es = Elasticsearch([ip],http_auth=('elastic', 'password'),port=9200) def create_index(self,index_name="ott",index_type="ott_type"): ''' 创建索引,创建索引名称为ott,类型为ott_type的索引 :param ex: Elasticsearch对象 :return: ''' #创建映射 _index_mappings = { "mappings": { self.index_type: { "properties": { "title": { "type": "text", "index": True, "analyzer": "ik_max_word", "search_analyzer": "ik_max_word" }, "date": { "type": "text", "index": True }, "keyword": { "type": "string", "index": "not_analyzed" }, "source": { "type": "string", "index": "not_analyzed" }, "link": { "type": "string", "index": "not_analyzed" } } } } } if self.es.indices.exists(index=self.index_name) is not True: res = self.es.indices.create(index=self.index_name, body=_index_mappings) print(res) def Index_Data(self): ''' 数据存储到es :return: ''' list = [ { "date": "2017-09-13", "source": "慧聪网", "link": "http://info.broadcast.hc360.com/2017/09/130859749974.shtml", "keyword": "电视", "title": "付费 电视 行业面临的转型和挑战" }, { "date": "2017-09-13", "source": "中国文明网", "link": "http://www.wenming.cn/xj_pd/yw/201709/t20170913_4421323.shtml", "keyword": "电视", "title": "电视 专题片《巡视利剑》广获好评:铁腕反腐凝聚党心民心" } ] for item in list: res = self.es.index(index=self.index_name, doc_type=self.index_type, body=item) print(res['created']) def Get_Data_By_Body(self,word): # doc = {'query': {'match_all': {}}} doc = { "query": { "match": { "keyword": word } } } _searched = self.es.search(index=self.index_name, body=doc) #print(type(_searched)) return _searched['hits']['hits']; #obj = ElasticObj("ott1", "ott_type1") #h = obj.Get_Data_By_Body("电视")
编写视图
最后,让我们创建一个简单的搜索视图,查找按标签过滤的所有帖子,并按标题中的单词进行搜索:
blog/views.py:
from django.http import HttpResponse import traceback from elasticsearch import Elasticsearch from .ElasticObj import ElasticObj def hello(request): #h = create_mapping( ) obj = ElasticObj("ott1", "ott_type1") h = obj.Get_Data_By_Body("电视")#搜索电视 return HttpResponse(h)
编写路由
blog/urls.py:
from django.conf.urls import url from . import views urlpatterns = [ # url(r'^api/list$', views.BlogView.as_view(), name='blog-list'), url(r'^api/$', views.hello), ]
测试
[root@localhost blog]# curl http://10.0.0.30:8000/blog/api/
{'_index': 'ott1', '_type': 'ott_type1', '_id': '3', '_score': 0.6931472, '_source': {'date': '2017-09-13', 'source': '人民电视', 'link': 'http://tv.people.com.cn/BIG5/n1/2017/0913/c67816-29533981.html', 'keyword': '电视', 'title': '中国第21批赴刚果(金)维和部隊启程--人民 电视 --人民网'}}{'_index': 'ott1', '_type': 'ott_type1', '_id': '2', '_score': 0.18232156, '_source': {'date': '2017-09-13', 'source': '中国文明网', 'link': 'http://www.wenming.cn/xj_pd/yw/201709/t20170913_4421323.shtml', 'keyword': '电视', 'title': '电视 专题片《巡视利剑》广获好评:铁腕反腐凝聚党心民心'}}{'_index': 'ott1', '_type': 'ott_type1', '_id': '4', '_score': 0.18232156, '_source': {'date': '2017-09-13', 'source': '站长之家', 'link': 'http://www.chinaz.com/news/2017/0913/804263.shtml', 'keyword': '电视', 'title': '电视 盒子 哪个牌子好? 吐血奉献三大选购秘笈'}}{'_index': 'ott1', '_type': 'ott_type1', '_id': 'AWxhMSpVp5xr6Yl4nhAw', '_score': 0.18232156, '_source': {'date': '2017-09-13', 'source': '慧聪网', 'link': 'http://info.broadcast.hc360.com/2017/09/130859749974.shtml', 'keyword': '电视', 'title': '付费 电视 行业面临的转型和挑战'}}{'_index': 'ott1', '_type': 'ott_type1', '_id': '1', '_score': 0.18232156, '_source': {'date': '2017-09-13', 'source': '慧聪网', 'link': 'http://info.broadcast.hc360.com/2017/09/130859749974.shtml', 'keyword': '电视', 'title': '付费 电视 行业面临的转型和挑战'}}[
或直接在浏览器上打开: