Search code examples
elasticsearchelasticsearch-java-api

searching for a word in a string indexed field in elasticsearch java


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))


Solution

  • 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)