1.学习完elasticsearch。了解Java的操作。
2.在将elasticsearch部署到服务器的时候出现了很多错误。
3.写完elasticsearch的接口。
4.
/** * <pre>通过文章标题查找文档</pre> * @param title 文章标题 * @return 文档类的链表 */ @GetMapping("findByTitle") public Page4Navigator<ReturnDoc> findDocumentByTitle( @RequestParam(value = "title",defaultValue = "") String title, @RequestParam(value = "current",defaultValue = "1")int current, @RequestParam(value = "pageSize",defaultValue = "10")int pageSize ) { NativeSearchQueryBuilder query = new NativeSearchQueryBuilder(); BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery(); if(StringUtils.isNotBlank(title)){ boolQueryBuilder.must(QueryBuilders.matchQuery("title",title)); } query.withQuery(boolQueryBuilder); Pageable pageable = PageRequest.of(current-1,pageSize); SearchHits<Document> searchHits = elasticsearchRestTemplate.search(query.build(),Document.class, IndexCoordinates.of("document")); searchHits.getTotalHits(); List<ReturnDoc> list = new ArrayList<>(); for (SearchHit<Document>searchHit:searchHits){ ReturnDoc returnDoc = new ReturnDoc(); returnDoc.setDocument(searchHit.getContent()); returnDoc.setPage(1); returnDoc.setScore(searchHit.getScore()*100); list.add(returnDoc); } Page<ReturnDoc> page = new PageImpl<>(list, pageable, searchHits.getTotalHits()); return new Page4Navigator<>(page, 5); } private void createQuery(BoolQueryBuilder queryBuilder, String subject, String searchTypeOf, String linkOf,String fieldName){ if(subject!=null && subject.length()>0){ if("precision".equals(searchTypeOf)){ //精确 if("and".equals(linkOf)){ queryBuilder.must(QueryBuilders.matchQuery(fieldName,subject)); }else if("or".equals(linkOf)){ queryBuilder.should(QueryBuilders.matchQuery(fieldName,subject)); }else{ queryBuilder.mustNot(QueryBuilders.matchQuery(fieldName,subject)); } }else{ //模糊 if("and".equals(linkOf)){ queryBuilder.must(QueryBuilders.fuzzyQuery(fieldName,subject).fuzziness(Fuzziness.AUTO)); }else if("or".equals(linkOf)){ queryBuilder.should(QueryBuilders.fuzzyQuery(fieldName,subject).fuzziness(Fuzziness.AUTO)); }else{ queryBuilder.mustNot(QueryBuilders.fuzzyQuery(fieldName,subject).fuzziness(Fuzziness.AUTO)); } } } } @PostMapping("advancedSearch") public Page4Navigator<ReturnDoc> advanceSearch(@RequestBody SearchDoc searchDoc){ NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder(); BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery(); // boolQueryBuilder.must(QueryBuilders.matchQuery("title",searchDoc.getTitle())) createQuery(boolQueryBuilder,searchDoc.getTitle(),searchDoc.getSearchTypeOfTitle(),searchDoc.getLinkOfTitle(),"title"); createQuery(boolQueryBuilder,searchDoc.getAuthor(),searchDoc.getSearchTypeOfAuthor(),searchDoc.getLinkOfAuthor(),"author"); createQuery(boolQueryBuilder,searchDoc.getContent(),searchDoc.getSearchTypeOfContent(),searchDoc.getLinkOfContent(),"pageContents"); builder.withQuery(boolQueryBuilder); SearchHits<Document> searchHits = elasticsearchRestTemplate.search(builder.build(), Document.class, IndexCoordinates.of("document")); builder = new NativeSearchQueryBuilder(); boolQueryBuilder = QueryBuilders.boolQuery(); createQuery(boolQueryBuilder,searchDoc.getContent(),searchDoc.getSearchTypeOfContent(),searchDoc.getLinkOfContent(),"content"); createQuery(boolQueryBuilder,searchDoc.getContent(),searchDoc.getSearchTypeOfContent(),searchDoc.getLinkOfContent(),"imgInfos"); builder.withHighlightFields( new HighlightBuilder.Field("content") .preTags("") .postTags("") .fragmentSize(50) ,new HighlightBuilder.Field("imgInfos") .preTags("") .postTags("") .fragmentSize(50) ); builder.withQuery(boolQueryBuilder); SearchHits<Paragraph> paragraph = elasticsearchRestTemplate.search(builder.build(), Paragraph.class, IndexCoordinates.of("paragraph")); Map<Long,List<String>> map = new HashMap<>(); for (SearchHit<Paragraph> p:paragraph){ Long id = p.getContent().getId(); List<String> field = p.getHighlightField("content"); field.addAll(p.getHighlightField("imgInfos")); if (map.containsKey(id/10000)){ map.put(id/10000,field); }else { map.get(id/10000).addAll(field); } } Pageable pageable = PageRequest.of(searchDoc.getCurrent()-1, searchDoc.getPageSize(), Sort.by(Sort.Direction.DESC,"score")); List<ReturnDoc> list = new ArrayList<>(); for(SearchHit<Document> d:searchHits){ ReturnDoc returnDoc = new ReturnDoc(); returnDoc.setDocument(d.getContent()); returnDoc.setPage(1); returnDoc.setScore(d.getScore()); returnDoc.setAbstract(map.get(d.getContent().getId())); list.add(returnDoc); } Page<ReturnDoc> page = new PageImpl<>(list,pageable, searchHits.getTotalHits()); return new Page4Navigator<>(page,5); } @GetMapping("find") public List<PdfRestInfo> find(@RequestParam(value = "title",defaultValue = "") String title,@RequestParam(value = "authors",defaultValue = "")List<String>authors,@RequestParam(value = "content",defaultValue = "")String content,@RequestParam(value = "current",defaultValue = "1")int current){ List<PdfRestInfo> pdfRestInfoList = new ArrayList<>(); List<ReturnDoc> returnDocList = findDocumentByTitle(title, current,100).getContent(); for (ReturnDoc r:returnDocList){ PdfRestInfo pdfRestInfo = new PdfRestInfo(); pdfRestInfo.setAuthors(r.getDocument().getAuthor().toString().replaceAll(",|\\[|]","")); pdfRestInfo.setFilePath(r.getDocument().getUrl()); pdfRestInfo.setName(r.getDocument().getTitle()); String str = ""; for (String s:r.getAbstract()){ str=str+s+'\n'; } pdfRestInfo.setTextSummary(str); pdfRestInfoList.add(pdfRestInfo); } return pdfRestInfoList; }
标签:boolQueryBuilder,title,冲刺,searchDoc,returnDoc,new,QueryBuilders From: https://www.cnblogs.com/lhk20213937/p/17327996.html