题目放在这里:
接前一节:https://www.toutiao.com/article/7113727029614215719/
-------------------------------------------------------------------------------------------------------------------
----------------------准备知识分割线------------------------------------------------------------------------
温故而知新,这里先复习下Elastic search的相关概念:
0.什么是ES?
摘自:https://cloud.tencent.com/developer/article/1750560
常用场景:
像下图中使用的设计:
特点:
1.ES 和Mysql 对比?
1.1 拿ES 和 mysql 做对比,es 里面的document 相当于mysql的table, 而document 相当于row, field 相当于column;
而es 中间支持shard的概念,可以把一个表(index)拆到不同的分片(shard),不同分片放到不同的节点,并通过replicaset 来做冗余保护。
2.python 如何使用ES?
参考这篇文章:
https://elasticsearch-py.readthedocs.io/en/7.x/
这里讲了几个概念,支持异步,支持复杂查询,支持ssl 模式交互等。
https://elasticsearch-py.readthedocs.io/en/7.x/async.html
2.支持复杂查询DSL ,需要调用单独的接口:
https://elasticsearch-dsl.readthedocs.io/en/latest/
简单接口模式如下图:7.0 版本
3.传说中的倒排索引到底是什么意思?
摘自:https://cloud.tencent.com/developer/article/1750560
什么是倒排索引?
倒排索引也可以成为反向索引。
作为开发咱们经常接触到的就是 MySql,假设有一堆技术书籍,并且已经编上号。
- 如果放在 MySql 里面就是这样
Mysql 中布局
此时我想查询所有关于 并发 的书籍。
select * from table_book where book_name like %并发%;
然后会开始遍历表格,查找到 1和4两条记录。
- 如果是倒排索引处理的话
首先会将每个名称进行分词,比如 Java 并发编程之美 会被分为 Java 、并发、 编程、 之 、美。分词结束之后按照词关联书籍的编号。
ES 中倒排索引
在倒排索引中搜索并发,然后进行检索,就很容易定位到关于并发书籍的编号。
-------------------------------------------------------------------------------------------------------------------
----------------------准备知识分割线------------------------------------------------------------------------
题目中要用到的几个函数:
es.count(index="index_name") 查询某table 中的条目数量,返回值是一个字典,含count 值和shard值。取count值即可获取相关信息。
收集某个表的行信息,看到是7行(count)
向ES里某个表里插入JSON数据:
es.index(index="inventory", id = int(id)+1 ,document={“name”:name,'Serial number':sn,'Value':value}
from flask import Flask,render_template,request,url_for,redirect,jsonify
from datetime import date
import requests
from elasticsearch import Elasticsearch
app = Flask(__name__)
def store_into_es(name,sn,value):
#连接ES
es = Elasticsearch(['https://**.public.tencentelasticsearch.com:9200'],basic_auth=('elastic', '**'), sniff_on_start=False,sniff_on_node_failure=False,sniff_timeout=None)
# 查看inventory 表中的信息
id = es.count(index='inventory')['count']
print(id)
print(es.count(index='inventory'))
#print("the id is ".format(id))
try:
print("the id is {0}, we need +1".format(id))
except:
print("the id is not exist")
id = 0
print("The info to be stored is {0}".format({'name':name,'Serial number':sn,'value':value}))
res = es.index(index="inventory",id=int(id)+1,document={'name':name,'Serial number':sn,'value':value})
print(res)
return 1
def display_es():
# 连接ES
list1 =[]
es = Elasticsearch(['https://**.public.tencentelasticsearch.com:9200'],basic_auth=('elastic', '**'), sniff_on_start=False, sniff_on_node_failure=False,sniff_timeout=None)
# 查看inventory 表中的信息
id = es.count(index='inventory')['count']
for i in range(1, id + 1):
res = es.get(index="inventory", id=i)
print(res['_source'])
list1.append(res['_source'])
return list1
"""
1.把数据json化, {'name':name,'Serial number':sn,'value':value};
2.把json数据插入表格inventory,其中id 为之前的id+1;
3.
"""
@app.route('/api/input/',methods=['POST'])
def edit():
name = request.json['name']
sn = request.json['sn']
value = request.json['value']
result = store_into_es(name,sn,value)
if result == 1:
info = "The inventory info is successful stored"
else:
info = "Something goes wrong, can not store into storage"
return jsonify("info",info)
@app.route('/api/display/',methods=['POST'])
def display_info():
list1 = display_es()
return jsonify("enventory info",list1)
if __name__ =='__main__':
app.run(host='0.0.0.0',port=8008,debug=True)
上代码,
看下插入的效果:
插入一条数据:{"name":"testitem22", "sn":"12164", "value":"434"}
Postman 返回信息
后台返回信息拆入成功:
后台返回信息
展示所有列表信息:
后台现实的信息
这里只完成了2个功能:
插入和搜索所有列表。还有一个模糊搜索,匹配。
明天继续。
留言与评论(共有 0 条评论) “” |