SpringBoot輕松整合ElasticSearch


完整項目代碼地址(https://github.com/fonxian/spring-elasticsearch-example/tree/master/spring-elasticsearch-example)

一、整合過程

引入依賴

	<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>


    <dependencies>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

    </dependencies>

添加配置


server.port=8085

spring.data.elasticsearch.cluster-nodes = bei1:9300
elasticsearch.cluster.name=coffe-elasticsearch

創建實體類和數據訪問類

實體類


@Document(indexName = "book",type = "book")
public class Book {
    @Id
    private String id;
    private String name;
    private Long price;
    @Version
    private Long version;

    public Map<Integer, Collection<String>> getBuckets() {
        return buckets;
    }

    public void setBuckets(Map<Integer, Collection<String>> buckets) {
        this.buckets = buckets;
    }

    @Field(type = FieldType.Nested)
    private Map<Integer, Collection<String>> buckets = new HashMap();

    public Book(){}

    public Book(String id, String name,Long version) {
        this.id = id;
        this.name = name;
        this.version = version;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Long getPrice() {
        return price;
    }

    public void setPrice(Long price) {
        this.price = price;
    }

    public long getVersion() {
        return version;
    }

    public void setVersion(long version) {
        this.version = version;
    }
}

數據訪問類


@Component
public interface BookRepository extends ElasticsearchRepository<Book,String> {

    Page<Book> findByNameAndPrice(String name, Long price, Pageable pageable);
    Page<Book> findByNameOrPrice(String name, Long price, Pageable pageable);
    Page<Book> findByName(String name, Pageable pageable);

}

二、單元測試、測試整合結果

使用單元測試測試使用結果

創建測試類


@SpringBootTest
@RunWith(SpringRunner.class)
public class BookRepositoryTest {

    @Autowired
    private BookRepository repository;
    @Autowired
    private ElasticsearchTemplate esTemplate;
    
}

(1)添加文檔



    /**
     * 插入文檔
     */
    @Test
    public void indexBook() {

        Book book = new Book();
        book.setId("123456");
        book.setName("瓦爾登湖");
        book.setPrice(20L);
        book.setVersion(1L);
        repository.save(book);

        Book book2 = new Book();
        book2.setId("234567");
        book2.setName("Java編程思想");
        book2.setPrice(88L);
        book2.setVersion(1L);
        repository.save(book2);

        Book book3 = new Book();
        book3.setId("8910");
        book3.setName("程序員的自我修養");
        book3.setPrice(56L);
        book3.setVersion(1L);
        repository.save(book3);

    }



(2)查詢所有文檔

    /**
     * 獲取所有文檔
     */
    @Test
    public void getAll() {
        repository.findAll().forEach(book -> {
            System.out.println(book.getName());
            System.out.println(book.getPrice());
        });
    }

運行結果

(3)使用查詢條件


	/**
     * 使用查詢條件
     */
    @Test
    public void queryByNameOrPrice() {
        Page<Book> books = repository.findByNameOrPrice("瓦爾登湖", 56L, Pageable.unpaged());
        books.forEach(book -> {
            System.out.println(book.getName());
            System.out.println(book.getPrice());
        });
    }


運行結果

(4)使用原生方式查詢


    /**
     * 原生方式查詢字段
     */
    @Test
    public void queryByName() {

        QueryBuilder queryBuilder = new QueryStringQueryBuilder("修養").field("name");
        SearchQuery searchQuery = new NativeSearchQueryBuilder()
                .withQuery(queryBuilder)
                .build();
        Page<Book> bookPage = esTemplate.queryForPage(searchQuery, Book.class);
        bookPage.getContent().forEach(book -> {
            System.out.println(book.getName());
        });

    }


運行結果

參考文檔

spring-data-elasticsearch


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM