Elasticsearch

Elasticsearch

ES是一个可以实现复杂搜索的全文检索服务器

一、安装

1.安装Elasticsearch

指定分配521m内存,可以改为256m,安装完成访问9200端口测试

docker run -d \
  --name es \
  -e "ES_JAVA_OPTS=-Xms512m -Xmx512m" \
  -e "discovery.type=single-node" \
  -v es-data:/usr/share/elasticsearch/data \
  -v es-plugins:/usr/share/elasticsearch/plugins \
  --privileged \
  --network work-net \
  -p 9200:9200 \
  -p 9300:9300 \
  elasticsearch:7.12.1

2.安装Kibana

图形化展示Elasticsearch内容,提供开发控制台对其接口进行语法提示,访问5601端口进行测试,第一次打开选择Explore on my own -> Dev tools

docker run -d \
--name kibana \
-e ELASTICSEARCH_HOSTS=http://es:9200 \
--network=hm-net \
-p 5601:5601  \
kibana:7.12.1

3.安装IK分词器

提供对文档分词的功能

docker exec -it es ./bin/elasticsearch-plugin  install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.12.1/elasticsearch-analysis-ik-7.12.1.zip

安装完成后重启Elasticsearch容器

docker restart es

二、使用

1.模糊查询关键字时使用倒排索引

在查询之前,向Elasticsearch发出创建索引,analyzer 属性用于指定在索引和搜索时如何分析文本字段,index 属性用于控制字段是否被索引,properties type为object时会用到,该字段的子字段

PUT /索引库名称
{
  "mappings": {
    "properties": {
      "字段名":{
        "type": "text",
        "analyzer": "ik_smart"
      },
      "字段名2":{
        "type": "keyword",
        "index": "false"
      },
      "字段名3":{
        "properties": {
          "子字段": {
            "type": "keyword"
          }
        }
      },
      // ...略
    }
  }
}

2.生成倒排索引以及原理

当创建倒排所以后,Elasticsearch会按照指定的是否索引以及分词规则对需要被索引的字段分成词条,随后按照词条去搜索其出现的位置,记录下来生成一张表,如下

词条 出现位置
小米 1,3,5,7
苹果 2,4,6
三星 8,9

以后,当用户发出模糊查询时,将模糊查询的语句分成词条,去表中查找出现位置,再按照出现位置去表中正向搜索,返回数据

三、索引的操作

新增已在上方,下面只写改删查

1.改

索引不支持修改已经填写的数据,只能新增

PUT /索引库名/_mapping
{
  "properties": {
    "新字段名":{
      "type": "integer"
    }
  }
}

2.删、查

DELETE /索引库名
GET /索引库名

四、IK分词器的使用

Elasticsearch官方默认分词器会将中文分成一个一个字,因此使用IK分词器,ik分词器包含两种模式

  • ik_smart:只能语义切分
  • is_max_word:最细粒度切分

添加网络新词,进入IK分词器内的config下,修改IKAnalyzer.cfg.xml内容

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
        <comment>IK Analyzer 扩展配置</comment>
        <!--用户可以在这里配置自己的扩展字典 *** 添加扩展词典-->
        <entry key="ext_dict">ext.dic</entry>
</properties>

在config下新建ext.dic,在内部输入新词

泰酷辣
蜜雪冰城

重启Elasticsearch

docker restart es

五、JavaAPI的使用

1.导入依赖

<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>elasticsearch-rest-high-level-client</artifactId>
</dependency>

因为SpringBoot默认的ES版本是7.17.10,所以我们需要覆盖默认的ES版本

  <properties>
      <maven.compiler.source>11</maven.compiler.source>
      <maven.compiler.target>11</maven.compiler.target>
      <elasticsearch.version>7.12.1</elasticsearch.version> <!-- 修改此处-->
  </properties>

2.初始化RestHighLevelClient

新建配置类,将Kibana生成索引的内容粘贴过来,并初始化RestHighLevelClient

@Configuration
public class EsConfig {

    public static final String HOST = "http://121.43.129.12:9200";

    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" +
            "      },\n" +
            "      \"price\":{\n" +
            "        \"type\": \"integer\"\n" +
            "      },\n" +
            "      \"stock\":{\n" +
            "        \"type\": \"integer\"\n" +
            "      },\n" +
            "      \"image\":{\n" +
            "        \"type\": \"keyword\",\n" +
            "        \"index\": false\n" +
            "      },\n" +
            "      \"category\":{\n" +
            "        \"type\": \"keyword\"\n" +
            "      },\n" +
            "      \"brand\":{\n" +
            "        \"type\": \"keyword\"\n" +
            "      },\n" +
            "      \"sold\":{\n" +
            "        \"type\": \"integer\"\n" +
            "      },\n" +
            "      \"commentCount\":{\n" +
            "        \"type\": \"integer\",\n" +
            "        \"index\": false\n" +
            "      },\n" +
            "      \"isAD\":{\n" +
            "        \"type\": \"boolean\"\n" +
            "      },\n" +
            "      \"updateTime\":{\n" +
            "        \"type\": \"date\"\n" +
            "      }\n" +
            "    }\n" +
            "  }\n" +
            "}";

    @Bean
    public RestHighLevelClient client() {
        return new RestHighLevelClient(
                RestClient.builder(HttpHost.create(HOST))
        );
    }

}

3.创建索引

    @Autowired
    private RestHighLevelClient client;

    @Test
    public void test() throws IOException {
        CreateIndexRequest request = new CreateIndexRequest("items");
        request.source(EsConfig.MAPPING_TEMPLATE, XContentType.JSON);
        CreateIndexResponse createIndexResponse = client.indices().create(request, RequestOptions.DEFAULT);
        log.info("isOK?:{}",createIndexResponse.isAcknowledged());
    }
posted @   超级大菠萝面包  阅读(22)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示