Spring-Boot整合Elasticsearch

Spring Data Elasticsearch项目提供了与Elasticsearch搜索引擎的集成。Spring Data Elasticsearch的关键功能区域是一个以POJO为中心的模型,该模型用于与Elastichsearch文档进行交互并轻松编写存储库样式的数据访问层。

Maven依赖

<!--https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-elasticsearch-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
            <version>2.3.0.RELEASE</version>
        </dependency>

配置application.properties

spring.elasticsearch.rest.uris=http://localhost:9200
# spring.elasticsearch.rest.username= // 用户名
# spring.elasticsearch.rest.password=// 密码
# spring.elasticsearch.rest.connection-timeout= // 连接超时时间
# spring.elasticsearch.rest.read-timeout= // 读取超时时间

CRUD操作

实体类

package com.example.entity;

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;

import java.io.Serializable;

@Data
@Document(indexName = "account", type = "_doc")
public class Message implements Serializable {
    private static final long serialVersionUID = 5710293639676035958L;
    @Id
    private String id;

    @Field(type = FieldType.Text, analyzer = "ik_max_word")
    private String username;

    @Field(type = FieldType.Text, analyzer = "ik_max_word")
    private String email;

    @Field(type = FieldType.Long)
    private String age;

    @Field(type = FieldType.Long) // 意思自定义属性格式 时间格式,我们在java程序中可以传入这些格式的时间
    private Long createTime;
}

Dao

package com.example.dao;

import com.example.entity.Message;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface MessageDao extends ElasticsearchRepository<Message, String> {
    Page<Message> findAll(Pageable pageable);

    Iterable<Message> findAll(Sort sort);

    List<Message> findByUsername(String username);

    List<Message> findByAgeBetween(Long mix, Long max);

    Long countByAgeBetween(Long mix, Long max);
}

Test

查询

package com.example;

import com.example.dao.MessageDao;
import com.example.entity.Message;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;

import java.util.List;

@SpringBootTest
public class SpringDataElasticsearchFindTest {
    @Autowired
    private MessageDao messageDao;

    @Test
    public void findAll() {
        Iterable<Message> all = messageDao.findAll();
        all.forEach(System.out::println);
    }

    @Test
    public void findAllPageRequest() {
        Iterable<Message> all = messageDao.findAll(PageRequest.of(0, 1));
        all.forEach(System.out::println);
    }

    @Test
    public void findAllSort() {
        Iterable<Message> all = messageDao.findAll(Sort.by("age").descending());
        all.forEach(System.out::println);
    }

    @Test
    public void findByUsername() {
        List<Message> messages = messageDao.findByUsername("JonssonYan");
        messages.forEach(System.out::println);
    }

    @Test
    public void findByAgeBetween() {
        List<Message> messages = messageDao.findByAgeBetween(10L, 20L);
        messages.forEach(System.out::println);
    }

    @Test
    public void countByAgeBetween() {
        Long count = messageDao.countByAgeBetween(10L, 20L);
        System.out.println(count);
    }
}

增加

package com.example;

import com.example.dao.MessageDao;
import com.example.entity.Message;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.Date;

@SpringBootTest
public class SpringDataElasticsearchInsertTest {
    @Autowired
    private MessageDao messageDao;

    @Test
    public void insert() {
        Message message = new Message();
        message.setId("3");
        message.setAge("18");
        message.setEmail("yz808@qq.com");
        message.setCreateTime(new Date().getTime());
        message.setUsername("JonssonYan");
        messageDao.save(message);
    }
}

删除

package com.example;

import com.example.dao.MessageDao;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
public class SpringDataElasticsearchDeleteTest {
    @Autowired
    private MessageDao messageDao;

    @Test
    public void deleteById(){
        messageDao.deleteById("2");
    }
}

自定义JSON查询

Dao

package com.example.dao;

import com.example.entity.Message;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.elasticsearch.annotations.Query;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface MessageDao extends ElasticsearchRepository<Message, String> {
    @Query("{\"match\": {\"username\": {\"query\": \"?0\"}}}")
    List<Message> findByUsername(String username);
}

Test

@Test
    public void findByUsername() {
        List<Message> messages = messageDao.findByUsername("JonssonYan");
        messages.forEach(System.out::println);
    }

References

[1] Spring Data Elasticsearch: https://spring.io/projects/spring-data-elasticsearch

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值