ElasticSearch-1 ES基础概念及命令操作
ElasticSearch-2 ES6 TransportClient + SpringBoot2 增删改查
ElasticSearch-3 ES6 TransportClient 实现全文检索
ElasticSearch-4 ES7 RestHighLevelClient + SpringBoot2 增删改查
ElasticSearch-5 ES7 RestHighLevelClient 实现仿京东搜索
一、配置
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-elasticsearch'
}
二、POJO:EsBlog
package com.springboot.elasticsearch.domain.es;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import java.io.Serializable;
/**
* jdf
* Date:5/6/2019 9:32 AM
* @author xieyabo
*/
@Document(indexName = "blog",type="blog")
public class EsBlog implements Serializable {
private static final long serialVersionUID = 2019021796030422372L;
@Id
private String id;
private String title;
private String summary;
private String content;
protected EsBlog(){
}
public EsBlog(String title, String summary, String content){
this.title = title;
this.summary = summary;
this.content = content;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getSummary() {
return summary;
}
public void setSummary(String summary) {
this.summary = summary;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
@Override
public String toString() {
return String.format("EsBlog[id='%s',title='%s',summary='%s',content='%s']",id,title,summary,content);
}
}
三、JPA查询接口:EsBlogRepository
package com.springboot.elasticsearch.repository.es;
import com.springboot.elasticsearch.domain.es.EsBlog;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
public interface EsBlogRepository extends ElasticsearchRepository {
Page findDistinctEsBlogByTitleContainingOrSummaryContainingOrContentContaining(String title, String summary, String content, Pageable pageable);
}
四、测试
EsBlogTest:
package com.springboot.elasticsearch.domain.es;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import java.io.Serializable;
/**
* jdf
* Date:5/6/2019 9:32 AM
* @author xieyabo
*/
@Document(indexName = "blog",type="blog")
public class EsBlogTest implements Serializable {
private static final long serialVersionUID = 2019021796030422372L;
@Id
private String id;
private String title;
private String summary;
private String content;
protected EsBlogTest(){
}
public EsBlogTest(String title, String summary, String content){
this.title = title;
this.summary = summary;
this.content = content;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getSummary() {
return summary;
}
public void setSummary(String summary) {
this.summary = summary;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
@Override
public String toString() {
return String.format("EsBlog[id='%s',title='%s',summary='%s',content='%s']",id,title,summary,content);
}
}
ElasticsearchApplicationTests:
package com.springboot.elasticsearch;
import com.springboot.elasticsearch.domain.es.EsBlog;
import com.springboot.elasticsearch.repository.es.EsBlogRepository;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.Assertions.assertThat;
@RunWith(SpringRunner.class)
@SpringBootTest
public class ElasticsearchApplicationTests {
@Autowired
private EsBlogRepository esBlogRepository;
@Before
public void initRepositoryDate(){
// 清除存在的数据
esBlogRepository.deleteAll();
// 向ES里插入数据
esBlogRepository.save(new EsBlog("登鹳雀楼","王之涣的登鹳雀楼", "白日依山尽,黄河入海流。欲穷千里目,更上一层楼。"));
esBlogRepository.save(new EsBlog("相思","王维的相思","红豆生南国,春来发几枝。愿君多采撷,此物最相思。"));
esBlogRepository.save(new EsBlog("静夜思","李白的静夜思","床前明月光,疑似地上霜。举头望明月,低头思故乡"));
}
@Test
public void testFindDistinctEsBlogByTitleContainingOrSummaryContainingOrContentContaining() {
PageRequest pageable = new PageRequest(0, 20);
String title = "思";
String summary = "相思";
String content = "相思";
Page page = esBlogRepository.findDistinctEsBlogByTitleContainingOrSummaryContainingOrContentContaining(title, summary, content, pageable);
assertThat(page.getTotalElements()).isEqualTo(2);
System.out.println("-----start 1-----");
for (EsBlog blog:page.getContent()){
System.out.println(blog.toString());
}
System.out.println("-----end 1-----");
}
}
五、Controller:BlogController
package com.springboot.elasticsearch.controller;
import com.springboot.elasticsearch.domain.es.EsBlog;
import com.springboot.elasticsearch.repository.es.EsBlogRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/blog")
public class BlogController {
@Autowired
private EsBlogRepository esBlogRepository;
@GetMapping
public List list(@RequestParam(value = "title") String title,
@RequestParam(value = "summary" ) String summary,
@RequestParam(value = "content") String content,
@RequestParam(value = "pageIndex",defaultValue = "0") int pageIndex,
@RequestParam(value = "pageSize",defaultValue = "10") int pageSize ){
Pageable pageable = new PageRequest(pageIndex, pageSize);
Page page = esBlogRepository.findDistinctEsBlogByTitleContainingOrSummaryContainingOrContentContaining(title,summary,content,pageable);
return page.getContent();
}
}