How to provide highlighting with Spring data elasticsearch

后端 未结 4 932
死守一世寂寞
死守一世寂寞 2020-12-29 00:11

it seems that SpringData ES don\'t provide classes to fetch highlights returned by ES. Spring Data can return Lists of Objects but the highlights sections in the Json return

4条回答
  •  庸人自扰
    2020-12-29 00:46

    Spring Data Elasticsearch 4.0 now has the SearchPage result type, which makes things a little easier if we need to return highlighted results:

    This is a working sample:

        String query = "(id:123 OR id:456) AND (database:UCLF) AND (services:(sealer?), services:electronic*)"
        
        NativeSearchQuery searchQuery = new NativeSearchQueryBuilder()
                .withPageable(pageable)
                .withQuery(queryStringQuery(query))
                .withSourceFilter(sourceFilter)
                .withHighlightFields(new HighlightBuilder.Field("goodsAndServices"))
                .build();
        
        
        SearchHits searchHits = template.search(searchQuery, Trademark.class, IndexCoordinates.of("trademark"));
        SearchPage page = SearchHitSupport.searchPageFor(searchHits, searchQuery.getPageable());
        return (Page) SearchHitSupport.unwrapSearchHits(page);
    

    And this would be the response from Page object in json:

    {
        "content": [
            {
                "id": "123",
                "score": 12.10748,
                "sortValues": [],
                "content": {
                    "_id": "1P0XzXIBdRyrchmFplEA",
                    "trademarkIdentifier": "abc234",
                    "goodsAndServices": null,
                    "language": "EN",
                    "niceClass": "2",
                    "sequence": null,
                    "database": "UCLF",
                    "taggedResult": null
                },
                "highlightFields": {
                    "goodsAndServices": [
                        "VARNISHES, SEALERS, AND NATURAL WOOD FINISHES"
                    ]
                }
            }
        ],
        "pageable": {
            "sort": {
                "unsorted": true,
                "sorted": false,
                "empty": true
            },
            "offset": 0,
            "pageNumber": 0,
            "pageSize": 20,
            "unpaged": false,
            "paged": true
        },
        "searchHits": {
            "totalHits": 1,
            "totalHitsRelation": "EQUAL_TO",
            "maxScore": 12.10748,
            "scrollId": null,
            "searchHits": [
                {
                    "id": "123",
                    "score": 12.10748,
                    "sortValues": [],
                    "content": {
                        "_id": "1P0XzXIBdRyrchmFplEA",
                        "trademarkIdentifier": "abc234",
                        "goodsAndServices": null,
                        "language": "EN",
                        "niceClass": "2",
                        "sequence": null,
                        "database": "UCLF",
                        "taggedResult": null
                    },
                    "highlightFields": {
                        "goodsAndServices": [
                            "VARNISHES, SEALERS, AND NATURAL WOOD FINISHES"
                        ]
                    }
                }
            ],
            "aggregations": null,
            "empty": false
        },
        "totalPages": 1,
        "totalElements": 1,
        "size": 20,
        "number": 0,
        "numberOfElements": 1,
        "last": true,
        "first": true,
        "sort": {
            "unsorted": true,
            "sorted": false,
            "empty": true
        },
        "empty": false
    }
    

提交回复
热议问题