ES索引查询匹配,简单记录

存入数据代码,以及es删除索引数据

from elasticsearch import Elasticsearch
import elasticsearch.helpers
from datetime import datetime
actions = []
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
es = Elasticsearch(["your_ip"], port=your_port)
### es.delete(index="knowledge", doc_type="knowledge", id='8ba14cFXTn6GKHaDwbKeoQ')
### 在服务器上直接删  curl -XDELETE localhost:29201/knowledge_index

with open('your_data.txt', 'r', encoding='utf8') as f:

    for i,line in enumerate(f):
        if(i%2==0):
            question = line
        if(i%2==1):
            answer = line
            data = {}
            data['_op_type'] = 'index'
            data['_type'] = 'knowledge'
            data['_index'] = 'knowledge'
            data['_source'] = {}
            data['_source']['Question'] = question
            data['_source']['Answer'] = answer
            data['_source']['@timestamp'] = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
            data['_source']['version'] = '0.0.1'
            actions.append(data)
elasticsearch.helpers.bulk(es,actions)

从es里查询数据------单匹配查询

import elasticsearch
es = elasticsearch.Elasticsearch('your_ip:your_port') 
question = '今天天气怎么样啊?'
idx = 'knowledge'
field = 'Question'
body = {
    "sort": {
        "_score": "desc"
    },
    "query": {
        "match": {
            field: question,
        }
    },
    "size": 10,
}
result = es.search(index=idx, body=body)
print('result', result)

你可能感兴趣的:(ES索引查询匹配,简单记录)