Search code examples
elasticsearchelasticsearch-pluginelasticsearch-java-api

Elasticsearch: finding the index of the word for which response document is returned


I am querying elasticsearch and getting an array in return. But the returned array contains multiple sentences. i want to find the index of the word for which the document is returned. The relevant part of the returned document:

"_id": "AVIcfnn-laLYKmOp14og", "_score": 0.17643881, "_routing": "1658591699", "_parent": "1658591699", "_source": { "heading": "", "paragraphs": [ "Plug And Stitch With New Compact And Portable Electric Sewing Machine It Is Suitable For Many Functions, Like You Can Do Straight Stitch, 4 Step Button Hole, Free Arm For Circular Stitch, Auto Trip Bobbin Winder, Single Touch Reverse Stitch, Good For Quilting,Lace Work,picot,smoking,cording,blind Hem Stitch,zig-zag Stitch, Etc. Smart Look With Low Maintainence And With Inbuilt Electric Motor Of 60 Watts Warranty Period Two Years , And Service Provided By Company At Your Door Step.", "The images represent actual product though color of the image and product may slightly differ.", "Apr 13, 2015" ] },

How to find the word in this data for which this document is returned?


Solution

  • Use Highlighting.

    https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-highlighting.html

    {
        "query" : {...},
        "highlight" : {
            "fields" : {
                "content" : { fragment_size : 150, number_of_fragments: 1}
            }
        }
    }
    

    Above snippet will return 1 fragment of size 150 characters for the match. Play with fragment_size and number_of_fragments for maximum effect.