gpt4 book ai didi

python - Python中的Elasticsearch多字段查询请求

转载 作者:行者123 更新时间:2023-12-02 23:42:44 25 4
gpt4 key购买 nike

我是Elasticsearch和Python的初学者,并且在Elasticsearch中创建了包含一些数据的索引,并且我想使用python对这些数据执行查询请求。这是我在Kibana的开发工具中创建的数据映射:

PUT /main-news-test-data
{
"mappings": {
"properties": {
"content": {
"type": "text"
},
"title": {
"type": "text"
},
"lead": {
"type": "text"
},
"agency": {
"type": "keyword"
},
"date_created": {
"type": "date"
},
"url": {
"type": "keyword"
},
"image": {
"type": "keyword"
},
"category": {
"type": "keyword"
},
"id":{
"type": "keyword"
}
}
}
}
这是我的Python代码,其中我们给它一个关键字和一个类别编号,它必须为匹配的关键字检入 flex 数据的标题,行首和内容字段,并还要检查输入的类别编号和数据类别编号并返回/打印出符合以下条件的任何对象:
from elasticsearch import Elasticsearch
import json,requests

es = Elasticsearch(HOST="http://localhost", PORT=9200)
es = Elasticsearch()

def QueryMaker (keyword,category):
response = es.search(index="main-news-test-data",body={"from":0,"size":5,"query":{"multi_match":{
"content":keyword,"category":category,"title":keyword,"lead":keyword}}})
return(response)

if __name__ == '__main__':
keyword = input('Enter Keyword: ')
category = input('Enter Category: ')
#startDate = input('Enter StartDate: ')
#endDate = input('Enter EndDate: ')
data = QueryMaker(keyword,category)
print(data)
但是当我将数据提供给输入时,我收到此错误:
elasticsearch.exceptions.RequestError: RequestError(400, 'parsing_exception', '[multi_match] query does not support [content]')
我究竟做错了什么?
编辑:关键字必须包含在标题,线索和内容中,但不必与它们相同

最佳答案

您的 multi_match 查询语法在这里是错误的,我还认为您需要类似的内容查看更多:https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-multi-match-query.html

{
"from":0,
"size":5,
"query": {
"bool": {
"should": [
{
"multi_match" : {
"query": keyword,
"fields": [ "content", "title","lead" ]
}
},
{
"multi_match" : {
"query": category,
"fields": [ "category" ]
}
}
]
}
}
}

关于python - Python中的Elasticsearch多字段查询请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64079351/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com