javaelasticsearchelastic-stackelk

Java Elastic query not returning results


I have an application which uses java HLRC client fetching results from Elasticsearch cluster. I am facing a peculiar problem in which I am not able to get certain results from Java client side. But the catch is when i am firing the underlying query of Java client in kibana devtools I am getting valid results. I am just not sure what I am doing wrong.

This issue is just happening for a particular field as there are multiple fields and when I fire query on those fields I am getting correct results. So the issue that my whole Java logic is wrong might not be the case, it's just that one specific field which is when queried does not give me desired results.

I am pasting the mapping, the query and the spring boot code here ( Please make in mind the details here are sanitised)

Mapping of the particular field

"abcField": {
    "type": "text",
    "fields": {
      "keyword": {
        "type": "keyword",
        "ignore_above": 256
      }
    }
  }

Springboot Query

query = QueryBuilders.termQuery("abcField", xyz);
boolQueryBuilder = QueryBuilders.boolQuery().must(query).must(QueryBuilders.rangeQuery(timeField)
                  .from(startTime).to(endTime).format(date_optional_time));

Generated Query

    {
  "bool" : {
    "must" : [
      {
        "term" : {
          "abcField.keyword" : {
            "value" : "xyz",
            "boost" : 1.0
          }
        }
      },
      {
        "range" : {
          "timeField" : {
            "from" : "2025-01-28T11:34:31.427",
            "to" : "2025-01-28T11:56:31.428",
            "include_lower" : true,
            "include_upper" : true,
            "format" : "date_optional_time",
            "boost" : 1.0
          }
        }
      }
    ],
    "adjust_pure_negative" : true,
    "boost" : 1.0
  }
}

Now I know that the term queries are not analysed that's why I am using the keyword field here. This query when fired in kibana dev tools gives me correct results but the same in java gives me 0 results.


Solution

  • Text field vs Keyword Field ?

    While I haven't tested it myself I could not help but notice that:

    Sringboot code

    query = QueryBuilders.termQuery("abcField", xyz);
                                     ^^^^^^^^
    

    You reference the abcField

    Generated Query

    {
        "term" : {
            "abcField.keyword" : {
                "value" : "xyz",
                "boost" : 1.0
            }
        }
    }
    

    Which reference the field abcField.keyword

    Are you sure there is not a typo on you code ? Shouldn't it be

    query = QueryBuilders.termQuery("abcField.keyword", xyz);
                                     ^^^^^^^^^^^^^^^^
    

    To better investigate

    The explain api

    There is the explain api that can help you understand why or why not documents are matching.

    Debugging the Elasticsearch springboot

    The following post explains how to debug the application.

    Hope this helps!