ElasticSearch7.10-分词器

文章目录

分词器

1.字符过滤器

1.介绍
2.过滤html标签
3.mappings过滤规则(屏蔽非文明用语)
4.正则替换

2.自定义分词器

1.代码
java 复制代码
PUT /test_index
{
  "settings": {
    "analysis": { 
      "char_filter": { 
        "my_char_filter": { 
          "type": "html_strip" // 自定义字符过滤器,使用 html_strip 类型,剥离 HTML 标签
        }
      },
      "filter": { 
        "my_stopword": { 
          "type": "stop", // 自定义过滤器,移除停用词
          "stopwords": ["is", "a"] // 指定停用词列表,包括 "is" 和 "a"
        }
      }, 
      "tokenizer": { 
        "my_tokenizer": { 
          "type": "pattern", // 自定义分词器,基于正则表达式进行分词
          "pattern": "[,\\.?\\s]+" // 正则表达式,将逗号、句号、问号和空格作为分隔符
        }
      }, 
      "analyzer": { 
        "my_analysis": { 
          "type": "custom", // 自定义分析器类型
          "char_filter": ["my_char_filter"], // 使用自定义字符过滤器 "my_char_filter"
          "tokenizer": "my_tokenizer", // 使用自定义分词器 "my_tokenizer"
          "filter": ["my_stopword"] // 使用自定义停用词过滤器 "my_stopword"
        }
      }
    }
  }
}
2.查询
java 复制代码
GET /test_index/_analyze
{
  "analyzer": "my_analysis",
  "text": "<p>This is a test, isn't it amazing?</p>"
}

3.中文分词器

1.下载ik分词器7.10.0版本(跟es对应)

https://release.infinilabs.com/analysis-ik/stable/

2.应用ik分词器
1.进入插件目录下创建一个ik目录
sh 复制代码
cd /usr/local/ElasticSearch/elasticsearch-7.10.0/plugins && mkdir ik
2.将下载的压缩包上传上去
sh 复制代码
cd ik
3.安装unzip并解压
1.安装
sh 复制代码
yum install unzip
2.解压
sh 复制代码
unzip elasticsearch-analysis-ik-7.10.0.zip
3.删除zip
sh 复制代码
rm -f elasticsearch-analysis-ik-7.10.0.zip
4.将这个ik目录分别复制给另外三台节点
sh 复制代码
cp -r /usr/local/ElasticSearch/elasticsearch-7.10.0/plugins/ik /usr/local/ElasticSearch/node2/plugins && cp -r /usr/local/ElasticSearch/elasticsearch-7.10.0/plugins/ik /usr/local/ElasticSearch/node3/plugins
4.重启es
1.首先查找到es,然后kill -9
sh 复制代码
ps -aux | grep elasticsearch
2.重新启动三个节点
sh 复制代码
su elasticsearch
sh 复制代码
cd /usr/local/ElasticSearch/elasticsearch-7.10.0/bin/ && ./elasticsearch -d && cd /usr/local/ElasticSearch/node2/bin/ && ./elasticsearch -d && cd /usr/local/ElasticSearch/node3/bin/ && ./elasticsearch -d
3.使用head插件查看状态
3.测试ik分词器是否生效
1.创建索引并应用ik分词器
java 复制代码
PUT /test_index
{
  "mappings": {
    "properties": {
      "title": {
        "type": "text",
        "analyzer": "ik_smart"           
      },
      "description": {
        "type": "text",
        "analyzer": "ik_max_word"       
      }
    }
  }
}
2.插入数据
java 复制代码
POST /test_index/_doc
{
  "title": "Elasticsearch 是一个分布式搜索引擎",
  "description": "IK 分词器支持中文分词,并且可以用于全文检索"
}
3.查看分词结果
java 复制代码
POST /test_index/_analyze
{
  "field": "title",
  "text": "Elasticsearch 是一个分布式搜索引擎"
}
java 复制代码
POST /test_index/_analyze
{
  "field": "description",
  "text": "IK 分词器支持中文分词,并且可以用于全文检索"
}
4.ik分词器的配置文件

索引的批量操作

1.基于mget的批量查询

