0

I am new to Elasticsearch. I am able to run the following query in Windows prompt:

{
  "query": {
    "filtered": {
      "query": {
        "term": {
          "title": "crime"
        }
      },
      "filter": {
        "term": {
          "year": 1961
        }
      }
    }
  },
  "highlight": {
    "fields": {
      "title": {}
    }
  }
}

In Java client, I am able to create the following string and set it in Java client:

{
  "filtered": {
    "query": {
      "term": {
        "title": "crime"
      }
    },
    "filter": {
      "term": {
        "year": 1961
      }
    }
  }
}

SearchResponse sr = client.prepareSearch("book")
        .setTypes("history")
        .addHighlightedField("title")
        .setQuery(the_above_query_string)

However, as show in the above Java code, I have to add highlighted fields via .addHighlightedField("title"). Can I set the highlight fields via the following string in the above Java code (the way to set query)?

{
  "highlight": {
    "fields": {
      "title": {}
    }
  }
}
Saeed Zhiany
  • 2,051
  • 9
  • 30
  • 41
curious1
  • 14,155
  • 37
  • 130
  • 231
  • Any specific reason why you want to do it like that ? – dark_shadow Aug 03 '14 at 17:22
  • Two reasons. 1. Curious about the architectural design of Elasticsearch. I feel it is "prettier" and balanced to be able to set the highlight string. 2. If I could do it in string without Java method, I could easily change highlight config without touching Java code. Thanks very much for chiming in. – curious1 Aug 03 '14 at 20:31
  • 1
    Even if you will be able to create a highlight string like query builder, you will have to change that in java code only. Also, regarding prettier this, you can yourself see you are only telling elasticsearch what all fields you need highlighted. It is better giving those fields directly rather than giving a complex string which it needs to parse anyways. Also, in case you of queries you have a lot of options which can't be integrated in searchrequestbuilder directly, that 's the reason they give separate querbuilders but for highlighting you don't need one. – dark_shadow Aug 04 '14 at 04:02

0 Answers0