今天尝试用 elasticsearch_dsl 的 indices.analyze 接口分析字符串遇到的大坑,我所用的版本是 elasticsearch-dsl 6.4.0
我看的视频教程中调用 indices.analyze 接口是这样写的:
res = es.indices.analyze(index=index,body=text,analyzer='ik_max_word',params={'filter':["lowercase"]} )
调试运行却发现提示这个错误:
TypeError: analyze() got an unexpected keyword argument 'analyzer'
查看源码发现我所用的这个版本里面已经没有 analyzer 这个参数了
def analyze(self, index=None, body=None, params=None):
"""
Perform the analysis process on a text and return the tokens breakdown of the text.
`<http://www.elastic.co/guide/en/elasticsearch/reference/current/indices-analyze.html>`_
:arg index: The name of the index to scope the operation
:arg body: Define analyzer/tokenizer parameters and the text on which
the analysis should be performed
:arg format: Format of the output, default 'detailed', valid choices
are: 'detailed', 'text'
:arg prefer_local: With `true`, specify that a local shard should be
used if available, with `false`, use a random shard (default: true)
"""
return self.transport.perform_request(
"GET", _make_path(index, "_analyze"), params=params, body=body
)
试着将分析字典写在 body 参数中:
res = es.indices.analyze(index=index,body={'text':text,'analyzer':"ik_max_word"}, params={'filter':["lowercase"]})
但是还是不行,提示了我这个错误:
elasticsearch.exceptions.RequestError: RequestError(400, 'illegal_argument_exception', 'request [/cnblog/_analyze] contains unrecognized parameter: [filter] -> did you mean [filter_path]?')
经过各种尝试,后来试着把 filter 字段也写在 body参数中,终于测试成功了