文章目录

  • SpringBoot集成elasticsearch系列文章目录
  • 前言
  • 一、准备工作
  • 二、使用步骤
  • 1.引入依赖
  • 2.yml配置es集群
  • 3.简单Test
  • 3.1 创建索引以及分片设置
  • 3.2 创建索引库并设置mapping信息
  • 3.3删除索引库
  • 3.4 添加索引库字段信息
  • 总结



前言

Elasticsearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。Elasticsearch是用Java语言开发的,并作为Apache许可条款下的开放源码发布,是一种流行的企业级搜索引擎。Elasticsearch用于云计算中,能够达到实时搜索,稳定,可靠,快速,安装使用方便。其次是Apache Solr,也是基于Lucene。如果仅用于查询,Solr优势强于Elasticsearch。


提示:以下是本篇文章正文内容,下面案例可供参考

一、准备工作

springData中有elasticsearch开始依赖,工程构建之前最好找到版本之间的依赖。

springboot es跨索引查询 springboot创建es索引_elasticsearch

springboot es跨索引查询 springboot创建es索引_spring_02

本文springboot工程采用2.3.2.RELEASE版本,与已有的es7.10.1适配,基于此开始构建项目。

二、使用步骤

1.引入依赖

代码如下(示例):

<properties>
        <!--告诉springboot我们处理的ES的版本-->
        <elasticsearch.version>7.10.2</elasticsearch.version>
</properties>

<dependencies>
	<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
    </dependency>
</dependencies>

2.yml配置es集群

代码如下(示例):单机配置一个即可

spring:
  elasticsearch:
    rest:
      uris:
        - 192.168.53.112:9200
        - 192.168.53.113:9200
        - 192.168.53.114:9200

3.简单Test

代码如下(示例):

3.1 创建索引以及分片设置

@Test
public void createIndex() throws Exception{
  //1 创建索引并设置分片
  //1.1 创建一个RestHightLevelClient对象,相当于和服务端建立连接。
  RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
  		  //没有集群的话  此处可new 一个即可。
          new HttpHost("192.168.53.112",9200)
          new HttpHost("192.168.53.113",9200),
          new HttpHost("192.168.53.114",9200),
  ));

  //1.2 使用client的索引管理的对象,indices()返回索引管理对象。
  IndicesClient indicesClient = client.indices();
  //两个参数
  //1.2.1 创建索引请求对象  参数:创建的索引库的名称
  CreateIndexRequest request = new CreateIndexRequest("hello")
  			    .settings(Settings.builder()
                        .put("number_of_shards", 5)
                        .put("number_of_replicas", 1)
                        .build()
                );
  //1.2.2 请求选项,使用默认值。配置请求头,主要用于认证。
  CreateIndexResponse response = indicesClient.create(request,   RequestOptions.DEFAULT);

  //显示结果
  System.out.println(response.toString());
 }

3.2 创建索引库并设置mapping信息

@Test
public void createIndexAndMapping() throws Exception{
	RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
  		  //没有集群的话  此处可new 一个即可。
          new HttpHost("192.168.53.112",9200)
          new HttpHost("192.168.53.113",9200),
          new HttpHost("192.168.53.114",9200),
  	));
    //创建json数据
    XContentBuilder mappings = XContentFactory.jsonBuilder()
            .startObject()
               .startObject("properties")
                    .startObject("id")
                        .field("type","long")
                    .endObject()
                    .startObject("title")
                        .field("type","text")
                        .field("analyzer","ik_smart")
                        .field("store",true)
                    .endObject()
                .endObject()
            .endObject();

    //创建索引请求对象  参数:创建的索引库的名称,分片副片数量以及mapping信息
    CreateIndexRequest request = new CreateIndexRequest("hello1")
            .settings(Settings.builder()
                    .put("number_of_shards", 5)
                    .put("number_of_replicas", 1)
                    .build()
            )
            .mapping(mappings);

   //两个参数
    //1 创建索引请求对象  参数:创建的索引库的名称
    //2 请求选项,使用默认值。配置请求头,主要用于认证。
    CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);

    //显示结果
    System.out.println(response.toString());
	}
}

3.3删除索引库

@Test
public void deleteIndex() throws Exception{
	RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
  		  //没有集群的话  此处可new 一个即可。
          new HttpHost("192.168.53.112",9200)
          new HttpHost("192.168.53.113",9200),
          new HttpHost("192.168.53.114",9200),
  	));
		//删除索引库
        AcknowledgedResponse response = client.indices().delete(new DeleteIndexRequest("hello"), RequestOptions.DEFAULT);

        //显示结果
        System.out.println(response.toString());
  	
}

3.4 添加索引库字段信息

@Test
public void putIndex() throws Exception{
	RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
  		  //没有集群的话  此处可new 一个即可。
          new HttpHost("192.168.53.112",9200)
          new HttpHost("192.168.53.113",9200),
          new HttpHost("192.168.53.114",9200),
  	));
  	
  	String mappings = "{\n" +
                "\t\t\t\"properties\":{\n" +
                "\t\t\t\t\"id\":{\n" +
                "\t\t\t\t\t\"type\" : \"long\"\n" +
                "\t\t\t\t},\n" +
                "\t\t\t\t\"title\" :{\n" +
                "\t\t\t\t\t\"type\" : \"text\",\n" +
                "\t\t\t\t\t\"analyzer\" : \"ik_smart\",\n" +
                "\t\t\t\t\t\"store\" : true\n" +
                "\t\t\t\t},\n" +
                "\t\t\t\t\" content\" :{\n" +
                "\t\t\t\t\t\"type\" : \"text\",\n" +
                "\t\t\t\t\t\"analyzer\" : \"ik_smart\",\n" +
                "\t\t\t\t\t\"store\" :true\n" +
                "\t\t\t\t}\n" +
                "\t\t\t}\n" +
                "\t\t}";
		//将字符串以json形式发送
        PutMappingRequest request = new PutMappingRequest("hello1")
                .source(mappings, XContentType.JSON);

        //修改索引库
        AcknowledgedResponse response = client.indices().putMapping(request, RequestOptions.DEFAULT);

        //显示结果
        System.out.println(response.toString());
}

总结

以上就是这篇文章要讲的内容,本文仅仅简单介绍了es索引库的创建,修改已经字段创建的使用接口,而es还提供了大量能使我们快速便捷地处理数据接口。