1. 程式人生 > >Elasticsearch(五)elasticsearch高亮搜尋

Elasticsearch(五)elasticsearch高亮搜尋

許多應用都傾向於在每個搜尋結果中 高亮 部分文字片段,以便讓使用者知道為何該文件符合查詢條件。在 Elasticsearch 中檢索出高亮片段也很容易。
再次執行前面的查詢,並增加一個新的 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外掛示例

這裡寫圖片描述