使用Python弹性搜索访问inner_hits
数据时遇到问题。我正在
RequestError(400,'search_phase_execution_exception', 'failed to create query'
我尝试使用inner_hits{}
时出错。
我的弹性搜索版本为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}')
答案 0 :(得分:0)
感谢您在运行代码时准确地发布代码,并且可以复制粘贴并运行代码。确实有很大帮助。
映射的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,7,'timed_out':False,'_shards':{'total':5,'successful': 5,'已跳过':0,'失败':0},'点击数:{'总计':1,'最大得分': 0.6931472,'点击数':[{'_index':'2','_type':'tablets','_id':'1','_score':0.6931472,'_source':{'名称':[{ ID”:1,“组合”: '对乙酰氨基酚','同义词':'Crocin'},{'ID':2,'组合': '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
。
该字段应该为nested
,为什么不是呢?
映射中有错字,缺少逗号。 Elasticsearch无法放置映射。为什么脚本没有失败?
由于在Python调用es.indices.create()
中设置了ignore="400"
参数,这使得Python Elasticsearch客户端忽略了HTTP 400 response code,这又相应于“格式错误的数据错误”。 / p>
那么,为什么Elasticsearch允许您执行其他查询,例如为文档建立索引并进行搜索?
因为默认情况下,Elasticsearch不需要映射,并且将从文档的结构推断出来。这称为dynamic mapping。
希望有帮助!