SpringDataElasticsearch操作Elasticsearch创建索引库以及创建映射
程序员文章站
2022-12-17 22:19:22
导包 org.springframework.boot spring-boot-starter-parent 2.1.3.RELEASE ...
导包
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.8.1</version>
</dependency>
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.1</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<!-- 6.2.4有坑,所以这里用6.4.3-->
<version>6.4.3</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!-- elasticsearch的启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
application.yml配置
注意:SpringDataElasticsearch
底层使用的不是Elasticsearch
提供的RestHighLevelClient
,而是TransportClient
,并不采用Http
协议通信,而是访问elasticsearch
对外开放的tcp
端口
spring:
data:
elasticsearch:
cluster-name: text-elastic #集群名称
cluster-nodes: 127.0.0.1:9301,127.0.0.1:9302,127.0.0.1:9303 #有多少机器,写多少
启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class EsDemoApplication {
public static void main(String[] args) {
SpringApplication.run(EsDemoApplication.class,args);
}
}
准备一个pojo对象
import lombok.Data;
@Data
public class Esneo{
private Long id;
private String title; //标题
private String category;// 分类
private String brand; // 品牌
private Double price; // 价格
private String images; // 图片地址
public Esneo() {
}
public Esneo(Long id, String title, String category, String brand, Double price, String images) {
this.id = id;
this.title = title;
this.category = category;
this.brand = brand;
this.price = price;
this.images = images;
}
}
创建索引库以及创建映射
想来想去还是,测试类方便,所有这里还是用测试类的方式演示
实体类注释:
-
@Document:声明索引库配置
-
indexName
:索引库名称 -
type
:类型名称,默认是“docs” -
shards
:分片数量,默认5 -
replicas
:副本数量,默认1
-
- @Id:声明实体类的id
-
@Field:声明字段属性
-
type
:字段的数据类型 -
analyzer
:指定分词器类型 -
index
:是否创建索引
-
修改对应的pojo
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
@Data //注意lombok包
@Document(indexName = "lianxi",type = "esneo",shards = 3,replicas =1)
public class Esneo {
@Id
private Long id;
@Field(type = FieldType.Text,analyzer = "ik_max_word")
private String title; //标题
@Field(type = FieldType.Keyword)
private String category;// 分类
@Field(type = FieldType.Keyword)
private String brand; // 品牌
@Field(type = FieldType.Double)
private Double price; // 价格
@Field(type = FieldType.Keyword)
private String images; // 图片地址
public Esneo() {
}
public Esneo(Long id, String title, String category, String brand, Double price, String images) {
this.id = id;
this.title = title;
this.category = category;
this.brand = brand;
this.price = price;
this.images = images;
}
}
测试类代码
import com.itcsnd.pojo.Esneo;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringElasticsearchTest {
@Autowired
private ElasticsearchTemplate elasticsearchTemplate;
/**
* 创建索引和映射
*/
@Test
public void testCreateMappingAndIndex(){
try {
elasticsearchTemplate.putMapping(Esneo.class);
} catch (Exception e) {
elasticsearchTemplate.createIndex(Esneo.class);
elasticsearchTemplate.putMapping(Esneo.class);
}
}
/**
* 创建索引库
*/
@Test
public void testCreateIndex(){
// 创建索引库,并制定实体类的字节码
elasticsearchTemplate.createIndex(Esneo.class);
}
/**
* 创建映射
*/
@Test
public void testCreateMapping(){
// 创建索引库,并制定实体类的字节码
elasticsearchTemplate.putMapping(Esneo.class);
}
}
本文地址:https://blog.csdn.net/weixin_44257023/article/details/110148280
推荐阅读
-
mysql数据库表的创建以及字段的增删改查操作及一些常用的查询命令介绍
-
SpringDataElasticsearch操作Elasticsearch创建索引库以及创建映射
-
Elasticsearch索引(index)、映射(mapping)等相关的创建
-
ElasticSearch创建索引(index)和添加映射(mapping)
-
使用SQL创建数据库表以及相关操作
-
SQL系列一——数据库与表操作以及创建约束
-
mysql数据库表的创建以及字段的增删改查操作及一些常用的查询命令介绍
-
SpringDataElasticsearch操作Elasticsearch创建索引库以及创建映射
-
数据库基本表的操作创建表,索引和查询
-
数据库基本表的操作创建表,索引和查询