Elasticsearch Built-in analyzer reference 內置分析器

摘要

這些系統內置分析器,都是通過組合 tokenizertoken filter實現,它們之間由包含、重疊、互斥等關係,根據實際需求選擇你所需要的分析器,或是重新自定義分析器,以期達到最佳狀態。默認的標準分析器,能滿足大多數需求。

標準分析器 standard analyzer

默認分析器,根據Unicode文本分段算法的定義,分析器將文本劃分爲單詞邊界上的多個術語。它刪除大多數標點符號,小寫術語,並支持停用詞。

配置參數
參數 含義
max_token_length 最大令牌長度。如果看到令牌超過此長度,則將使用max_token_length間隔分割。默認爲255
stopwords ,預定義的停用詞列表,例如_english_或包含停用詞列表的數組。默認爲_none_
stopwords_path ,包含停用詞的文件的路徑。
配置示例

對比"max_token_length": 5"max_token_length": 4,輸出結果,體會參數含義

PUT my_index
{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_english_analyzer": {
          "type": "standard",
          "max_token_length": 5,
          "stopwords": "_english_"
        }
      }
    }
  }
}

POST my_index/_analyze
{
  "analyzer": "my_english_analyzer",
  "text": "The 2 QUICK Brown-Foxes jumped over the lazy dog's bone."
}
/*輸出*/
[ 2, quick, brown, foxes, jumpe, d, over, lazy, dog's, bone ]

/*如果max_token_length爲4*,則有以下輸出*/
[2, quic, k, brow, n, foxe, s, jump, ed, lazy, dog, s, bone]
定義

標準分析器包括:

  1. tokenizer:標準分詞器
  2. token filter:小寫標記分詞器,停用標記分詞器(默認情況下禁用)

如果需要在配置參數之外自定義分析器,這通常需要通過添加標記過濾器,將其重新創建爲分析器並進行修改。這將重新創建內置標準分析器,您可以將其用作起點:

PUT /standard_example
{
  "settings": {
    "analysis": {
      "analyzer": {
        "rebuilt_standard": {
          "tokenizer": "standard",
          "filter": [
            "lowercase",
            /*添加標記過濾器*/
            "custom token filter"      
          ]
        }
      }
    }
  }
}

簡單分析器 sample analyzer

以非字母字符,作爲分割器,將字符串分割爲單個術語。簡單分析器不需要配置參數。

POST _analyze
{
  "analyzer": "simple",
  "text": "The 2 QUICK Brown-Foxes jumped over the lazy dog's bone."
}

/*輸出*/
[ the, quick, brown, foxes, jumped, over, the, lazy, dog, s, bone ]
定義
  1. tokenizer:小寫分詞器

如果需要自定義分析器,這通常需要通過添加標記過濾器將其重新創建爲分析器並進行修改。這將重新創建內置分析器,您可以將其用作進一步定製的起點:

PUT /simple_example
{
  "settings": {
    "analysis": {
      "analyzer": {
        "rebuilt_simple": {
          "tokenizer": "lowercase",
          "filter": [     
          /*添加標記過濾器*/
            "custom token filter"      
          ]
        }
      }
    }
  }
}

空白字符分析器 whitespace analyzer

以任意空白字符作爲分隔符,將字符串分割爲單個術語。這個分析器不需要配置參數。

POST _analyze
{
  "analyzer": "whitespace",
  "text": "The 2 QUICK Brown-Foxes jumped over the lazy dog's bone."
}

/*輸出*/
[ The, 2, QUICK, Brown-Foxes, jumped, over, the, lazy, dog's, bone. ]
定義

tokenizer:空白字符分詞器

如果需要自定義分析器,這通常需要通過添加標記過濾器將其重新創建爲分析器並進行修改。這將重新創建內置分析器,您可以將其用作進一步定製的起點:

PUT /simple_example
{
  "settings": {
    "analysis": {
      "analyzer": {
        "rebuilt_whitespace": {
          "tokenizer": "whitespace",
          "filter": [     
          /*添加標記過濾器*/
            "custom token filter"      
          ]
        }
      }
    }
  }
}

停用分析器 stop analyzer

在簡單分析器的基礎上,添加了對停用字的支持,默認使用_english_停用字。

POST _analyze
{
  "analyzer": "stop",
  "text": "The 2 QUICK Brown-Foxes jumped over the lazy dog's bone."
}
/*輸出*/
[ quick, brown, foxes, jumped, over, lazy, dog, s, bone ]
配置參數
參數 含義
stopwords ,預定義的停用詞列表,例如_english_或包含停用詞列表的數組。默認爲_none_
stopwords_path ,包含停用詞的文件的路徑。
配置示例
PUT my_index
{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_stop_analyzer": {
          "type": "stop",
          "stopwords": ["the", "over"]
        }
      }
    }
  }
}

POST my_index/_analyze
{
  "analyzer": "my_stop_analyzer",
  "text": "The 2 QUICK Brown-Foxes jumped over the lazy dog's bone."
}
/*輸出*/
[ quick, brown, foxes, jumped, lazy, dog, s, bone ]
定義

標準分析器包括:

  1. tokenizer:標準分詞器
  2. token filter:停用標記分詞器

