elasticsearch,nested,Python,elasticsearch,Nested" /> elasticsearch,nested,Python,elasticsearch,Nested" />

Python 弹性搜索-无法创建查询

Python 弹性搜索-无法创建查询,python,elasticsearch,nested,Python,elasticsearch,Nested,我在使用Python弹性搜索访问internal\u hits数据时遇到了一个问题。我要走了 RequestError(400,'search\u phase\u execution\u exception','failed to create query' 尝试使用内部\u hits{}时出错 我的弹性搜索版本6.5.4,python版本3.7.2 from elasticsearch import Elasticsearch es = Elasticsearch() mapping =

我在使用Python弹性搜索访问
internal\u hits
数据时遇到了一个问题。我要走了

RequestError(400,'search\u phase\u execution\u exception','failed to create query'

尝试使用
内部\u 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\u 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)
就这样! 脚本的输出如下所示:

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"
        }
    ]
}
{'take':7,'timed_out':False,'u shards':{'total':5,'successful': 5,'跳过':0,'失败':0},'命中':{'total':1,'最高分数': 0.6931472,'点击数':[{'u索引':'2','u类型':'tablets','u id':'1','u分数':0.6931472,'u来源':{'Names':[{'id':1,'组合': “扑热息痛”,“同义词”:“克罗辛”},{'ID':2,“组合”: “潘他普唑”,“同义词”:“潘他普”}]},“内部点击”:{“名称”: {'hits':{'total':1,'max_score':0.6931472,'hits':[{'u index':'2', “_-type':”平板电脑“,“_-id':”1“,“_-nested':{”字段“:”名称“, 'offset':1},'u score':0.6931472,'u source':{'ID':2,'Combination': “潘他普唑”,“同义词”:“潘他普”}}}]}}}}

为什么我会收到关于
创建查询失败的错误消息?
Elasticsearch引发错误
无法创建查询
,因为它无法针对非字段创建查询

字段应该是嵌套的,为什么不是这样

映射中有一个输入错误,缺少一个逗号。Elasticsearch无法放置映射。为什么脚本没有失败

因为在对
es.indexs.create()
的Python调用中设置了参数,这使得Python Elasticsearch客户端忽略了,这反过来对应于“格式错误的数据错误”

那个么为什么Elasticsearch允许您进行其他查询,比如文档索引和搜索

因为默认情况下Elasticsearch不需要映射,而是从文档的结构中推断出来。这称为