999 - Elasticsearch Analysis 01

2019-05-07  本文已影响0人  歌哥居士

简介

Analysis在创建索引或搜索时指定,将文本解析成倒排索引,以供搜索。

简单解释下倒排索引:通常书本的目录就是正排索引,根据目录找章节;而倒排索引就是根据内容(例如一个单词)找所在的章节。

Analyzer

analyzer,内置或自定义,是一个包含三个构建块(依次为character filterstokenizerstoken filters)的包。

例如:一个内置的english analyzer在转换句子The QUICK brown foxes jumped over the lazy dog!时:

  1. 转为小写。
  2. 删除对搜索无意义的词元:the(停止词,stopwords)。
  3. 归纳词干,例如:foxes → fox,jumped → jump,lazy → lazi。
  4. 最后添加这些条件到倒排索引中:[quick, brown, fox, jump, over, lazi, dog]。
  5. 当查询时,可以查询"a query fox",虽然该字符串不在原文本中,但当使用了同样的analyzer时,该句子中的单词与倒排索引中的词元可以匹配上。

Character filters

Tokenizer

Token filters

测试analyzers

可以在请求体中指定analyzer(或者tokenizer、token filter、Character Filter),

GET _analyze
{
  "analyzer": "whitespace", 
  "text": "The quick brown fox."
}

GET _analyze
{
  "analyzer": "english", 
  "text": "The QUICK brown foxes jumped over the lazy dog!"
}

GET _analyze
{
  "tokenizer": "whitespace", 
  "filter": ["lowercase","stop"], 
  "text": "The QUICK brown foxes jumped over the lazy dog!"
}

从上面第二个的返回结果中可以看到位置和起始结束字符偏移量

{
  "tokens" : [
    {
      "token" : "quick",
      "start_offset" : 4,
      "end_offset" : 9,
      "type" : "<ALPHANUM>",
      "position" : 1
    },
    {
      "token" : "brown",
      "start_offset" : 10,
      "end_offset" : 15,
      "type" : "<ALPHANUM>",
      "position" : 2
    },
    {
      "token" : "fox",
      "start_offset" : 16,
      "end_offset" : 21,
      "type" : "<ALPHANUM>",
      "position" : 3
    },
    {
      "token" : "jump",
      "start_offset" : 22,
      "end_offset" : 28,
      "type" : "<ALPHANUM>",
      "position" : 4
    },
    {
      "token" : "over",
      "start_offset" : 29,
      "end_offset" : 33,
      "type" : "<ALPHANUM>",
      "position" : 5
    },
    {
      "token" : "lazi",
      "start_offset" : 38,
      "end_offset" : 42,
      "type" : "<ALPHANUM>",
      "position" : 7
    },
    {
      "token" : "dog",
      "start_offset" : 43,
      "end_offset" : 46,
      "type" : "<ALPHANUM>",
      "position" : 8
    }
  ]
}
上一篇 下一篇

猜你喜欢

热点阅读