Elasticsearch分词器--空格分词器(whitespace analyzer)

介绍

文本分析,是将全文本转换为一系列单词的过程,也叫分词。analysis是通过analyzer(分词器)来实现的,可以使用Elasticearch内置的分词器,也可以自己去定制一些分词器。除了在数据写入时将词条进行转换,那么在查询的时候也需要使用相同的分析器对语句进行分析。

|---------------------|--------------------------|
| 分词器名称 | 处理过程 |
| Standard Analyzer | 默认的分词器,按词切分,小写处理 |
| Simple Analyzer | 按照非字母切分(符号被过滤),小写处理 |
| Stop Analyzer | 小写处理,停用词过滤(the, a, this) |
| Whitespace Analyzer | 按照空格切分,不转小写 |
| Keyword Analyzer | 不分词,直接将输入当做输出 |
| Pattern Analyzer | 正则表达式,默认是\W+(非字符串分隔) |

实战

1、空格分词器展示

POST:http://localhost:9200/_analyze/

复制代码
{
  "analyzer": "whitespace",
  "text": "hello this my white space analyzer"
}

结果:按照空格进行分词处理

复制代码
{
    "tokens":[
        {
            "token":"hello",
            "start_offset":0,
            "end_offset":5,
            "type":"word",
            "position":0
        },
        {
            "token":"this",
            "start_offset":6,
            "end_offset":10,
            "type":"word",
            "position":1
        },
        {
            "token":"my",
            "start_offset":11,
            "end_offset":13,
            "type":"word",
            "position":2
        },
        {
            "token":"white",
            "start_offset":14,
            "end_offset":19,
            "type":"word",
            "position":3
        },
        {
            "token":"space",
            "start_offset":20,
            "end_offset":25,
            "type":"word",
            "position":4
        },
        {
            "token":"analyzer",
            "start_offset":26,
            "end_offset":34,
            "type":"word",
            "position":5
        }
    ]
}

2、空格分词器创建与查询

目前我们有一些应用场景需要根据空格分词之后的内容进行精准查询,这样空格分词器就满足我们的需求了。

(1)创建索引,针对想要分词的字段指定空格分词器

whitespace_analyzer_1:指定为whitespace

content:指定为空格分词器

复制代码
{
  "settings": {
    "analysis": {
      "analyzer": {
        "whitespace_analyzer_1": {
          "type": "whitespace"
        }
      }
    }
  },
  "mappings": {
    "_doc": {
      "properties": {
        "id": {
          "type": "keyword"
        },
        "title": {
          "type": "text"
        },
        "content": {
          "type": "text",
          "analyzer": "whitespace_analyzer_1"
        }
      }
    }
  }
}

(2)索引查询

保存一条数据:

复制代码
{
  "id": "002",
  "title": "科目2",
  "content": "this is whitespace"
}

根据分词查询:

复制代码
{
  "query": {
    "match": {
      "desc": "this"
    }
  }
}

根据不存在的分词查询则查询不到

复制代码
{
  "query": {
    "match": {
      "desc": "that"
    }
  }
}
相关推荐
Biehmltym5 小时前
【AI】09AI Agent LLM → Streaming → Session 记录 的完整链路
大数据·人工智能·elasticsearch
小湘西6 小时前
Elasticsearch 的一些默认配置上下限
java·大数据·elasticsearch
Dxy12393102169 小时前
Elasticsearch 8如何做好标题搜索
大数据·elasticsearch
斯普信云原生组10 小时前
Elasticsearch(ES) 内存 CPU 过高问题排查报告
大数据·elasticsearch·搜索引擎
弘毅 失败的 mian10 小时前
Git 分支管理
大数据·经验分享·笔记·git·elasticsearch
阿坤带你走近大数据11 小时前
Elasticsearch(ES)的基本概念、架构及基本使用介绍
大数据·elasticsearch
Elastic 中国社区官方博客11 小时前
使用 Elasticsearch 中的结构化输出创建可靠的 agents
大数据·人工智能·elk·elasticsearch·搜索引擎·ai·全文检索
G皮T12 小时前
【Elasticsearch】查询性能调优(六):track_total_hits 影响返回结果的相关性排序吗
大数据·数据库·elasticsearch·搜索引擎·全文检索·性能·opensearch
imbackneverdie13 小时前
AI赋能下的下一代检索工具:DeepSearch与传统数据库/搜索引擎有何本质不同?
人工智能·搜索引擎·ai·自然语言处理·aigc·ai写作·ai工具
LCG米13 小时前
嵌入式Linux系统构建:为STM32MP157移植Buildroot并开发温湿度采集驱动
linux·stm32·elasticsearch