美文网首页
ES索引查询匹配,简单记录

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

作者: 布口袋_天晴了 | 来源:发表于2019-06-22 19:34 被阅读0次

    存入数据代码,以及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索引查询匹配,简单记录

          本文链接:https://www.haomeiwen.com/subject/uimvqctx.html