停用词分词器

停用词分词器 #

停用词(stop)分词器会在文本中移除预定义的停用词。该分词器由一个小写词元生成器和一个停用词词元过滤器组成。

参数说明 #

你可以使用以下参数来配置一个停用词分词器。

参数必填/可选数据类型描述
stopwords可选字符串或字符串列表一个包含自定义停用词列表(如 _english)的字符串,或者一个自定义停用词列表的数组。默认值为 _none_
stopwords_path可选字符串包含停用词列表的文件的路径(相对于配置目录的绝对路径或相对路径)。

参考样例 #

以下命令创建一个名为 my_stop_index 并使用停词器分词器的索引:

PUT /my_stop_index
{
  "mappings": {
    "properties": {
      "my_field": {
        "type": "text",
        "analyzer": "stop"
      }
    }
  }
}

配置自定义分词器 #

以下命令来配置一个自定义分词器的索引,该自定义分词器的作用等同于停用词分词器:

PUT /my_custom_stop_analyzer_index
{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_custom_stop_analyzer": {
          "tokenizer": "lowercase",
          "filter": [
            "stop"
          ]
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "my_field": {
        "type": "text",
        "analyzer": "my_custom_stop_analyzer"
      }
    }
  }
}

产生的词元 #

以下请求用来检查分词器生成的词元:

POST /my_custom_stop_analyzer_index/_analyze
{
  "analyzer": "my_custom_stop_analyzer",
  "text": "The large turtle is green and brown"
}

返回内容中包含了产生的词元

{
  "tokens": [
    {
      "token": "large",
      "start_offset": 4,
      "end_offset": 9,
      "type": "word",
      "position": 1
    },
    {
      "token": "turtle",
      "start_offset": 10,
      "end_offset": 16,
      "type": "word",
      "position": 2
    },
    {
      "token": "green",
      "start_offset": 20,
      "end_offset": 25,
      "type": "word",
      "position": 4
    },
    {
      "token": "brown",
      "start_offset": 30,
      "end_offset": 35,
      "type": "word",
      "position": 6
    }
  ]
}

指定停用词 #

下面演示了怎么去指定一个自定义的停用词列表:

PUT /my_new_custom_stop_index
{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_custom_stop_analyzer": {
          "type": "stop",
          "stopwords_path": "stopwords.txt"
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "description": {
        "type": "text",
        "analyzer": "my_custom_stop_analyzer"
      }
    }
  }
}

在这个例子中,该文件位于配置目录中。你也可以指定该文件的完整路径。