1.环境搭建
java 复制代码
PUT /new_index
{
  "mappings": {
    "properties": {
      "name": {
        "type": "text"
      },
      "age": {
        "type": "integer"
      }
    }
  }
}
java 复制代码
POST /new_index/_doc/1
{
  "name": "Alice",
  "age": 25
}

POST /new_index/_doc/2
{
  "name": "Bob",
  "age": 30
}

POST /new_index/_doc/3
{
  "name": "Charlie",
  "age": 22
}
2.根据id进行批量查询
java 复制代码
GET /new_index/_mget
{
  "ids": [1,2]
}
3.还可以在批量查询时指定自己想要的字段
java 复制代码
GET /new_index/_mget
{
  "docs": [
    {
      "_id": 1,
      "_source": {
        "include": [
          "name"
        ]
      }
    },
    {
      "_id": 2,
      "_source": {
        "exclude": [
          "name"
        ]
      }
    }
  ]
}

2.文档的四种操作类型

1.介绍
2._create 创建数据
3.删除数据
5._update 更新数据

3._bulk

模糊搜索和智能搜索推荐

1.基本介绍

2.前缀搜索

1.基础数据
java 复制代码
#prefix: 前缀搜索
POST /my_index/_bulk?filter_path=items.*.error
{ "index": { "_id": "1" } }
{ "text": "城管打电话喊商贩去摆摊摊" }
{ "index": { "_id": "2" } }
{ "text": "笑果文化回应老农去摆摊" }
{ "index": { "_id": "3" } }
{ "text": "老农耗时17年种出椅子树" }
{ "index": { "_id": "4" } }
{ "text": "夫妻结婚30多年AA制,被城管抓" }
{ "index": { "_id": "5" } }
{ "text": "黑人见义勇为阻止抢劫反被铐住" }
2.代码
java 复制代码
{
  "query": {
    "prefix": {
      "text": {
        "value": "笑"
      }
    }
  }
}
3.原理

前缀匹配是对倒排索引进行匹配的,而不是整个字段

3.通配符搜索

1.基本介绍

通配符也是匹配的倒排索引

2.代码

4.正则匹配

5.模糊查询

1.介绍
2.代码

注意:fuzzy是不分词的,match是分词的

6.短语前缀

1.介绍
2.代码

7.edge_ngram

java 复制代码
PUT /my_index
{
  "settings": {
    "analysis": {
      "filter": {
        "my_edge_ngram": {
          "type": "edge_ngram",
          "min_gram": 2,
          "max_gram": 5
        }
      },
      "analyzer": {
        "my_ngram_analyzer": {
          "type": "custom",
          "tokenizer": "standard",
          "filter": [
            "lowercase",
            "my_edge_ngram"
          ]
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "text": {
        "type": "text",
        "analyzer": "my_ngram_analyzer",
        "search_analyzer": "standard"
      }
    }
  }
}
相关推荐
专注API从业者8 分钟前
基于 Flink 的淘宝实时数据管道设计:商品详情流式处理与异构存储
大数据·前端·数据库·数据挖掘·flink
小猿姐1 小时前
KubeBlocks for Milvus 揭秘
数据库·云原生
AI 嗯啦1 小时前
SQL详细语法教程(四)约束和多表查询
数据库·人工智能·sql
杜子不疼.1 小时前
《Python学习之文件操作:从入门到精通》
数据库·python·学习
TDengine (老段)2 小时前
TDengine IDMP 高级功能(4. 元素引用)
大数据·数据库·人工智能·物联网·数据分析·时序数据库·tdengine
DashVector2 小时前
如何通过Java SDK分组检索Doc
java·数据库·面试
Olrookie3 小时前
XXL-JOB GLUE模式动态数据源实践:Spring AOP + MyBatis 解耦多库查询
java·数据库·spring boot
苏婳6663 小时前
【最新版】怎么下载mysqlclient并成功安装?
数据库·python·mysql
Tapdata5 小时前
《实时分析市场报告 2025》上线 | 从批处理到实时洞察,2025 年全球实时分析市场全景解读
数据库
海梨花5 小时前
【从零开始学习Redis】项目实战-黑马点评D2
java·数据库·redis·后端·缓存