Spring Data Elasticsearchの基本的な使用法は本当に香りがよいです。警告!

Spring Data Elasticsearchプロジェクトは、Elasticsearch検索エンジンとの統合を提供します。SpringDataElasticsearchの主要な機能領域はPOJO中心のモデルであり、Elastichsearchドキュメントと対話し、リポジトリスタイルのデータアクセスレイヤーを簡単に作成するために使用されます。
公式紹介:https://spring.io/projects/spring-data-elasticsearch

1つ、Mavenの依存関係

		<dependency>
			<groupId>org.springframework.data</groupId>
			<artifactId>spring-data-elasticsearch</artifactId>
			<version>4.0.4.RELEASE</version>
		</dependency>

二、配置application.properties

spring.data.elasticsearch.cluster-name=my-elasticsearch
spring.data.elasticsearch.cluster-nodes=http://127.0.0.1:9300

3、エンティティクラス

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

、、ダオ

5つの基本的なCRUD操作

公式の説明を参照してください:https://docs.spring.io/spring-data/elasticsearch/docs/4.0.4.RELEASE/reference/html/#elasticsearch.repositories

1.クエリ

ダオ

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

テスト

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

2.増やす

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("[email protected]");
        message.setCreateTime(new Date().getTime());
        message.setUsername("JonssonYan");
        messageDao.save(message);
    }
}

ここに写真の説明を挿入

3.変更

変更は追加と同じですが、同じIDを設定するだけで、実行後にデータが自動的に置き換えられます

4.削除

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

ここに写真の説明を挿入

6、高度

1.カスタムJSONクエリ

テスト

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

ダオ

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

おすすめ

転載: blog.csdn.net/y1534414425/article/details/108989217