Pages

Monday 10 February 2014

Highlighted Fragments


Each field highlighted can control the size of the highlighted fragment in characters (defaults to 100), and the maximum number of fragments to return (defaults to 5).

The fragment_size is ignored when using the postings highlighter, as it outputs sentences regardless of their length.

If the number_of_fragments value is set to 0 then no fragments are produced, instead the whole content of the field is returned, and of course it is highlighted.

For example:

POST  Method

http://localhost:9200/contents/message/_search/?pretty=true
{
    "query": {
        "query_string": {
            "query": "Elasticsearch"
        }
    },
    "highlight": {
        "fields": {
            "message": {"fragment_size" : 50, "number_of_fragments" : 2}
        }
    }
}

Results:

{
  "took": 4,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 0.21650635,
    "hits": [
      {
        "_index": "contents",
        "_type": "message",
        "_id": "2",
        "_score": 0.21650635,
        "_source": {
          "message": "Elasticsearch is a flexible and powerful open source, distributed, real-time search and analytics engine. Architected from the ground up for use in distributed environments where reliability and scalability are must haves, Elasticsearch gives you the ability to move easily beyond simple full-text search.Through its robust set of APIs and query DSLs, plus clients for the most popular programming languages, Elasticsearch delivers on the near limitless promises of search technology."
        },
        "highlight": {
          "message": [
            "<em>Elasticsearch</em> is a flexible and powerful open",
            " and scalability are must haves, <em>Elasticsearch</em> gives you"
          ]
        }
      },
      {
        "_index": "contents",
        "_type": "message",
        "_id": "1",
        "_score": 0.17677669,
        "_source": {
          "message": "By combining the massively popular Elasticsearch, Logstash and Kibana we have created an end-to-end stack that delivers actionable insights in real-time from almost any type of structured and unstructured data source. Built and supported by the engineers behind each of these open source products, the Elasticsearch ELK stack makes searching and analyzing data easier than ever before."
        },
        "highlight": {
          "message": [
            "By combining the massively popular <em>Elasticsearch</em>",
            ", the <em>Elasticsearch</em> ELK stack makes searching"
          ]
        }
      }
    ]
  }
}

NOTE: I am using firefox rest client to run this example.

No comments:

Post a Comment