SpringDataElasticsearch操作Elasticsearch创建索引库以及创建映射

导包

    <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);
    }
}

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
对于Spring Data集成Elasticsearch 8.1.2,你可以按照以下步骤进行操作: 1. 添加依赖:在你的项目中,打开pom.xml文件,添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> </dependency> ``` 2. 配置Elasticsearch连接:在application.properties或application.yml文件中添加以下配置: ```properties # Elasticsearch spring.data.elasticsearch.cluster-nodes=localhost:9200 spring.data.elasticsearch.cluster-name=my-cluster spring.data.elasticsearch.repositories.enabled=true ``` 确保将`localhost:9200`替换为你的Elasticsearch服务器的地址和端口号。 3. 创建实体类:创建一个Java类,用于映射Elasticsearch索引中的文档。例如,假设你的索引中有一个名为"product"的文档类型,你可以创建一个对应的Product类: ```java import org.springframework.data.annotation.Id; import org.springframework.data.elasticsearch.annotations.Document; @Document(indexName = "product") public class Product { @Id private String id; private String name; // 其他属性和getter/setter方法... } ``` 这里的`indexName`属性指定了该类对应的索引名称。 4. 创建Repository接口:创建一个继承自ElasticsearchRepository的接口,用于操作Elasticsearch中的文档。例如,创建一个ProductRepository接口: ```java import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; public interface ProductRepository extends ElasticsearchRepository<Product, String> { // 可以在这里定义一些自定义的查询方法 } ``` 这个接口会自动实现一些基本的CRUD操作,你也可以在其中定义自定义的查询方法。 现在,你就可以在你的应用程序中使用ProductRepository来操作Elasticsearch中的文档了。 这是一个简单的Spring Data集成Elasticsearch的例子,具体的使用方法和配置可能会根据你的实际情况有所不同。你可以根据Spring Data Elasticsearch的文档进行更详细的配置和使用。希望对你有所帮助!如有更多问题,请继续提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值