elasticsearch TypeError:“ObjectApiResponse”对象不支持项分配

cgyqldqp  于 7个月前  发布在  ElasticSearch
关注(0)|答案(1)|浏览(82)

我在python中制作了一个API来将其与elasticsearch链接,但在执行时,它在第23行res 'ST '] = search_term中生成错误,翻译为“ObjectApiResponse”对象不支持项目分配,我感谢您的帮助和合作

from flask import Flask, render_template, request
from elasticsearch import Elasticsearch
import os

os.chdir('/home/ambu/Documents/search')
app: Flask = Flask(__name__, template_folder='templates')
es = Elasticsearch("http://127.0.0.1:9200")

@app.route('/')
def index():
    return render_template('search.html')

@app.route('/search/results', methods=['GET', 'POST'])
def request_search():
    search_term = request.form["input"]
    res = es.search(
        index='data_example4',
        body={
            "query": {"match": {"content": search_term}},
            "highlight": {"pre_tags": ["<b>"], "post_tags": ["</b>"], "fields": {"content": {}}}})
    res['ST'] = search_term
    for hit in res['hits']['hits']:
        hit['good_summary'] = '….'.join(hit['highlight']['content'][1:])
        return render_template('results.html', res=res)

if __name__ == '__main__':
    app.run('127.0.0.1', debug=True)

字符串
感谢您帮助解决这个问题。

quhf5bfb

quhf5bfb1#

ElasticSearch Python客户端从版本7到版本8有一些变化。其中一个变化是API不再返回原始dict,它现在是一个结构化的ApiResponseObject。但是你可以使用update方法来添加到这个对象。

res.update({"ST": search_term})

字符串
有关更多信息,请参阅https://www.elastic.co/guide/en/elasticsearch/client/python-api/current/migration.html

相关问题