1.创建索引
C:\Users\DELL>curl -XPUT http://localhost:9200/qa/
2.创建type
http://localhost:9200/qa/_mapping/question
<http://localhost:9200/qa/_mapping/question>
{
"properties": {
"title": {
"analyzer": "ik_max_word",
"type": "text"
}
}
}
ES设置mapping出错 <https://ask.csdn.net/questions/678700>
3.插入数据
http://localhost:9200/qa/question/1 <http://localhost:9200/qa/question/1>
{
"title":"去哪里照X光?"
}
ES学习连接 <https://www.cnblogs.com/sheeva/p/4837881.html>
4.使用crul命令插入ES中
C:\Users\DELL>curl -H "Content-Type: application/json" -XPOST
localhost:9200/qa/
question/7 -d "{"""title""":"""hello"""}"
{"_index":"qa","_type":"question","_id":"7","_version":1,"result":"created","_sh
ards":{"total":2,"successful":1,"failed":0},"_seq_no":1,"_primary_term":1}
新版本一定要添加-H,windows下面要用三个双引号。
Python错误解决方法 <https://blog.csdn.net/dtiove/article/details/78870607>
5.在cmd中插入一直遇到乱码问题,因此使用postman把代码转化成python命令
import http.client conn = http.client.HTTPConnection("localhost") payload =
"{\r\n\"title\":\"去哪里照X光?\"\r\n}".encode('utf-8') headers = { 'URIEncoding':
"UTF-8", 'Content-Type': "application/json", 'Cache-Control': "no-cache",
'Postman-Token': "7223e318-04a7-4790-92b7-728214eb9585" } conn.request("POST",
"qa,question,10", payload, headers) res = conn.getresponse() data = res.read()
print(data.decode("utf-8"))
这样就能够成功跑通。
Postman转Python的学习链接
<https://blog.csdn.net/qq_36350532/article/details/80239979>
6.使用postman,搜索索引和type下面的所有数据
import requests url = "http://localhost:9200/qa/question/3" payload =
"{\r\n\"title\":\"去哪里照X光?\"\r\n}".encode('utf-8') headers = { 'URIEncoding':
"UTF-8", 'Content-Type': "application/json", 'Cache-Control': "no-cache",
'Postman-Token': "8c149885-5c1d-4c26-9ee8-7e9d089610b5" } response =
requests.request("PUT", url, data=payload, headers=headers) print(response.text)
7.删除id为1的数据
8.使用ES做搜索
#-*-coding:utf8-*- import requests import json import xlrd
data=xlrd.open_workbook("协和导诊问答测试结果对比.xls") table=data.sheets()[0]
rows=table.nrows for row in range(rows-1): text=table.row_values(row+1)[0] url
= "http://localhost:9200/qa/question/_search?pretty&q=title:"+text # print(url)
headers = { 'URIEncoding': "UTF-8", 'Content-Type': "application/json",
'Cache-Control': "no-cache", 'Postman-Token':
"4bbfa686-e907-4e75-b4fe-e4a3cf3f66f4" } response = requests.request("GET",
url, headers=headers) response = json.loads(response.text) #
print(response["hits"]["hits"][0]["_source"]["title"]) if
len(response["hits"]["hits"])!=0:
print(text+"|"+response["hits"]["hits"][0]["_source"]["title"])
热门工具 换一换