sur*_*ind 3 python nested elasticsearch
我在inner_hits使用 Python 弹性搜索访问数据时遇到了问题。我越来越
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}')
感谢您完全按照运行代码发布代码,并以可以复制粘贴和运行的方式发布代码。它真的有很大帮助。
您的映射的 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, 'skiipped': 0, 'failed': 0}, 'hits': {'total ': 1, 'max_score': 0.6931472, 'hits': [{'_index': '2', '_type': 'tablets', '_id': '1', '_score': 0.6931472, '_source': {'名称':[{'ID':1,'组合':'扑热息痛','同义词':'克罗辛'},{'ID':2,'组合':'泮他拉唑','同义词':' Pantap'}]}, 'inner_hits': {'Names': {'hits': {'total': 1, 'max_score': 0.6931472, 'hits': [{'_index': '2', '_type' : '平板电脑', '_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。
这个领域应该是nested,为什么不是这样?
映射中有一个错字,缺少一个逗号。Elasticsearch 未能放置映射。为什么脚本没有失败?
因为在Python调用中设置了es.indices.create()该ignore="400"参数,使得Python Elasticsearch客户端忽略了HTTP 400响应码,进而对应“格式错误的数据错误”。
那么为什么 Elasticsearch 允许您执行其他查询,例如文档索引和搜索?
因为默认情况下 Elasticsearch 不需要映射,而是从文档的结构中推断出来。这称为动态映射。
| 归档时间: | 
 | 
| 查看次数: | 6389 次 | 
| 最近记录: |