社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
私信  •  关注

Essex

Essex 最近回复了
7 年前
回复了 Essex 创建的主题 » ElasticSearch和python:搜索功能问题

这就是我所做的,而且似乎有效:

def search_in_annual(self, q):
    try:
        response = self.es.search(
            index='app', doc_type='annual-report', q=q, _source_exclude=['data'], size=5000)

        if response['hits']['total'] == 0:

            response = self.es.search(
                index='app', doc_type='annual-report',
                body={
                    "query":
                        {"prefix": {"title": q}},
                }, _source_exclude=['data'], size=5000)

    except ConnectionError:
        return -1, None

    total = 0
    hits = []
    if response:
        for hit in response["hits"]["hits"]:
            hits.append({
                'id': hit['_id'],
                'title': hit['_source']['title'],
                'file': hit['_source']['relative_path'],
            })

        total = response["hits"]["total"]
    return total, hits

它允许搜索标题、前缀和内容以查找我的文档。