当前位置: 首页 > article >正文

[学习笔记]IK分词器的学习

IK分词器有几种模式

# 测试分词器
POST /_analyze
{
  "text":"黑马程序员学习java太棒了",
  "analyzer": "standard"
}

在这里插入图片描述

# 测试分词器
POST /_analyze
{
  "text":"黑马程序员学习java太棒了",
  "analyzer": "ik_max_word"
}

在这里插入图片描述

# 测试分词器
POST /_analyze
{
  "text":"黑马程序员学习java太棒了",
  "analyzer": "ik_smart"
}

在这里插入图片描述

总结

ik_max_word最细切分, 占用内存多,但是分词多
ik_smart最少切分,占用内存少,但是分词少
standard中文没用

IK分词器如何拓展词条?如何停用词条

在这里插入图片描述
配置你的ext.dic和stopword.dic在这里插入图片描述
ext.dic在这里插入图片描述

mapping属性

在这里插入图片描述

文档操作

在这里插入图片描述
在这里插入图片描述

分词规则总结

在这里插入图片描述

CREATE TABLE `tb_hotel`  (
  `id` bigint(20) NOT NULL COMMENT '酒店id',
  `name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '酒店名称',
  `address` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '酒店地址',
  `price` int(10) NOT NULL COMMENT '酒店价格',
  `score` int(2) NOT NULL COMMENT '酒店评分',
  `brand` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '酒店品牌',
  `city` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '所在城市',
  `star_name` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '酒店星级,1星到5星,1钻到5钻',
  `business` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '商圈',
  `latitude` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '纬度',
  `longitude` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '经度',
  `pic` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '酒店图片',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Compact;
# 酒店的mapping
PUT /hotel
{
  "mappings": {
      "properties": {
        "id":{
          "type": "keyword"
        },
        "name":{
          "type": "text",
          "analyzer": "ik_max_word",
          "copy_to": "all"
        },
        "address":{
          "type":"keyword",
          "index": false
        },
        "price":{
          "type":"integer"
        },
        "score":{
          "type":"integer"
        },
        "brand":{
          "type": "keyword"
        },
        "city":{
          "type":"keyword"
        },
        "star_name":{
          "type": "keyword"
        },
        "business":{
          "type": "keyword",
          "copy_to": "all"
        },
        "location":{
          "type":"geo_point"
        },
        "pic":{
          "type": "keyword",
          "index": false
        },
        "all":{
          "type": "text",
          "analyzer": "ik_max_word"
        }
      }
  }

Java的测试代码

package cn.itcast.hotel.constants;

public class HotelIndexConstants {
    public static final String MAPPING_TEMPLATE = "{\n" +
            "  \"mappings\": {\n" +
            "      \"properties\": {\n" +
            "        \"id\":{\n" +
            "          \"type\": \"keyword\"\n" +
            "        },\n" +
            "        \"name\":{\n" +
            "          \"type\": \"text\",\n" +
            "          \"analyzer\": \"ik_max_word\",\n" +
            "          \"copy_to\": \"all\"\n" +
            "        },\n" +
            "        \"address\":{\n" +
            "          \"type\":\"keyword\",\n" +
            "          \"index\": false\n" +
            "        },\n" +
            "        \"price\":{\n" +
            "          \"type\":\"integer\"\n" +
            "        },\n" +
            "        \"score\":{\n" +
            "          \"type\":\"integer\"\n" +
            "        },\n" +
            "        \"brand\":{\n" +
            "          \"type\": \"keyword\"\n" +
            "        },\n" +
            "        \"city\":{\n" +
            "          \"type\":\"keyword\"\n" +
            "        },\n" +
            "        \"star_name\":{\n" +
            "          \"type\": \"keyword\"\n" +
            "        },\n" +
            "        \"business\":{\n" +
            "          \"type\": \"keyword\",\n" +
            "          \"copy_to\": \"all\"\n" +
            "        },\n" +
            "        \"location\":{\n" +
            "          \"type\":\"geo_point\"\n" +
            "        },\n" +
            "        \"pic\":{\n" +
            "          \"type\": \"keyword\",\n" +
            "          \"index\": false\n" +
            "        },\n" +
            "        \"all\":{\n" +
            "          \"type\": \"text\",\n" +
            "          \"analyzer\": \"ik_max_word\"\n" +
            "        }\n" +
            "      }\n" +
            "  }\n" +
            "  ";
}

package cn.itcast.hotel;

import cn.itcast.hotel.pojo.Hotel;
import cn.itcast.hotel.pojo.HotelDoc;
import cn.itcast.hotel.service.impl.HotelService;
import com.alibaba.fastjson.JSON;
import org.apache.http.HttpHost;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;

import static cn.itcast.hotel.constants.HotelIndexConstants.MAPPING_TEMPLATE;

/**
 * @author lst
 * @date 2023年11月23日 13:38
 */
@SpringBootTest
public class HotelT {
    private RestHighLevelClient restHighLevelClient;
    @Autowired
    HotelService hotelService;

    @BeforeEach
    public void before() {
        restHighLevelClient = new RestHighLevelClient(RestClient.builder(HttpHost.create("http://localhost:9200")));
    }

    @AfterEach
    void tearDown() throws IOException {
        restHighLevelClient.close();
    }

    @Test
    public void testCreateIndex() throws IOException {
        CreateIndexRequest request = new CreateIndexRequest("hotel");
        request.source(MAPPING_TEMPLATE, XContentType.JSON);
        restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);
    }

    @Test
    public void testDeleteIndex() throws IOException {
        DeleteIndexRequest request = new DeleteIndexRequest("hotel");
        restHighLevelClient.indices().delete(request, RequestOptions.DEFAULT);
    }
}

http://www.kler.cn/a/147618.html

相关文章:

  • Flink Source 详解
  • 【星海随笔】ZooKeeper-Mesos
  • 探索IDE的无限可能:使用技巧与插件推荐
  • 学习rust语言宏之macro_rules!
  • Python期末复习 | 列表、元组、字典、集合与字符串 | 代码演示
  • 【WPF】Prism学习(二)
  • 探究Kafka原理-7.exactly once semantics 和 性能测试
  • Python批量裁剪图像尺寸、压缩图像大小代码实现
  • 阿里云对象存储oss-文件上传过程详解(两种方式)
  • 简易版扫雷+代码分析
  • ElasticSearch的日志配置
  • 十分钟搭建VScode C/C++运行环境
  • 03、K-means聚类实现步骤与基于K-means聚类的图像压缩(1)
  • Python基础语法之学习数据转换
  • React Native 源码分析(五)—— Fabric创建View的过程
  • 复习 学习 多看
  • leetcode9.回文数
  • java Swing UI设置统一字体大小
  • 一起学docker系列之十docker安装tomcat实践
  • leetCode 226.翻转二叉树
  • MFC—CTabCtrl 、CListCtrl
  • k8s docker总结特殊点
  • 【Jmeter】什么是BeanShell?
  • ACE 2005,LDC2006T06数据集获取
  • 2023人形机器人行业海外科技研究:从谷歌看机器人大模型进展
  • Doris_Doris导入常见问题