1.BookController.java
@RestController
public class BookController {
@Autowired
private BookService bookService;
@GetMapping(value = {"/loadAll/{page}/{size}"})
public String loadAll(@PathVariable("page")int page, @PathVariable("size")int size){
return bookService.loadAll(page,size);
}
@GetMapping(value = {"/loadByName/{name}"})
public String loadByName(@PathVariable("name")String name){
return bookService.loadByName(name);
}
@GetMapping(value = {"/loadBySort/{fieldName}"})
public String loadBySort(@PathVariable("fieldName")String fieldName){
return bookService.loadBySort(fieldName);
}
@GetMapping(value = {"/loadByFetchField"})
public String loadBySort(String[] fieldNames){
String[] fieldName = new String[2];
fieldName[0] = "bookId";
fieldName[1] = "bookAuthor";
return bookService.loadByFetchField(fieldName);
}
@GetMapping(value = {"/loadByNameHight/{name}"})
public String loadByNameHight(@PathVariable("name")String name){
return bookService.loadByNameHight(name);
}
@GetMapping(value = {"/loadByMutil/{name}/{author}"})
public String loadByMutil(@PathVariable("name")String name,@PathVariable("author")String author){
return bookService.loadByMutil(new String[]{name,author});
}
@GetMapping(value = {"/loadByFilter/{name}/{price}"})
public String loadByFilter(@PathVariable("name")String name,@PathVariable("price")String price){
return bookService.loadByFilter(new String[]{name,price});
}
}
2.BookService.java
public interface BookService {
String loadAll(int page,int size);
public String loadByName(String name);
public String loadBySort(String fieldName);
public String loadByFetchField(String... fieldNames);
public String loadByNameHight(String name);
public String loadByMutil(String...values);
public String loadByFilter(String...values);
}
3.BookServiceImpl.java
@Service("bookService")
public class BookServiceImpl implements BookService {
@Autowired
private RestHighLevelClient client;
//分页查询
@Override
public String loadAll(int page, int size) {
SearchRequest request = new SearchRequest();
request.indices("book");
SearchSourceBuilder searchSource = new SearchSourceBuilder();
searchSource.from((page-1)*size);
searchSource.size(size);
request.source(searchSource);
SearchResponse response = null;
try{
response = client.search(request, RequestOptions.DEFAULT);
System.out.println(response);
}catch(Exception e){
System.out.println(e.getMessage());
}
String result = "";
SearchHits hits = response.getHits();
for (SearchHit hit:hits) {
result += hit.getSourceAsString();
}
return result;
}
//根据名字查询
@Override
public String loadByName(String name) {
SearchRequest request = new SearchRequest();
request.indices("book");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.matchQuery("bookName",name));
request.source(searchSourceBuilder);
SearchResponse response = null;
try{
response = client.search(request,RequestOptions.DEFAULT);
}catch(Exception e){
System.out.println(e.getMessage());
}
String result = "";
SearchHits hits = response.getHits();
for (SearchHit hit:hits) {
result += hit.getSourceAsString();
}
return result;
}
//排序
@Override
public String loadBySort(String fieldName) {
SearchRequest request = new SearchRequest();
request.indices("book");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.sort(fieldName, SortOrder.DESC);
request.source(searchSourceBuilder);
SearchResponse response = null;
try{
response = client.search(request,RequestOptions.DEFAULT);
}catch(Exception e){
System.out.println(e.getMessage());
}
String result = null;
SearchHits hits = response.getHits();
for (SearchHit hit:hits) {
result += hit;
}
return result;
}
//提取部分列
@Override
public String loadByFetchField(String... fieldNames) {
SearchRequest request = new SearchRequest();
request.indices("book");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.fetchSource(fieldNames,null);
searchSourceBuilder.sort("bookId",SortOrder.DESC);
request.source(searchSourceBuilder);
SearchResponse response = null;
try{
response = client.search(request,RequestOptions.DEFAULT);
}catch(Exception e){
System.out.println(e.getMessage());
}
String result = null;
SearchHits hits = response.getHits();
for (SearchHit hit:hits) {
result += hit.getSourceAsString();
}
return result;
}
//高亮显示
@Override
public String loadByNameHight(String name) {
SearchRequest request = new SearchRequest();
request.indices("book");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
HighlightBuilder highlightBuilder = new HighlightBuilder();
highlightBuilder.preTags("<font color='red'>");
highlightBuilder.postTags("</font>");
highlightBuilder.field("bookName");
searchSourceBuilder.highlighter(highlightBuilder);
searchSourceBuilder.query(QueryBuilders.matchQuery("bookName",name));
request.source(searchSourceBuilder);
SearchResponse response = null;
try{
response = client.search(request,RequestOptions.DEFAULT);
}catch(Exception e){
System.out.println(e.getMessage());
}
String result = "";
SearchHits hits = response.getHits();
for (SearchHit hit:hits) {
result += hit.getHighlightFields();
System.out.println(hit.getHighlightFields());
}
return result;
}
//条件查询
@Override
public String loadByMutil(String... values) {
SearchRequest request = new SearchRequest();
request.indices("book");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
QueryBuilder queryBuilder = QueryBuilders.matchPhraseQuery("bookName",values[0]);
QueryBuilder queryBuilder1 = QueryBuilders.matchPhraseQuery("bookAuthor",values[1]);
searchSourceBuilder.query(QueryBuilders.boolQuery().must(queryBuilder).mustNot(queryBuilder1));//需要满足queryBuilder条件,而满足queryBuilder1条件的内容
request.source(searchSourceBuilder);
SearchResponse response = null;
try{
response = client.search(request,RequestOptions.DEFAULT);
}catch(Exception e){
System.out.println(e.getMessage());
}
SearchHits hits = response.getHits();
String result = "";
for (SearchHit hit:hits) {
result += hit.getSourceAsString();
}
return result;
}
//范围查询
@Override
public String loadByFilter(String... values) {
SearchRequest request = new SearchRequest();
request.indices("book");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
QueryBuilder queryBuilder = QueryBuilders.matchPhraseQuery("bookName",values[0]);
QueryBuilder queryBuilder1 = QueryBuilders.rangeQuery("bookPrice").gte(values[1]);
//must().filter() 先执行must()结果再filter()
searchSourceBuilder.query(QueryBuilders.boolQuery().mustNot(queryBuilder).filter(queryBuilder1));
request.source(searchSourceBuilder);
SearchResponse response = null;
try{
response = client.search(request,RequestOptions.DEFAULT);
}catch(Exception e){
System.out.println(e.getMessage());
}
SearchHits hits = response.getHits();
String result = "";
for (SearchHit hit:hits) {
result += hit.getSourceAsString();
}
return result;
}
}