Springboot integrates elasticsearch to achieve simple addition, deletion and modification

Premise: First of all, of course, the es environment is configured, you can refer to my previous blog https://blog.csdn.net/weixin_39025362/article/details/105359306

1. Create springboot project and introduce maven dependency of es and lombok

<dependency>
   <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<!--lombok是创建实体类通过注解@Data为实体创建get、set、toString等方法,使我们的实体类非常简洁-->
 <dependency>
   <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <optional>true</optional>
</dependency>

2. Create es index library

1. The index library can be created through the postman or head plug-in. The author uses es6.2.4 The
following introduction uses postman to create the es index library, put request, url: http: // localhost: 9200 / test-search
as described in mysql , Is to create a database called test-search, and then a data table called article,
with three fields, id long type, title text type using ik word search, content text type, using ik word search

{
  "settings": {
    "index.number_of_shards": 1,
    "index.number_of_replicas": 0,
    "index.refresh_interval": "-1"
  },
  "mappings": {
    "article": {
      "properties": {
        "id": {
          "type": "long",
          "index": false
        },
        "title": {
          "type": "text",
          "analyzer": "ik_max_word",
          "search_analyzer": "ik_max_word"
        },
        "content": {
          "type": "text",
          "analyzer": "ik_max_word",
          "search_analyzer": "ik_max_word",
          "boost": 1.5
        }
      }
    }
  }
}

Insert picture description here
Index library created successfully
Insert picture description here

3. Go back to the springboot project and start writing code

1. Create an entity class called article

@Data
@Document(indexName = "test-search",type = "article",shards = 1,replicas = 0, refreshInterval = "-1")
public class Article implements Serializable {
    private Long id;

    private String title; //标题

    private String content;// 缩略内容
    
}

2.dao layer, create an ArticleRepository interface, inheriting ElasticsearchRepository

public interface ArticleRepository extends ElasticsearchRepository<Article,Long> {
    /**
     * 使用jpa的一个好处就是你想联合title和content去搜索,它就会有方法出现,使用pageable分页
     * @param title
     * @param content
     * @param pageable
     * @return
     */
    Page<Article> findByTitleLikeOrContentLike(String title, String content, Pageable pageable);

    /**
     * 根据article的content内容去搜索,使用pageable份额有
     * @param content
     * @param pageable
     * @return
     */
    Page<Article> findByContentLike(String content, Pageable pageable);
}

3.service layer,

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

    public void save(Article article){
        articleRepository.save(article);
    }

    public Page<Article> search(String title, String content, Pageable pageable){
        return articleRepository.findByTitleLikeOrContentLike(title,content,pageable);
    }

    public Page<Article> search(String content,Pageable pageable){
        return articleRepository.findByContentLike(content,pageable);
    }
    public Page<Article> findAll(Pageable pageable){
        return articleRepository.findAll(pageable);
    }
}

4,controller


@RestController
public class ArticleController {
    @Autowired
    private ArticleRepository articleRepository;

    @Autowired
    private ArticleService articleService;
	
	//可以插入单条也可以多条
    @RequestMapping("/articleSave")
    public String save(){
    List<Article> articles = new ArrayList<>();
    articles.add(new Article(System.currentTimeMillis(),"小米",
            "小米手机"));
    articles.add(new Article(System.currentTimeMillis(),"华为",
            "华为手机"));

    for(int i=0; i<articles.size(); i++){
        articleRepository.save(articles.get(i));
        System.out.println(articles.get(i));
    }
    return "success";
    }

    @RequestMapping("/search")
    public String search(HttpServletRequest request){
        int pageNum = 0;
        int pageSize = 10;
        String title = request.getParameter("title");
        String content = request.getParameter("content");
        Pageable pageable = PageRequest.of(pageNum,pageSize);
        Page<Article> articles = articleService.search(title,content,pageable);
        return articles.toString();
    }
}

Published 25 original articles · praised 4 · visits 1516

Guess you like

Origin blog.csdn.net/weixin_39025362/article/details/105360676