Wechat search a search: science and technology cat, sharing programming, software, technology.

The Spring Data Elasticsearch project provides integration with the Elasticsearch search engine. The key functional area of Spring Data Elasticsearch is a POJO-centric model for interacting with Elastichsearch documents and writing a repository style Data access layer easily.

Maven rely on

<! --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>
Copy the code

Configure the application. The properties

spring.elasticsearch.rest.uris=http://localhost:9200
# spring.elasticsearch.rest.username= / / user name
# spring.elasticsearch.rest.password=/ / password
# spring.elasticsearch.rest.connection-timeout= // Connection timeout time
# spring.elasticsearch.rest.read-timeout= // Read timeout time
Copy the code

CRUD operations

Entity class

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) // Means custom attribute format time format, we can pass these formats in Java program time
    private Long createTime;
}
Copy the code

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

Copy the code

Test

The query

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(a) {
        Iterable<Message> all = messageDao.findAll();
        all.forEach(System.out::println);
    }

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

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

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

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

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

increase

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(a) {
        Message message = new Message();
        message.setId("3");
        message.setAge("18");
        message.setEmail("[email protected]");
        message.setCreateTime(new Date().getTime());
        message.setUsername("JonssonYan"); messageDao.save(message); }}Copy the code

delete

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(a){
        messageDao.deleteById("2"); }}Copy the code

Customize THE JSON query

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);
}
Copy the code

Test

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

References

[1] Spring Data Elasticsearch: Spring. IO /projects/sp…