实现复杂查询数据接口
This commit is contained in:
@@ -0,0 +1,87 @@
|
||||
package top.fjy8018.elasticsearch.Controller;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.elasticsearch.action.search.SearchRequestBuilder;
|
||||
import org.elasticsearch.action.search.SearchResponse;
|
||||
import org.elasticsearch.action.search.SearchType;
|
||||
import org.elasticsearch.client.transport.TransportClient;
|
||||
import org.elasticsearch.index.query.BoolQueryBuilder;
|
||||
import org.elasticsearch.index.query.QueryBuilders;
|
||||
import org.elasticsearch.index.query.RangeQueryBuilder;
|
||||
import org.elasticsearch.search.SearchHit;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author F嘉阳
|
||||
* @date 2018-05-19 22:46
|
||||
*/
|
||||
@RequestMapping("/query/book")
|
||||
@RestController
|
||||
@Slf4j
|
||||
public class QueryBookController {
|
||||
|
||||
private static String INDEX = "book";
|
||||
|
||||
@Autowired
|
||||
private TransportClient client;
|
||||
|
||||
/**
|
||||
* 复合查询,根据传入的条件进行复合查询,部分条件可不传
|
||||
*
|
||||
* @param author
|
||||
* @param title
|
||||
* @param gtWordCount 最小字数
|
||||
* @param ltWordCount 最大字数
|
||||
* @return
|
||||
*/
|
||||
@PostMapping("/novel")
|
||||
public ResponseEntity query(@RequestParam(name = "author", required = false) String author,
|
||||
@RequestParam(name = "title", required = false) String title,
|
||||
@RequestParam(name = "gt_word_count", defaultValue = "0") int gtWordCount,
|
||||
@RequestParam(name = "lt_word_count", required = false) Integer ltWordCount) {
|
||||
String type = "novel";
|
||||
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
|
||||
|
||||
if (author != null) {
|
||||
boolQuery.must(QueryBuilders.matchQuery("author", author));
|
||||
}
|
||||
|
||||
if (title != null) {
|
||||
boolQuery.must(QueryBuilders.matchQuery("title", title));
|
||||
}
|
||||
|
||||
RangeQueryBuilder rangeQuery = QueryBuilders.rangeQuery("word_count").from(gtWordCount);
|
||||
if (ltWordCount != null && ltWordCount > 0) {
|
||||
rangeQuery.to(ltWordCount);
|
||||
}
|
||||
// 加入filter中
|
||||
boolQuery.filter(rangeQuery);
|
||||
|
||||
SearchRequestBuilder builder = client.prepareSearch(INDEX).setTypes(type)
|
||||
.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
|
||||
.setQuery(boolQuery)
|
||||
.setFrom(0)
|
||||
.setSize(10);
|
||||
// 输出构造体
|
||||
log.debug(builder.toString());
|
||||
|
||||
SearchResponse response = builder.get();
|
||||
|
||||
List result = new ArrayList<Map<String, Object>>();
|
||||
for (SearchHit hit : response.getHits()) {
|
||||
result.add(hit.getSourceAsMap());
|
||||
}
|
||||
|
||||
return new ResponseEntity(result, HttpStatus.OK);
|
||||
}
|
||||
}
|
||||
@@ -81,7 +81,7 @@ public class UpdateBookController {
|
||||
if (response.getId().equals(id)) {
|
||||
return new ResponseEntity(HttpStatus.OK);
|
||||
}
|
||||
|
||||
|
||||
log.error("【response】" + response.toString() + "【title】" + title + "【author】" + author + "【wordCount】" + wordCount + "【publishDate】" + publishDate);
|
||||
return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user