接昨天:
https://www.toutiao.com/article/7116738277759386112/
今天研究了下DSL 语句里面关于fuzzy 查询,
参考这里:
https://www.elastic.co/guide/cn/elasticsearch/guide/current/fuzzy-query.html
from flask import Flask,render_template,request,url_for,redirect,jsonify
from datetime import date
import requests
from elasticsearch import Elasticsearch
from elasticsearch_dsl import Search
app = Flask(__name__)
def store_into_es(name,sn,value):
#连接ES
es = Elasticsearch(['http://localhost:9200'])
# 查看inventory 表中的信息
#print("the id is ".format(id))
try:
id = es.count(index='inventory')['count']
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(['http://localhost:9200'])
# 查看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
def search_es(searchstring):
#连接es
es = Elasticsearch(['http://localhost:9200'])
searchstring = searchstring
print("The string to be searched is {0}".format(searchstring))
# 查看inventory 表中的信息
s = Search(using=es,index="inventory").query("fuzzy",name=searchstring)
response = s.execute()
print(response.to_dict())
responsedic = response.to_dict()
infolist = responsedic['hits']['hits']
return_list = []
if infolist !=[]:
info = responsedic['hits']['hits'][0]['_source']
for i in infolist:
print(i['_source'])
return_list.append(i['_source'])
else:
info = []
print(return_list)
return return_list
"""
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)
@app.route('/api/query/',methods=['POST'])
def query_info():
searchstring = request.json['searchstring']
info = search_es(searchstring)
return jsonify("enventory info",info)
if __name__ =='__main__':
app.run(host='0.0.0.0',port=8008,debug=True)查询当前总量
查询总量后台日志
模糊查询:
模糊查询testitem2
模糊查询testitem1
模糊查询testitem3
从查询日志可以看出,优先返回相似度最高的,再往下排序。这个和查询目的是一致的。
查询testitem3 ,日志
| 留言与评论(共有 0 条评论) “” |