elasticsearch6.5.4搜索(1.简单查询)

本文介绍如何使用Elasticsearch 6.5.4进行简单的搜索操作,包括通过Postman工具及Python脚本来查询索引库中特定类型文档的方法,并演示了如何获取包含特定词汇的文档。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

elasticsearch6.5.4搜索(1.简单查询)

查询索引library中book类型的title字段中含有crime一词的文档

1.使用postman

url:http://localhost:9002/library/book/_search?pretty=true
在这里插入图片描述
结果:
在这里插入图片描述

2.python 查询

# -*- coding: utf-8 -*-
import json
from elasticsearch import Elasticsearch
from elasticsearch.helpers import bulk

ES_HOST = '127.0.0.1'
ES_USER = ''
ES_PASSWD = ''
ES_PORT = 9002


class ES():
    def __init__(self):
        self.es = Elasticsearch([ES_HOST], port=ES_PORT, timeout=300)

    # def create_index(self, index):  参见上篇博文

    # def insert_bulk(self, data_list):  参见上篇博文

    def search_data(self, index, doc_type, query):
        """
        查询数据
        :param self:
        :param query: 查询条件
        :return:
        """
        rtn = self.es.search(index=index, doc_type=doc_type, scroll='5m', body=json.dumps(query), request_timeout=300)
        return rtn


if __name__ == '__main__':
    es = ES()
    print '---------'
    # 查询数据
    index = 'library'
    doc_type = 'book'
    query = {
        "min_score":0.5,  # 根据查询结果匹配度得分获取数据,分值越高,数据的匹配度越高。此处查询大于等于0.5的数据
        "version": True,  # 返回版本信息
        "size": 2,  # 返回2条数据
        "query": {"query_string": {"query": "title:crime"}},  # 查询条件
        "_source": ["author", "tags", "title", "year"],  # 返回指定字段
        "script_fields" : {  # 使用脚本字段,获取year字段值进行计算,计算结果赋值给新增字段correctYear
            "correctYear" : {
                "script" : "doc['year'].value - 1000"
            }
        }
    }
    print es.search_data(index, doc_type, query)
    print '---------'
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值