gpt4 book ai didi

python - Elasticsearch - 无法创建查询

转载 作者:行者123 更新时间:2023-12-02 22:11:51 26 4
gpt4 key购买 nike

访问inner_hits时遇到问题使用 Python Elasticsearch 的数据。我越来越

RequestError(400,'search_phase_execution_exception', 'failed to create query'



我尝试使用时出错 inner_hits{} .
我的 Elasticsearch 版本 6.5.4,python 版本 3.7.2。
from elasticsearch import Elasticsearch
es = Elasticsearch()


mapping = '''{
"mappings": {
"tablets": {
"properties": {
"Names": {
"type": "nested"
"properties":{
"ID": {"type" : "long"},
"Combination": {"type" : "text"},
"Synonyms": {"type" : "text"}
}
}
}
}
}
}'''

es.indices.create(index="2", ignore=400, body=mapping)

tablets = {
"Names":[
{
"ID" : 1,
"Combination": "Paracetamol",
"Synonyms": "Crocin"
},{
"ID" : 2,
"Combination": "Pantaprazole",
"Synonyms": "Pantap"
}]}

res = es.index(index="2", doc_type='json', id=1, body=tablets)

z = "patient took Pantaprazole."



res= es.search(index='2',body=
{
"query": {
"nested": {
"path": "Names",
"query": {
"match": {"Names.Combination" : z}
},
"inner_hits": {}
}
}
})
print(res)

Output---------------------------------------------------

"inner_hits": {}
File "C:\Users\aravind\AppData\Local\Programs\Python\Python37-32\lib\site-packages\elasticsearch\client\utils.py", line 76, in _wrapped
return func(*args, params=params, **kwargs)
File "C:\Users\aravind\AppData\Local\Programs\Python\Python37-32\lib\site-packages\elasticsearch\client\__init__.py", line 660, in search
doc_type, '_search'), params=params, body=body)
File "C:\Users\aravind\AppData\Local\Programs\Python\Python37-32\lib\site-packages\elasticsearch\transport.py", line 318, in perform_request
status, headers_response, data = connection.perform_request(method, url, params, body, headers=headers, ignore=ignore, timeout=timeout)
File "C:\Users\aravind\AppData\Local\Programs\Python\Python37-32\lib\site-packages\elasticsearch\connection\http_urllib3.py", line 186, in perform_request
self._raise_error(response.status, raw_data)
File "C:\Users\aravind\AppData\Local\Programs\Python\Python37-32\lib\site-packages\elasticsearch\connection\base.py", line 125, in _raise_error
raise HTTP_EXCEPTIONS.get(status_code, TransportError)(status_code, error_message, additional_info)
elasticsearch.exceptions.RequestError: RequestError(400, 'search_phase_execution_exception', 'failed to create query: {\n "nested" : {\n "query" : {\n
"match" : {\n "Names.Combination" : {\n "query" : "patient took Pantaprazole.",\n "operator" : "OR",\n "prefix_length" : 0,\n "max_expansions" : 50,\n "fuzzy_transpositions" : true,\n "lenient" : false,\n "zero_terms_query" : "NONE",\n "auto_generate_synonyms_phrase_query" : true,\n "boost" : 1.0\n }\n }\n },\n "path" : "Names",\n "ignore_unmapped" : false,\n "score_mode" : "avg",\n "boost" : 1.0,\n "inner_hits" : {\n "ignore_unmapped" : false,\n "from" : 0,\n "size" : 3,\n "version" : false,\n "explain" : false,\n "track_scores" : false\n }\n }\n}')

最佳答案

感谢您完全按照运行代码发布代码,并以可以复制粘贴和运行的方式发布代码。它真的有很大帮助。
您的映射的 JSON 中缺少一个逗号,但错误被忽略,因为您设置了 ignore="400" .
以下是固定脚本的外观:

import time

from elasticsearch import Elasticsearch
es = Elasticsearch()

# fix typo - missing comma after "nested"
mapping = '''{
"mappings": {
"tablets": {
"properties": {
"Names": {
"type": "nested",
"properties":{
"ID": {"type" : "long"},
"Combination": {"type" : "text"},
"Synonyms": {"type" : "text"}
}
}
}
}
}
}'''

# remove ignore="400"
es.indices.create(index="2", body=mapping)

tablets = {
"Names": [
{
"ID": 1,
"Combination": "Paracetamol",
"Synonyms": "Crocin"
}, {
"ID": 2,
"Combination": "Pantaprazole",
"Synonyms": "Pantap"
}
]
}
我们还需要设置 doc_type到映射中声明的那个:
# set doc_type to 'tablets' since this is what we defined in mapping
res = es.index(index="2", doc_type='tablets', id=1, body=tablets)

z = "patient took Pantaprazole."

# allow Elasticsearch to refresh data so it is searchable
time.sleep(2)

res= es.search(index='2',body=
{
"query": {
"nested": {
"path": "Names",
"query": {
"match": {"Names.Combination" : z}
},
"inner_hits": {}
}
}
})
print(res)
就是这样!
脚本的输出将如下所示:

{'took': 7, 'timed_out': False, '_shards': {'total': 5, 'successful':5, 'skipped': 0, 'failed': 0}, 'hits': {'total': 1, 'max_score':0.6931472, 'hits': [{'_index': '2', '_type': 'tablets', '_id': '1', '_score': 0.6931472, '_source': {'Names': [{'ID': 1, 'Combination':'Paracetamol', 'Synonyms': 'Crocin'}, {'ID': 2, 'Combination':'Pantaprazole', 'Synonyms': 'Pantap'}]}, 'inner_hits': {'Names':{'hits': {'total': 1, 'max_score': 0.6931472, 'hits': [{'_index': '2','_type': 'tablets', '_id': '1', '_nested': {'field': 'Names','offset': 1}, '_score': 0.6931472, '_source': {'ID': 2, 'Combination':'Pantaprazole', 'Synonyms': 'Pantap'}}]}}}}]}}


为什么我会收到关于 failed to create query 的错误消息?
Elasticsearch 引发错误 failed to create query因为它没有设法创建 nested query针对非 nested field 。
该字段应该是 nested ,为什么不是这样?
映射中有一个错字,缺少一个逗号。 Elasticsearch 未能放置映射。为什么脚本没有失败?
因为在 Python 中调用 es.indices.create() ignore="400" 参数被设置,这使得 Python Elasticsearch 客户端忽略了 HTTP 400 response code ,这又对应于“格式错误的数据错误”。
那么为什么 Elasticsearch 允许您执行其他查询,例如文档索引和搜索?
因为默认情况下 Elasticsearch 不需要映射,而是从文档的结构中推断出来。这被称为 dynamic mapping .

关于python - Elasticsearch - 无法创建查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54635346/

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