[学习笔记]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;
elasticsearch 复制代码
# 酒店的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的测试代码

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" +
            "  ";
}
java 复制代码
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);
    }
}
相关推荐
Nu11PointerException7 分钟前
JAVA笔记 | ResponseBodyEmitter等异步流式接口快速学习
笔记·学习
亦枫Leonlew2 小时前
三维测量与建模笔记 - 3.3 张正友标定法
笔记·相机标定·三维重建·张正友标定法
考试宝2 小时前
国家宠物美容师职业技能等级评价(高级)理论考试题
经验分享·笔记·职场和发展·学习方法·业界资讯·宠物
黑叶白树4 小时前
简单的签到程序 python笔记
笔记·python
@小博的博客4 小时前
C++初阶学习第十弹——深入讲解vector的迭代器失效
数据结构·c++·学习
幸运超级加倍~4 小时前
软件设计师-上午题-15 计算机网络(5分)
笔记·计算机网络
南宫生4 小时前
贪心算法习题其四【力扣】【算法学习day.21】
学习·算法·leetcode·链表·贪心算法
懒惰才能让科技进步5 小时前
从零学习大模型(十二)-----基于梯度的重要性剪枝(Gradient-based Pruning)
人工智能·深度学习·学习·算法·chatgpt·transformer·剪枝
love_and_hope5 小时前
Pytorch学习--神经网络--搭建小实战(手撕CIFAR 10 model structure)和 Sequential 的使用
人工智能·pytorch·python·深度学习·学习
Chef_Chen5 小时前
从0开始学习机器学习--Day14--如何优化神经网络的代价函数
神经网络·学习·机器学习