Sebastien Lorber
Sebastien Lorber

Reputation: 92210

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

Upvotes: 2

Views: 1581

Answers (3)

jo se
jo se

Reputation: 43

If the source is not deactivated by default you can:

{
    "_source" :  ["_id"],
    "query": {
        "match" : {
            "attachment.content" : "Setup"
        }
    },
    "highlight": {
        "fields" : {
            "attachment.content" : {}
        }
    }
}

You have to put something in the _score. It still returns every "metadata" about the document it found:

{
    "took": 4,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 1,
        "max_score": 0.2919385,
        "hits": [
            {
                "_index": "test",
                "_type": "_doc",
                "_id": "xpto",
                "_score": 0.2919385,
                "_source": {},
                "highlight": {
                    "attachment.content": [
                        "<em>Setup</em> the [GenericCommand.properties] file\n\nThe commands that ought to be recognized have to be defined"
                    ]
                }
            }
        ]
    }
}

Upvotes: 0

AMIT KUMAR
AMIT KUMAR

Reputation: 9

{
  "query": {
    "query_string": {
      "query": "**",
      "fields["
      sometext "]}},"
      highlight {
        "pre_tags": ["<em>"],
        "post_tags[</em>"],
      "order": "score",
      "require_field_match": true,
      "fields": {
        "sometext": {
          "fragment_size": 180,
          "number_of_fragments": 1
        }
      }
    }
  }

Upvotes: 1

farid
farid

Reputation: 379

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.

Upvotes: 3

Related Questions