Skip to content

Commit

Permalink
test: elasticsearch query highlight
Browse files Browse the repository at this point in the history
  • Loading branch information
novohit committed Nov 11, 2023
1 parent da241f8 commit 0b93722
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
35 changes: 35 additions & 0 deletions data-sync/src/main/java/org/novo/datasync/demo/TestController.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,16 @@
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.Criteria;
import org.springframework.data.elasticsearch.core.query.CriteriaQuery;
import org.springframework.data.elasticsearch.core.query.HighlightQuery;
import org.springframework.data.elasticsearch.core.query.highlight.Highlight;
import org.springframework.data.elasticsearch.core.query.highlight.HighlightField;
import org.springframework.data.elasticsearch.core.query.highlight.HighlightFieldParameters;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
Expand Down Expand Up @@ -57,4 +63,33 @@ public void query(@RequestParam("keyword") String keyword) {
List<UserDoc> res = searchHits.get().map(SearchHit::getContent).toList();
log.info("{}", res);
}

@GetMapping("/query/highlight")
public void queryHighlight(@RequestParam("keyword") String keyword) {
Criteria criteria = new Criteria();
criteria.and(new Criteria("username").matches(keyword));
CriteriaQuery criteriaQuery = new CriteriaQuery(criteria);
HighlightFieldParameters parameters = new HighlightFieldParameters.HighlightFieldParametersBuilder()
.withPreTags(new String[]{"<span style='color:red'>"})
.withPostTags(new String[]{"</span>"})
.build();
criteriaQuery.setHighlightQuery(new HighlightQuery(new Highlight(Arrays.asList(new HighlightField("username", parameters))), UserDoc.class));
SearchHits<UserDoc> searchHits = restTemplate.search(criteriaQuery, UserDoc.class);

List<UserDoc> resp = new ArrayList<>();
for (SearchHit<UserDoc> searchHit : searchHits) {
UserDoc content = searchHit.getContent();
//将高亮的字段取出来
List<String> requestBody = searchHit.getHighlightField("username");
StringBuilder highText = new StringBuilder();
for (String s : requestBody) {
highText.append(s);
}
//重新对字段赋值
content.setUsername(highText.toString());
resp.add(content);
}

log.info("{}", resp);
}
}
4 changes: 2 additions & 2 deletions data-sync/src/main/java/org/novo/datasync/demo/UserDoc.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ public class UserDoc {
@Id
private Long id;

@Field(type = FieldType.Text, searchAnalyzer = "ik_max_word")
@Field(type = FieldType.Text, analyzer = "ik_max_word")
private String username;

@Field(type = FieldType.Text, searchAnalyzer = "ik_max_word")
@Field(type = FieldType.Text, analyzer = "ik_max_word")
private String password;
}

0 comments on commit 0b93722

Please sign in to comment.