Spring Boot integration framework ElasticSearch

In the application, there is often retrieve functions - data contained in the database query keywords, check if the way the database (like keyword), the efficiency is very low. To solve this problem, we introduced ElasticSearch framework.

ElasticSearch download and install, please refer to the blog: https://blog.csdn.net/yjclsx/article/details/81302041

Next, we use the Spring Data Elasticsearch Repositories integrated ElasticSearch 

Step 1: introduction of mapping

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

Step 2: Set Profile

spring.data.elasticsearch.cluster-name=elasticsearch
spring.data.elasticsearch.cluster-nodes=127.0.0.1:9300
spring.data.elasticsearch.repositories.enabled=true

Step 3 :: define an entity class

package com.example.demo.domain;

import java.io.Serializable;
import org.springframework.data.elasticsearch.annotations.Document;

@Document(indexName = "blog", type = "article")
public class Article implements Serializable{
    private static final long serialVersionUID = 1L;
    private long id;
    private String title;
    private String summary;
    private String content;
    private int pv;
    //省略getter、setter方法
}

Here the definition of Article instance that represents the article. If analogy relational database, Index equivalent table, Document equivalent record

In ElasticSearch 6.X version, does not recommend the use of type, but also in the 7.X versions will be completely obsolete type, but I am here is ElasticSearch 5.6.8, it is still written type. Here, an Article on behalf of an article, also on behalf of an index entry.

Step 4: definition of an interface, and inheritance ElasticSearchRepository

package com.example.demo.repository;

import com.example.demo.domain.Article;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;


@Component 
//@Repository
public interface ArticleRepository extends ElasticsearchRepository<Article, Long> {

}

Repository here is equivalent to DAO, the operation is the same mysql or ElasticSearch

Step 5: Define the service interface and implementation class

package com.example.demo.service;

import com.example.demo.domain.Article;
import org.elasticsearch.index.query.QueryBuilder;

public interface ArticleService {

    Article save(Article Article);

    Iterable<Article> search(QueryBuilder queryBuilder);
}

Implementation class

package com.example.demo.service.impl;

import com.example.demo.domain.Article;
import com.example.demo.repository.ArticleRepository;
import com.example.demo.service.ArticleService;
import org.elasticsearch.index.query.QueryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    private ArticleRepository articleRepository;

    @Override
    public Article save(Article article) {
        return articleRepository.save(article);
    }

    @Override
    public Iterable<Article>  search(QueryBuilder queryBuilder) {
        return articleRepository.search(queryBuilder);
    }

}

Step 6: write a test method

package com.example.demo.controller;

import com.example.demo.domain.Article;
import com.example.demo.domain.JsonData;
import com.example.demo.repository.ArticleRepository;

import com.example.demo.service.ArticleService;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/api/v1/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;
    
    @GetMapping("save")
    public Object save(long id,String title){
    
        Article article = new Article();
        article.setId(id);
        article.setPv(123);
        article.setContent("搜索elasticsearch框架整合");
        article.setTitle(title);
        article.setSummary("搜索框架整合");

        articleService.save(article);
    
        return JsonData.buildSuccess();
    }

    
    @GetMapping("search")
    public Object search(String title){

        //QueryBuilder queryBuilder = QueryBuilders.matchAllQuery(); //搜索全部文档
        QueryBuilder queryBuilder = QueryBuilders.matchQuery("title", title); 

        Iterable<Article> list =  articleService.search(queryBuilder);
        
        return JsonData.buildSuccess(list);
    }

}

Use Postman test

 

 

Reference article: https://www.cnblogs.com/cjsblog/p/9756978.html

Guess you like

Origin www.cnblogs.com/jwen1994/p/11374154.html