Highlighting in Elasticsearch

I am using the Below Query

GET books/_search
{
  "query" :
    {
        "query_string":
        {
           "query": "java scala"
           
        }
    },
  "aggs": {
    "my_agg": {
      "terms": {
        "field": "language",
        "missing": "N/A"
      }
    }
  },
  "highlight": {
    "fields": { 
     "title" : {
                "fragment_size" : 15,
                "number_of_fragments" : 3,
                "fragmenter": "span"
            }
    }
   
  }
}

I can not see the Highlight tag into the Json Output..My Json Output is as Below:-

{
  "took": 6,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 3,
    "max_score": 0.7180147,
    "hits": [
      {
        "_index": "books",
        "_type": "book",
        "_id": "3",
        "_score": 0.7180147,
        "_source": {
          "title": "Programming in Scala: A Comprehensive Step-by-Step Guide, 2nd Edition",
          "author": "Martin Odersky",
          "language": "Scala",
          "publishYear": 2011,
          "summary": "Scala is an object-oriented programming language for the Java Virtual Machine. In addition to being object-oriented, Scala is also a functional language, and combines the best approaches to OO and functional programming."
        }
      },
      {
        "_index": "books",
        "_type": "book",
        "_id": "4",
        "_score": 0.64829546,
        "_source": {
          "title": "Hadoop: The Definitive Guide, 4th Edition",
          "author": "Tom White",
          "language": "Java",
          "publishYear": 2015,
          "summary": "Get ready to unlock the power of your data. With the fourth edition of this comprehensive guide, you’ll learn how to build and maintain reliable, scalable, distributed systems with Apache Hadoop. This book is ideal for programmers looking to analyze datasets of any size, and for administrators who want to set up and run Hadoop clusters."
        }
      },
      {
        "_index": "books",
        "_type": "book",
        "_id": "1",
        "_score": 0.48414356,
        "_source": {
          "title": "Effective Java",
          "author": "Josh Bloch",
          "language": "Java",
          "publishYear": 2008,
          "summary": "Are you looking for a deeper understanding of the Java programming language so that you can write code that is clearer, more correct, more robust, and more reusable? Look no further! Effective Java, Second Edition, brings together seventy-eight indispensable programmer’s rules of thumb: working, best-practice solutions for the programming challenges you encounter every day."
        }
      }
    ]
  },
  "aggregations": {
    "my_agg": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "java",
          "doc_count": 2
        },
        {
          "key": "scala",
          "doc_count": 1
        }
      ]
    }
  }
}

Pls Guide..

Try this in your query:

"query_string":
        {
           "query": "java scala",
           "fields": ["title"]     
        }

@pokaleshrey yes this Works ... But ..How Can I search the title and Summary field Both...

Specifying the field Shall Limit the No of Results...
For example :-

I tried the Below Query

GET books/_search
{
  "query" :
    {
       
        "query_string":
        {
           "query": "java scala",
           "fields": ["title","summary"]     
        }
    },
  "aggs": {
    "my_agg": {
      "terms": {
        "field": "language",
        "missing": "N/A"
      }
    }
  },
  "highlight": {
    "fields": { 
     "title" : {
                "fragment_size" : 15,
                "number_of_fragments" : 3,
                "fragmenter": "span"
            },
            "summary" : {
                "fragment_size" : 15,
                "number_of_fragments" : 3,
                "fragmenter": "span"
            }
    }
   
  }
}

and got the below result

{
  "took": 12,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 0.63948095,
    "hits": [
      {
        "_index": "books",
        "_type": "book",
        "_id": "3",
        "_score": 0.63948095,
        "_source": {
          "title": "Programming in Scala: A Comprehensive Step-by-Step Guide, 2nd Edition",
          "author": "Martin Odersky",
          "language": "Scala",
          "publishYear": 2011,
          "summary": "Scala is an object-oriented programming language for the Java Virtual Machine. In addition to being object-oriented, Scala is also a functional language, and combines the best approaches to OO and functional programming."
        },
        "highlight": {
          "summary": [
            "<em>Scala</em> is an",
            " <em>Java</em> Virtual",
            ", <em>Scala</em> is also a"
          ],
          "title": [
            " <em>Scala</em>: A"
          ]
        }
      },
      {
        "_index": "books",
        "_type": "book",
        "_id": "1",
        "_score": 0.3802836,
        "_source": {
          "title": "Effective Java",
          "author": "Josh Bloch",
          "language": "Java",
          "publishYear": 2008,
          "summary": "Are you looking for a deeper understanding of the Java programming language so that you can write code that is clearer, more correct, more robust, and more reusable? Look no further! Effective Java, Second Edition, brings together seventy-eight indispensable programmer’s rules of thumb: working, best-practice solutions for the programming challenges you encounter every day."
        },
        "highlight": {
          "summary": [
            " of the <em>Java</em>",
            " <em>Java</em>, Second"
          ],
          "title": [
            "Effective <em>Java</em>"
          ]
        }
      }
    ]
  },
  "aggregations": {
    "my_agg": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "java",
          "doc_count": 1
        },
        {
          "key": "scala",
          "doc_count": 1
        }
      ]
    }
  }
}

As You can See that there is a Decrease in the search result...

Pls Guide

Last option from my side: Try this:

{
  "query" :
    {
        "query_string":
        {
           "query": "java scala"
           
        }
    },
  "highlight": {
    "fields": { 
     "title" : {
                "fragment_size" : 15,
                "number_of_fragments" : 3,
                "fragmenter": "span"
            }
    },
    "require_field_match": false
   
  }
}

@pokaleshrey..Yes This worked.... Thanks :smile:

I Just Modified the Query a Bit :slight_smile:

 GET books/_search
    {
      
      "query" :
        {
            "query_string":
            {
               "query": "java scala"
               
            }
        },
        "aggs": {
        "my_agg": {
          "terms": {
            "field": "language",
            "missing": "N/A"
          }
        }
      },
      "highlight": {
        "fields": { 
         "title" : {
                    "fragment_size" : 15,
                    "number_of_fragments" : 3,
                    "fragmenter": "span"
                }
        },
        "require_field_match": false
       
      
    }
    }

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.