Elasticsearch-5-高亮搜索

许多应用都倾向于在每个搜索结果中 高亮 部分文本片段,以便让用户知道为何该文档符合查询条件。在 Elasticsearch 中检索出高亮片段也很容易。

[AdSense-A]
再次执行前面的查询,并增加一个新的 highlight 参数:

GET /megacorp/employee/_search {     "query" : {         "match_phrase" : {             "about" : "rock climbing"         }     },     "highlight": {         "fields" : {             "about" : {}         }     } } 

当执行该查询时,返回结果与之前一样,与此同时结果中还多了一个叫做 highlight 的部分。这个部分包含了 about 属性匹配的文本片段,并以 HTML 标签 封装:

{    ...    "hits": {       "total":      1,       "max_score":  0.23013961,       "hits": [          {             ...             "_score":         0.23013961,             "_source": {                "first_name":  "John",                "last_name":   "Smith",                "age":         25,                "about":       "I love to go rock climbing",                "interests": [ "sports", "music" ]             },             "highlight": {                "about": [                   "I love to go <em>rock</em> <em>climbing</em>"                  ]             }          }       ]    } } 

Client程序演示

增加一个方法:

/**      * 高亮搜索      * SearchRequestBuilder中的addHighlightedField()方法可以定制在哪个域值的检索结果的关键字上增加高亮      * @param client      */     private static void findEmployeesWithHighlight(Client client,String field,String phrase,String highlightField) {         HighlightBuilder highlightBuilder = new HighlightBuilder().field(highlightField);           highlightBuilder.preTags("<em>");           highlightBuilder.postTags("</em>");           SearchRequestBuilder request = client.prepareSearch("megacorp")                 .setTypes("employee")                 .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)                  .setQuery(QueryBuilders.boolQuery().must(matchPhraseQuery(field, phrase)))                 .highlighter(highlightBuilder);         SearchResponse response = request.get();          //处理结果         SearchHits hits = response.getHits();         Iterator<SearchHit> iterator = hits.iterator();         while(iterator.hasNext()) {             SearchHit hit = iterator.next();             Map<String, HighlightField> highlightFields = hit.getHighlightFields();               Map<String, Object> source = hit.getSource();              //获取高亮结果             Set<String> set = highlightFields.keySet();             for (String str : set) { //              System.out.println("key="+str+" value="+highlightFields.get(str));                  HighlightField highLight = highlightFields.get(str);                 String name = highLight.getName();                  Text[] texts = highLight.getFragments();                 for (int i = 0; i < texts.length; i++) {                     Text text = texts[i];                     System.out.println(name+"="+text);                 }              }          }       } 

主方法中增加调用:

// 7.highlight高亮搜索             findEmployeesWithHighlight(client, "about", "rock climbing", "about"); 

结果运行显示:

about=I love to go <em>rock</em> <em>climbing</em> about=I love to go <em>rock</em> <em>climbing</em> about=I love to go <em>rock</em> <em>climbing</em> about=I love to go <em>rock</em> <em>climbing</em> about=I love to go <em>rock</em> <em>climbing</em> about=I love to go <em>rock</em> <em>climbing</em> about=I love to go <em>rock</em> <em>climbing</em>

head插件示例

Elasticsearch(五)elasticsearch高亮搜索

打赏作者

您的支持将鼓励我们继续创作!

[微信] 扫描二维码打赏

[支付宝] 扫描二维码打赏

正在跳转到PayPal...

healthsun Author

发表评论

电子邮件地址不会被公开。 必填项已用*标注