Analyzer https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-analyzers.html#analysis-analyzers是一个包装器,它包装了三个函数:
-
字符过滤器 https://www.elastic.co/guide/en/elasticsearch/reference/current/analyzer-anatomy.html#_character_filters:主要用于剥离一些未使用的字符或者改变一些字符。
-
分词器 https://www.elastic.co/guide/en/elasticsearch/reference/current/analyzer-anatomy.html#_tokenizer:将文本分解为单独的标记(或单词),并根据某些因素(空格、ngram 等)进行分解。
-
令牌过滤器 https://www.elastic.co/guide/en/elasticsearch/reference/current/analyzer-anatomy.html#_token_filters:它接收令牌,然后应用一些过滤器(例如将大写术语更改为小写)。
简而言之analyzer用于告诉elasticsearch 应如何对文本进行索引和搜索。
你正在寻找的是分析API https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-analyze.html#indices-analyze,这是了解分析器如何工作的一个非常好的工具。文本提供给此 API,与索引无关。
在你的情况下获取请求:
GET http://localhost:9200/_analyze?text=I%20sing%20he%20sings%20they%20are%20singing&analyzer=snowball
相当于:
GET _analyze
{
"analyzer" : "snowball",
"text" : "I sing he sings they are singing"
}
其输出:
{
"tokens": [
{"token": "i", "position": 1, ...},
{"token": "sing", "position": 2, ...},
{"token": "he", "position": 3, ...},
{"token": "sing", "position": 4, ...},
{"token": "sing", "position": 7, ...},
]
}
如中提到的article https://www.elastic.co/blog/found-text-analysis-part-1.
还有一件事,假设您在索引中定义了一个自定义分析器,它以您自己的方式组合执行字符过滤、标记化和标记过滤,并且您想检查它将如何标记文本,那么您可以使用_analyze终点与您的索引名称,即使在这种情况下,您也必须提供文本。
GET my_index/_analyze
{
"analyzer" : "custom",
"text" : "I sing he sings they are singing" --> You have to provide the text.
}
为什么需要分析仪?
当您想要对文本或短语进行索引时,通常会使用分析器,将文本分解为单词非常有用,以便您可以搜索术语来获取文档。
Example:假设您有一个索引 (my_index),在该索引中您有一个文本字段(简介),并且您索引了一个文档,其中"intro":"你好,我是希德"如果您不使用分析器,那么这将存储为“嗨,我是 sid”。如果您想查询此文档,则必须编写完整的短语(查找 intro =“Hi There I am sid”的文档)。但是,如果这个短语被索引为标记,那么即使您查询标记(查找 intro="sid" 的文档),您也会获得该文档。
Note:默认情况下标准分析仪 https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-standard-analyzer.html#analysis-standard-analyzer用于所有文本字段。
希望能帮助到你 !