配置语言分析器

语言分析器都不需要任何配置,开箱即用,它们中的大多数都允许你控制它们的各方面行为,具体来说:

词干提取排除

想象下某个场景,用户们想要搜索 World Health Organization 的结果,但是却被替换为搜索 organ health 的结果。有这个困惑是因为 organorganization 有相同的词根: organ 。通常这不是什么问题,但是在一些特殊的文档中就会导致有歧义的结果,所以我们希望防止单词 organizationorganizations 被缩减为词干。

自定义停用词

英语中默认的停用词列表如下:

  1. a, an, and, are, as, at, be, but, by, for, if, in, into, is, it,
  2. no, not, of, on, or, such, that, the, their, then, there, these,
  3. they, this, to, was, will, with

关于单词 nonot 有点特别,这俩词会反转跟在它们后面的词汇的含义。或许我们应该认为这两个词很重要,不应该把他们看成停用词。

为了自定义 english (英语)分词器的行为,我们需要基于 english (英语)分析器创建一个自定义分析器,然后添加一些配置:

  1. PUT /my_index
  2. {
  3. "settings": {
  4. "analysis": {
  5. "analyzer": {
  6. "my_english": {
  7. "type": "english",
  8. "stem_exclusion": [ "organization", "organizations" ], (1)
  9. "stopwords": [ (2)
  10. "a", "an", "and", "are", "as", "at", "be", "but", "by", "for",
  11. "if", "in", "into", "is", "it", "of", "on", "or", "such", "that",
  12. "the", "their", "then", "there", "these", "they", "this", "to",
  13. "was", "will", "with"
  14. ]
  15. }
  16. }
  17. }
  18. }
  19. }
  20. GET /my_index/_analyze?analyzer=my_english (3)
  21. The World Health Organization does not sell organs.

<1> 防止 organizationorganizations 被缩减为词干

<2> 指定一个自定义停用词列表

<3> 切词为 worldhealthorganizationdoesnotsellorgan

我们在 stemmingstopwords 中分别详细讨论了词干提取和停用词。