|
@@ -1,10 +1,11 @@
|
|
|
package com.jjt.doc.controller;
|
|
|
|
|
|
import com.alibaba.fastjson2.JSON;
|
|
|
+import com.jjt.doc.domain.DocInfo;
|
|
|
import com.jjt.doc.domain.EsDocInfo;
|
|
|
import com.jjt.doc.domain.HelloEntity;
|
|
|
-import com.jjt.doc.domain.PageDTO;
|
|
|
-import com.jjt.doc.service.EsDocInfoService;
|
|
|
+import com.jjt.doc.service.IDocInfoService;
|
|
|
+import com.jjt.doc.service.IEsDocInfoService;
|
|
|
import com.jjt.doc.service.HelloDao;
|
|
|
import org.elasticsearch.index.query.MatchQueryBuilder;
|
|
|
import org.elasticsearch.index.query.QueryBuilders;
|
|
@@ -35,7 +36,9 @@ public class ElasticSearchController {
|
|
|
@Resource
|
|
|
private HelloDao helloDao;
|
|
|
@Resource
|
|
|
- private EsDocInfoService esDocInfoService;
|
|
|
+ private IEsDocInfoService esDocInfoService;
|
|
|
+ @Resource
|
|
|
+ private IDocInfoService infoService;
|
|
|
|
|
|
/**
|
|
|
* 创建索引 hello
|
|
@@ -124,12 +127,13 @@ public class ElasticSearchController {
|
|
|
/**
|
|
|
* 分页查询
|
|
|
*
|
|
|
- * @param pageDTO
|
|
|
+ * @param page
|
|
|
+ * @param size
|
|
|
* @return
|
|
|
*/
|
|
|
- @GetMapping
|
|
|
- public String getAll(@RequestBody PageDTO pageDTO) {
|
|
|
- PageRequest pageable = PageRequest.of(pageDTO.getPage(), pageDTO.getSize());
|
|
|
+ @GetMapping("/getAll")
|
|
|
+ public String getAll(@RequestParam(value = "page") int page, @RequestParam(value = "size") int size) {
|
|
|
+ PageRequest pageable = PageRequest.of(page, size);
|
|
|
Page<HelloEntity> all = helloDao.findAll(pageable);
|
|
|
return JSON.toJSONString(all);
|
|
|
}
|
|
@@ -176,8 +180,28 @@ public class ElasticSearchController {
|
|
|
* @return
|
|
|
*/
|
|
|
@GetMapping("/findByContent")
|
|
|
- public String queryContent(@RequestParam(value = "content", required = false) String content) {
|
|
|
- List<SearchHit<EsDocInfo>> all = esDocInfoService.findByContent(content);
|
|
|
+ public String queryContent(@RequestParam(value = "content", required = true) String content) {
|
|
|
+ List<SearchHit<EsDocInfo>> all = esDocInfoService.findByContent(content, PageRequest.of(1, 2));
|
|
|
+ return JSON.toJSONString(all);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 模糊查询ik分词器分词的content字段
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @GetMapping("/query")
|
|
|
+ public String query(@RequestParam(value = "content", required = true) String content, @RequestParam(value = "page") int page, @RequestParam(value = "size") int size) {
|
|
|
+ List<SearchHit<EsDocInfo>> all = esDocInfoService.findByContent(content, PageRequest.of(page, size));
|
|
|
+ for (SearchHit<EsDocInfo> re : all) {
|
|
|
+ EsDocInfo esDocInfo = re.getContent();
|
|
|
+ DocInfo docInfo = new DocInfo();
|
|
|
+ docInfo.setDocId(esDocInfo.getId());
|
|
|
+ docInfo.setCreateYear(esDocInfo.getYear());
|
|
|
+ docInfo = infoService.selectDocInfoByDocInfo(docInfo);
|
|
|
+ esDocInfo.setInfo(docInfo);
|
|
|
+// re.getContent().
|
|
|
+ }
|
|
|
return JSON.toJSONString(all);
|
|
|
}
|
|
|
|
|
@@ -207,11 +231,9 @@ public class ElasticSearchController {
|
|
|
goodsNameField.postTags("</span>");
|
|
|
|
|
|
//创建条件组装器并组装条件然后构建出封装了所有条件的对象
|
|
|
- NativeSearchQuery searchQuery = new NativeSearchQueryBuilder()
|
|
|
- .withQuery(matchQuery)
|
|
|
+ NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery)
|
|
|
//组装配置的高亮字段
|
|
|
- .withHighlightFields(goodsNameField)
|
|
|
- .build();
|
|
|
+ .withHighlightFields(goodsNameField).build();
|
|
|
|
|
|
//查询
|
|
|
SearchHits<HelloEntity> searchHits = elasticsearchRestTemplate.search(searchQuery, HelloEntity.class);
|