-1

I have a String field countries of format eg "SS,SX,US,IND,CND,TN" and I have a input String field countryCode which will be of form "SS". How can I query all the records where a given countryCode is in countries in elasticsearch? I have tried with match and match_phrase queries but didn't get the desired results.

Query.must(QueryBuilders.matchPhraseQuery("countries", countryCode))

JonyD
  • 1,237
  • 3
  • 21
  • 34

1 Answers1

0

You can try with a bool query instead:

QueryBuilder qb = boolQuery().must(termQuery("countries", countryCode))

(Your "countries" field should not be defined as not_analyzed)

Jaime Caffarel
  • 2,401
  • 4
  • 30
  • 42
  • @Jamie Caffarel I have indexed countries with type:string and index:not_analyzed but not able to retrieve the documents. countries are like "BA,AZ,AW,AU,AT,AS,AR,AQ,AO,AN,AM,AL,AI,AG,AF,AE,AD" and I am testing with countryCode "AZ" – user2872321 Nov 06 '16 at 08:27
  • @user2872321, No, the countries field should NOT contain the `not_analyzed`. It should have been defined just as a String in the mapping (no index) – Jaime Caffarel Nov 06 '16 at 08:29
  • @Jamie Caffarel Okay got it. Will try removing the index and search for it. Thanks very much. I think i got it now. If we mention not_analyzed will it check with the entire string. Is it why it not returning? – user2872321 Nov 06 '16 at 08:33
  • @user2872321 Yes, if you set `not_analyzed` in the mapping, you will be able to retrieve just exact values of that field, so in you example, you will match exactly "SS,SX,US,IND,CND,TN", and not parts of that string. – Jaime Caffarel Nov 06 '16 at 08:37