Search code examples
lucenehighlightelasticsearch

Use highlight in ElasticSearch with _source:false


Just wanted to know. Is it possible to highlight text in ElasticSearch on an index with _source = false ?

I mean i know if ES doesn't have the document he can't do the highlight but is there a way to just use ES as an highlight engine instead of a full search engine with highlights? (I provide the full document in the highlight query)

Thanks


Solution

  • I don't believe it's possible.

    However you can use _analyze on your search query and document and then compare tokens to highlight in your code.

    For example:

    curl -XGET 'localhost:9200/test/_analyze?analyzer=snowball' -d 'some search query keywords'
    

    {"tokens":[{"token":"some","start_offset":0,"end_offset":4,"type":"","position":1},{"token":"search","start_offset":5,"end_offset":11,"type":"","position":2},{"token":"query","start_offset":12,"end_offset":17,"type":"","position":3},{"token":"keyword","start_offset":18,"end_offset":26,"type":"","position":4}]}

    curl -XGET 'localhost:9200/test/_analyze?analyzer=snowball' -d '$document_text'

    {"tokens":..}

    Then look for those token matches in document and offsets should provide you with correct highlight location in document.