Elasticsearch Java High-level-rest-client Api:高亮查询

先构建一个索引
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();
    }
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容