弹性搜索 - 无法解析查询

时间:2017-03-07 12:52:39

标签: python elasticsearch

我正在使用python-elasticsearch客户端 - elasticsearch.py,当我尝试使用update_by_query方法更新文档时,弹性搜索无法解析查询。

我的更新机构如下:

{
  'script': {
    'inline': 'ctx._source.viewers += info',
    'params': {
      'info': {
        'time': datetime.datetime(2017, 3, 7, 18, 8, 50),
        'viewer': 'abc@xyz.com'
      }
    }
  }
}

弹性搜索的调用如下::

update = es.update_by_query(index=index_el,
                            doc_type='1',
                            q='delivery_reference_id:' + str(cam_id) +'',
                            body=doc)

cam_id的示例值为:

CAM_10_DATA_4_2017-03-07 18:02:07

在内部,形成以下查询::

http://127.0.0.1:9200/user_tracker/1/_update_by_query?q=delivery_reference_id%3ACAM_10_DATA_4_2017-03-07+18%3A02%3A07

收到的错误如下:

TransportError(400, 'search_phase_execution_exception', 'Failed to parse query [delivery_reference_id:CAM_10_DATA_4_2017-03-07 18:02:07]')

完整的错误日志如下:

Traceback (most recent call last):
  File "C:\Users\catch\AppData\Roaming\Python\Python34\site-packages\django\core\handlers\exception.py", line 42, in inner
    response = get_response(request)
  File "C:\Users\catch\AppData\Roaming\Python\Python34\site-packages\django\core\handlers\base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\Users\catch\AppData\Roaming\Python\Python34\site-packages\django\core\handlers\base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\catch\AppData\Roaming\Python\Python34\site-packages\django\views\decorators\csrf.py", line 58, in wrapped_view
    return view_func(*args, **kwargs)
  File "C:\Users\catch\AppData\Roaming\Python\Python34\site-packages\django\views\generic\base.py", line 68, in view
    return self.dispatch(request, *args, **kwargs)
  File "C:\Python34\lib\site-packages\rest_framework\views.py", line 483, in dispatch
    response = self.handle_exception(exc)
  File "C:\Python34\lib\site-packages\rest_framework\views.py", line 443, in handle_exception
    self.raise_uncaught_exception(exc)
  File "C:\Python34\lib\site-packages\rest_framework\views.py", line 480, in dispatch
    response = handler(request, *args, **kwargs)
  File "C:\Users\catch\PycharmProjects\myproject\myproj\apis\views.py", line 516, in get
    viewer=viewer_email)
  File "C:\Users\catch\PycharmProjects\myproject\myproj\apis\views.py", line 541, in update_track_info
    doc=doc)
  File "C:\Users\catch\PycharmProjects\myproject\myproj\apis\views.py", line 553, in es_update
    body=doc)
  File "C:\Python34\lib\site-packages\elasticsearch\client\utils.py", line 73, in _wrapped
    return func(*args, params=params, **kwargs)
  File "C:\Python34\lib\site-packages\elasticsearch\client\__init__.py", line 680, in update_by_query
    doc_type, '_update_by_query'), params=params, body=body)
  File "C:\Python34\lib\site-packages\elasticsearch\transport.py", line 318, in perform_request
    status, headers, data = connection.perform_request(method, url, params, body, ignore=ignore, timeout=timeout)
  File "C:\Python34\lib\site-packages\elasticsearch\connection\http_urllib3.py", line 128, in perform_request
    self._raise_error(response.status, raw_data)
  File "C:\Python34\lib\site-packages\elasticsearch\connection\base.py", line 122, in _raise_error
    raise HTTP_EXCEPTIONS.get(status_code, TransportError)(status_code, error_message, additional_info)
elasticsearch.exceptions.RequestError: TransportError(400, 'search_phase_execution_exception', 'Failed to parse query [delivery_reference_id:CAM_10_DATA_4_2017-03-07 18:02:07]')

如果有人想出来会很好。如果需要更多信息,请告诉我。

欣赏它。提前谢谢。

1 个答案:

答案 0 :(得分:1)

我认为您的查询语法不是最新的。我用以下语法得到了很好的结果:

{
  "query": {
    "bool": {
      "must": [
        { "match": { "MYFIELD": "myoldvalue" } }
      ]
    }
  },
  "script": {
    "inline": "ctx._source.MYFIELD = \"mynewvalue\"",
    "lang": "painless"
  }
}

HTH:)