当在搜索操作期间处理查询时,分析模块会分析任何索引中的内容。该模块由分析器,令牌生成器,令牌过滤器和字符过滤器组成。如果未定义分析器,则默认情况下,内置分析器,令牌,过滤器和令牌生成器会在分析模块中注册。
在下面的示例中,我们使用一个标准分析器,该分析器在没有指定其他分析器时使用。它将根据语法对句子进行分析,并生成句子中使用的单词。
POST _analyze
{
   "analyzer": "standard",
   "text": "Today's weather is beautiful"
}运行上面的代码后,我们得到如下所示的响应:
{
   "tokens" : [
      {
         "token" : "today's",
         "start_offset" : 0,
         "end_offset" : 7,
         "type" : "",
         "position" : 0
      },
      {
         "token" : "weather",
         "start_offset" : 8,
         "end_offset" : 15,
         "type" : "",
         "position" : 1
      },
      {
         "token" : "is",
         "start_offset" : 16,
         "end_offset" : 18,
         "type" : "",
         "position" : 2
      },
      {
         "token" : "beautiful",
         "start_offset" : 19,
         "end_offset" : 28,
         "type" : "",
         "position" : 3
      }
   ]
}我们可以使用各种参数配置标准分析器,以获取我们的自定义要求。
在以下示例中,我们将标准分析器配置为max_token_length为5。
为此,我们首先使用具有max_length_token参数的分析器创建索引。
PUT index_4_analysis
{
   "settings": {
      "analysis": {
         "analyzer": {
            "my_english_analyzer": {
               "type": "standard",
               "max_token_length": 5,
               "stopwords": "_english_"
            }
         }
      }
   }
}接下来,我们用如下所示的文本应用分析器。请注意令牌是如何不显示的,因为它在开头有两个空格,在结尾有两个空格。对于“ is”这个词,它的开头有一个空格,结尾有一个空格。把它们全部取出来,就变成了4个带空格的字母,这并不意味着它就是一个单词。至少在开头或结尾应该有一个非空格字符,使它成为一个要计数的单词。
POST index_4_analysis/_analyze
{
   "analyzer": "my_english_analyzer",
   "text": "Today's weather is beautiful"
}运行上面的代码后,我们得到如下所示的响应:
{
   "tokens" : [
      {
         "token" : "today",
         "start_offset" : 0,
         "end_offset" : 5,
         "type" : "",
         "position" : 0
      },
      {
         "token" : "s",
         "start_offset" : 6,
         "end_offset" : 7,
         "type" : "",
         "position" : 1
      },
      {
         "token" : "weath",
         "start_offset" : 8,
         "end_offset" : 13,
         "type" : "",
         "position" : 2
      },
      {
         "token" : "er",
         "start_offset" : 13,
         "end_offset" : 15,
         "type" : "",
         "position" : 3
      },
      {
         "token" : "beaut",
         "start_offset" : 19,
         "end_offset" : 24,
         "type" : "",
         "position" : 5
      },
      {
         "token" : "iful",
         "start_offset" : 24,
         "end_offset" : 28,
         "type" : "",
         "position" : 6
      }
   ]
}下表列出了各种分析仪的列表及其说明-
| 序号 | 分析器和说明 | 
|---|---|
| 1 | 标准分析器(standard) stopwords和max_token_length设置可以为这个分析器设置。默认情况下,stopwords列表为空,max_token_length为255。 | 
| 2 | 简单分析器(simple) 该分析器由小写的 tokenizer 组成。 | 
| 3 | 空白分析器 (whitespace) 该分析器由空格标记器组成 | 
| 4 | 停止分析器 (stop) 可以配置stopwords和stopwords_path。默认情况下,stopwords初始化为英文停止词,stopwords_path包含包含停止词的文本文件的路径 | 
令牌生成器用于从Elasticsearch中的文本生成令牌。通过将空格或其他标点符号考虑在内,可以将文本分解为标记。Elasticsearch有很多内置的标记器,可以在自定义分析器中使用。
下面显示了一个分词器的示例,该分词器在遇到非字母的字符时将文本分解为多个词,但也会将所有词都小写,如下所示-
POST _analyze
{
   "tokenizer": "lowercase",
   "text": "It Was a Beautiful Weather 5 Days ago."
}运行上面的代码后,我们得到如下所示的响应:
{
   "tokens" : [
      {
         "token" : "it",
         "start_offset" : 0,
         "end_offset" : 2,
         "type" : "word",
         "position" : 0
      },
      {
         "token" : "was",
         "start_offset" : 3,
         "end_offset" : 6,
         "type" : "word",
         "position" : 1
      },
      {
         "token" : "a",
         "start_offset" : 7,
         "end_offset" : 8,
         "type" : "word",
         "position" : 2
      },
      {
         "token" : "beautiful",
         "start_offset" : 9,
         "end_offset" : 18,
         "type" : "word",
         "position" : 3
      },
      {
         "token" : "weather",
         "start_offset" : 19,
         "end_offset" : 26,
         "type" : "word",
         "position" : 4
      },
      {
         "token" : "days",
         "start_offset" : 29,
         "end_offset" : 33,
         "type" : "word",
         "position" : 5
      },
      {
         "token" : "ago",
         "start_offset" : 34,
         "end_offset" : 37,
         "type" : "word",
         "position" : 6
      }
   ]
}令牌生成器的列表及其说明如下表所示:
| 序号 | 分词器和说明 | 
|---|---|
| 1 | 标准标记器 (standard) 这是基于基于语法的标记器构建的,max_token_length可以为这个标记器配置。 | 
| 2 | 边缘 NGram 标记器(edgeNGram) 像min_gram, max_gram, token_chars这样的设置可以为这个标记器设置。 | 
| 3 | 关键字标记器 (keyword) 这将生成整个输入作为输出,buffer_size可以为此设置。 | 
| 4 | 字母标记器(letter) 这将捕获整个单词,直到遇到一个非字母。 |