瀏覽代碼

添加think接口

Jacky Wong 4 年之前
父節點
當前提交
775d754f71

+ 6 - 0
search-service-quiz/src/main/java/com/liangjian/search/controller/QAProblemController.java

@@ -42,6 +42,12 @@ public class QAProblemController {
         }
     }
 
+    @GetMapping("/think")
+    @ApiOperation(value = "搜索联想")
+    public List<String> think(@ApiParam(value = "搜索关键词",required = true) @RequestParam String kw) {
+        return iqaProblemService.think(kw);
+    }
+
     @PostMapping("/problem/{id}")
     @ApiOperation(value = "新增问题")
     public ResultObject<Object> addProblem(@ApiParam(value = "id", required = true) @PathVariable("id") Long id) {

+ 2 - 0
search-service-quiz/src/main/java/com/liangjian/search/service/IQAProblemService.java

@@ -23,6 +23,8 @@ public interface IQAProblemService extends IService<QAProblem> {
 
     Page<QAProblemEs> search(String keyword, Integer page, Integer size);
 
+    List<String> think(String keyword);
+
     Tuple2<Integer,String> addQAProblemEs(Long id);
 
     @Deprecated

+ 29 - 1
search-service-quiz/src/main/java/com/liangjian/search/service/impl/QAProblemService.java

@@ -28,7 +28,8 @@ import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
 import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
 import org.springframework.stereotype.Service;
 
-import java.util.List;
+import java.util.*;
+import java.util.stream.Collectors;
 
 import static org.elasticsearch.index.query.QueryBuilders.*;
 
@@ -82,6 +83,33 @@ public class QAProblemService extends ServiceImpl<QAProblemMapper,QAProblem> imp
         //return qaProblemEsRepository.search(sourceBuilder.query(),pageable);
     }
 
+    @Override
+    public List<String> think(String keyword) {
+        boolean isChinese = keyword.getBytes().length != keyword.length();
+        BoolQueryBuilder queryBuilder = boolQuery().must(termQuery("state", 1));
+        if(isChinese) {
+            queryBuilder = queryBuilder.must(boolQuery().
+                    should(matchQuery("tags", keyword).boost(10)).
+                    should(matchQuery("title", keyword).boost(5))
+            );
+        } else {
+            queryBuilder = queryBuilder.must(boolQuery().
+                    should(matchQuery("tags.pinyin", keyword).boost(10)).
+                    should(matchQuery("title.pinyin", keyword).boost(5))
+            );
+        }
+        Page<QAProblemEs> page = qaProblemEsRepository.search(queryBuilder, PageRequest.of(0,10));
+        Set<String> set = page.getContent().stream()
+                .map(i -> i.getTags() == null ? i.getTitle() : i.getTags())
+                .flatMap(i -> Arrays.stream(i.split("\\|")))
+                .collect(Collectors.toSet());
+        List<String> res =  new ArrayList<>(set);
+        res = res.stream()
+                .sorted(Comparator.comparingInt(String::length))
+                .collect(Collectors.toList());
+        return res;
+    }
+
     @Override
     public Tuple2<Integer,String> addQAProblemEs(Long id) {
         var list = getWrappedProblem(id,0,1);