在Elasticsearch 7.x的Python SDK中,能够应用analyze
API来查看分词后的后果,并指定自定义的分词器。上面是一个示例代码:
from elasticsearch import Elasticsearch# 创立 Elasticsearch 客户端es = Elasticsearch()# 要剖析的文本内容text = "This is a sample text to analyze."# 自定义分词器的名称analyzer_name = "my_custom_analyzer"# 剖析文本内容response = es.indices.analyze( index="your_index_name", # 替换为你的索引名 body={ "analyzer": analyzer_name, "text": text })# 提取分词后果tokens = [token["token"] for token in response["tokens"]]# 打印分词后果print(tokens)
在上述代码中,你须要将your_index_name
替换为你理论应用的索引名称,同时依据你的需要,将analyzer_name
替换为你想要应用的自定义分词器的名称。而后,调用es.indices.analyze()
办法,传递analyzer
参数和text
参数来指定要应用的分词器和待剖析的文本内容。最初,从API的响应中提取分词后果并进行解决。