Elasticsearch:按类型删除分词

文章介绍了如何在Elasticsearch中使用Keep类型分词过滤器来保留或删除特定类型的分词,如排除数字或ALPHANUM类型,以优化文本分析过程。通过示例展示了设置exclude和include模式来控制过滤行为。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在我之前的文章 “Elasticsearch:分词器中的 token 过滤器使用示例”,我有很多示例展示如何使用分词器中的过滤器来对分词进行过滤。在今天的文章中,我将展示如何使用另外一种过滤器根据类型来保留或者移除一些分词。

保留类型分词过滤器能够跨类型保留或删除分词。 让我们想象一下项目描述字段,通常这个字段接收带有单词和数字的文本。 为所有文本生成分词可能没有意义,为了避免这种情况,我们将使用 Keep 类型分词过滤器。

删除数字标记

要删除数字类型,请将 “types” 参数设置为 “<NUM>”,此参数接受一个标记列表。 “mode” 参数设置为 “exclude”。

例子:

GET _analyze
{
  "tokenizer": "standard",
  "filter": [
    {
      "type": "keep_types",
      "types": [ "<NUM>" ],
      "mode": "exclude"
    },
    {
      "type": "stop"
    }
  ],
  "text": "The German philosopher and economist Karl Marx was born on May 5, 1818."
}

上面命令返回的分词为:

{
  "tokens": [
    {
      "token": "The",
      "start_offset": 0,
      "end_offset": 3,
      "type": "<ALPHANUM>",
      "position": 0
    },
    {
      "token": "German",
      "start_offset": 4,
      "end_offset": 10,
      "type": "<ALPHANUM>",
      "position": 1
    },
    {
      "token": "philosopher",
      "start_offset": 11,
      "end_offset": 22,
      "type": "<ALPHANUM>",
      "position": 2
    },
    {
      "token": "economist",
      "start_offset": 27,
      "end_offset": 36,
      "type": "<ALPHANUM>",
      "position": 4
    },
    {
      "token": "Karl",
      "start_offset": 37,
      "end_offset": 41,
      "type": "<ALPHANUM>",
      "position": 5
    },
    {
      "token": "Marx",
      "start_offset": 42,
      "end_offset": 46,
      "type": "<ALPHANUM>",
      "position": 6
    },
    {
      "token": "born",
      "start_offset": 51,
      "end_offset": 55,
      "type": "<ALPHANUM>",
      "position": 8
    },
    {
      "token": "May",
      "start_offset": 59,
      "end_offset": 62,
      "type": "<ALPHANUM>",
      "position": 10
    }
  ]
}

从上面的输出中,我们可以看出来所以的数字分词都被移除了。

我们也可以尝试使用如下的命令来保留数字:

GET _analyze
{
  "tokenizer": "standard",
  "filter": [
    {
      "type": "keep_types",
      "types": [ "<NUM>" ],
      "mode": "include"
    },
    {
      "type": "stop"
    }
  ],
  "text": "The German philosopher and economist Karl Marx was born on May 5, 1818."
}

上面的分词为:

{
  "tokens": [
    {
      "token": "5",
      "start_offset": 63,
      "end_offset": 64,
      "type": "<NUM>",
      "position": 11
    },
    {
      "token": "1818",
      "start_offset": 66,
      "end_offset": 70,
      "type": "<NUM>",
      "position": 12
    }
  ]
}

删除 aphanumeric 分词

要删除文本,我们只需将 “types” 字段设置为“<ALPHANUMERIC>”。

GET _analyze
{
  "tokenizer": "standard",
  "filter": [
    {
      "type": "keep_types",
      "types": [ "<ALPHANUM>" ],
      "mode": "exclude"
    },
    {
      "type": "stop"
    }
  ],
  "text": "The German philosopher and economist Karl Marx was born on May 5, 1818."
}

现在我们只有数字分词。

{
  "tokens": [
    {
      "token": "5",
      "start_offset": 63,
      "end_offset": 64,
      "type": "<NUM>",
      "position": 11
    },
    {
      "token": "1818",
      "start_offset": 66,
      "end_offset": 70,
      "type": "<NUM>",
      "position": 12
    }
  ]
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值