Search code examples
javaelasticsearchquery-builderelasticsearch-jest

Not able to Query alphanumeric fields from ELASTIC SEARCH using TERMS QUERY


I am trying to query Alphanumeric values from the index using TERMS QUERY, But it is not giving me the output.

Query:

{
  "size" : 10000,
  "query" : {
    "bool" : {
      "must" : {
        "terms" : {
          "caid" : [ "A100945","A100896" ]
        }
      }
    }
  },
  "fields" : [ "acco", "bOS", "aid", "TTl", "caid" ]
}

I want to get all the entries that has caid A100945 or A100896

The same query works fine for NUmeric fields.

I am not planning to use QueryString/MatchQuery as i am trying to build general query builder that can build query for all the request. Hence am looking to get the entries usinng TERMS Query only.

Note: I am using Java API org.elasticsearch.index.query.QueryBuilders for building the Query.

eg: QueryBuilders.termQuery("caid", "["A10xxx", "A101xxx"]")

Please help.

Regards, Mik


Solution

  • If you have not customized the mappings/analysis for the caid-field, then your values are indexed as e.g. a100945, a100896 (note the lowercasing.)

    The terms-query does not do query-time text-analysis, so you'll be searching for A100945 which does not match a100945.

    This is quite a common problem, and is explained a bit more in this article on Troubleshooting Elasticsearch searches, for Beginners.