Elasticsearch Java High-level-re
先构建一个索引
PUT test_index { "mappings": { "properties": { "content": { "type": "text", "analyzer": "english" } } } }
加点数据
PUT test_index/_doc/doc1 { "content" : "For you I'm only a fox like a hundred thousand other foxes. But if you tame me, we'll need each other. You'll be the only boy in the world for me. I'll be the only fox in the world for you." }
高亮查询例子1:
GET test_index/_search { "query": { "match_phrase" : {"content" : "only fox"} }, "highlight": { "type" : "unified", "number_of_fragments" : 3, "fields": { "content": {} } } }
对应的java api实现:
public void testHighLightQuery(){
//1.Create a search request
SearchRequest searchRequest = new SearchRequest("test_index");
//2.Use SearchSourceBuilder to construct the query request body
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
//Construct QueryBuilder
QueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("content","only fox");
sourceBuilder.query(matchQueryBuilder);
//Page setting
sourceBuilder.from(0);
sourceBuilder.size(5);
//Highlight setting
HighlightBuilder highlightBuilder = new HighlightBuilder();
highlightBuilder.requireFieldMatch(false).field("content").numOfFragments(3).highlighterType("unified");
sourceBuilder.highlighter(highlightBuilder);
searchRequest.source(sourceBuilder);
try {
//3.Send a request
SearchResponse searchResponse = restClient.search(searchRequest, RequestOptions.DEFAULT);
//4.Processing the response
if(RestStatus.OK.equals(searchResponse.status())){
SearchHits hits = searchResponse.getHits();
long totalHits = hits.getTotalHits().value;
SearchHit[] searchHits = hits.getHits();
for(SearchHit hit : searchHits){
String index = hit.getIndex();
String id = hit.getId();
float score = hit.getScore();
//Take the _source filed value
Map<String,Object> sourceAsMap = hit.getSourceAsMap();
String content = (String) sourceAsMap.get("content");
log.info("index:" + index + " id:"+id);
log.info("content : " + content);
//Take the highlight result
Map<String, HighlightField> highlightFieldMap = hit.getHighlightFields();
HighlightField highlightField = highlightFieldMap.get("content");
if(highlightField != null){
Text[] fragments = highlightField.fragments();
if(fragments != null){
String fragmentStr = fragments[0].string();
log.info("content highlight : " + fragmentStr);
}
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
高亮查询Demo2:
GET /_search { "query": { "match_phrase" : {"content" : "only fox"} }, "highlight":{ "pre_tags":[ "<tag1>" ,"<tag2>" ], "post_tags":[ "</ tag1>" ,"</ tag2>"], "fields":{ "content":{} } } }
Java api demo:
public void testHighLightQuery02(){
//1.Create a search request
SearchRequest searchRequest = new SearchRequest();
//2.Use SearchSourceBuilder to construct the query request body
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
//Construct QueryBuilder
QueryBuilder matchQueryBuilder = QueryBuilders.matchPhraseQuery("content","only fox");
sourceBuilder.query(matchQueryBuilder);
//Highlight setting
HighlightBuilder highlightBuilder = new HighlightBuilder();
highlightBuilder.requireFieldMatch(false)
.preTags("<tag1>","<tag2>")
.postTags("</ tag1>","</ tag2>")
.field("content");
sourceBuilder.highlighter(highlightBuilder);
searchRequest.source(sourceBuilder);
try {
//3.Send a request
SearchResponse searchResponse = restClient.search(searchRequest, RequestOptions.DEFAULT);
//4.Processing the response
if(RestStatus.OK.equals(searchResponse.status())){
SearchHits hits = searchResponse.getHits();
long totalHits = hits.getTotalHits().value;
SearchHit[] searchHits = hits.getHits();
for(SearchHit hit : searchHits){
String index = hit.getIndex();
String id = hit.getId();
float score = hit.getScore();
//Take the _source filed value
Map<String,Object> sourceAsMap = hit.getSourceAsMap();
String content = (String) sourceAsMap.get("content");
log.info("index:" + index + " id:"+id);
log.info("content : " + content);
//Take the highlight result
Map<String, HighlightField> highlightFieldMap = hit.getHighlightFields();
HighlightField highlightField = highlightFieldMap.get("content");
if(highlightField != null){
Text[] fragments = highlightField.fragments();
if(fragments != null){
String fragmentStr = fragments[0].string();
log.info("content highlight : " + fragmentStr);
}
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}