如果需要自定義分析器,這通常需要通過添加標記過濾器將其重新創建爲分析器並進行修改。這將重新創建內置分析器,您可以將其用作進一步定製的起點:

PUT /stop_example
{
  "settings": {
    "analysis": {
      "filter": {
        "english_stop": {
          "type":       "stop",
          "stopwords":  "_english_" 
        }
      },
      "analyzer": {
        "rebuilt_stop": {
          "tokenizer": "lowercase",
          "filter": [
            "english_stop",
            /*添加標記過濾器*/
            "custom token filter"             
          ]
        }
      }
    }
  }
}

關鍵詞分析器 Keyword Analyzer

將整個文本視爲一個術語

POST _analyze
{
  "analyzer": "keyword",
  "text": "The 2 QUICK Brown-Foxes jumped over the lazy dog's bone."
}

/*輸出*/
[ The 2 QUICK Brown-Foxes jumped over the lazy dog's bone. ]
定義

tokenizer:關鍵詞分詞器
如果需要自定義分析器,這通常需要通過添加標記過濾器將其重新創建爲分析器並進行修改。這將重新創建內置分析器,您可以將其用作進一步定製的起點:

PUT /keyword_example
{
  "settings": {
    "analysis": {
      "analyzer": {
        "rebuilt_keyword": {
          "tokenizer": "keyword",
          "filter": [         
          ]
        }
      }
    }
  }
}

正則表達式分析器 pattern analyzer

使用一個正則表達式,作爲分割字符串的算法,正則表達式應該是匹配標記的分隔符,而不是匹配標記本身,默認爲\W+,匹配任意非字母字符。

當心錯誤的正則表達式,模式分析器使用 Java正則表達式。編寫不正確的正則表達式可能會非常緩慢地運行,甚至拋出StackOverflowError並導致正在運行的節點突然退出。

POST _analyze
{
  "analyzer": "pattern",
  "text": "The 2 QUICK Brown-Foxes jumped over the lazy dog's bone."
}

/*輸出*/
[ the, 2, quick, brown, foxes, jumped, over, the, lazy, dog, s, bone ]
配置參數
參數 含義
pattern 一個Java的正則表達式,則默認爲\W+
flags Java正則表達式標誌。標記應以管道分隔。
lowercase ,術語是否小寫。默認爲true
stopwords ,預定義的停用詞列表,例如_english_或包含停用詞列表的數組。默認爲_none_
stopwords_path ,包含停用詞的文件的路徑。
配置示例

在此示例中,我們將分析器配置爲將電子郵件地址拆分爲非單詞字符或下劃線(\W|_),並小寫結果:

PUT my_index
{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_email_analyzer": {
          "type":      "pattern",
          "pattern":   "\\W|_", 
          "lowercase": true
        }
      }
    }
  }
}

POST my_index/_analyze
{
  "analyzer": "my_email_analyzer",
  "text": "[email protected]"
}
/*輸出*/
[ john, smith, foo, bar, com ]
更復雜的示例
PUT my_index
{
  "settings": {
    "analysis": {
      "analyzer": {
        "camel": {
          "type": "pattern",
          "pattern": "([^\\p{L}\\d]+)|(?<=\\D)(?=\\d)|(?<=\\d)(?=\\D)|(?<=[\\p{L}&&[^\\p{Lu}]])(?=\\p{Lu})|(?<=\\p{Lu})(?=\\p{Lu}[\\p{L}&&[^\\p{Lu}]])"
        }
      }
    }
  }
}

GET my_index/_analyze
{
  "analyzer": "camel",
  "text": "MooseX::FTPClass2_beta"
}

/*輸出*/
[ moose, x, ftp, class, 2, beta ]

解釋

 ([^\p{L}\d]+)                 # swallow non letters and numbers,
| (?<=\D)(?=\d)                 # or non-number followed by number,
| (?<=\d)(?=\D)                 # or number followed by non-number,
| (?<=[ \p{L} && [^\p{Lu}]])    # or lower case
  (?=\p{Lu})                    #   followed by upper case,
| (?<=\p{Lu})                   # or upper case
  (?=\p{Lu}                     #   followed by upper case
    [\p{L}&&[^\p{Lu}]]          #   then lower case
  )
定義

tokenizer:正則表達式分詞器
token filter:小寫標記分詞器,停用標記分詞器(默認情況下禁用)
如果需要自定義分析器,這通常需要通過添加標記過濾器將其重新創建爲分析器並進行修改。這將重新創建內置分析器,您可以將其用作進一步定製的起點:

PUT /pattern_example
{
  "settings": {
    "analysis": {
      "tokenizer": {
        "split_on_non_word": {
          "type":       "pattern",
          "pattern":    "\\W+" 
        }
      },
      "analyzer": {
        "rebuilt_pattern": {
          "tokenizer": "split_on_non_word",
          "filter": [
            "lowercase"       
          ]
        }
      }
    }
  }
}

語言分析器 language analyzer

一組旨在分析特定語言的分析器。將支持以下類型:arabic,armenian,basque,bengali,brazilian,bulgarian,catalan,cjk,czech,danish,dutch,english,estonian,finnish,french,galician,german,greek,hindi,hungarian,indonesian,irish,italian,latvian,lithuanian,norwegian,persian,portuguese,romanian,russian,sorani,spanish,swedish,turkish,thai.,

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章