admin管理员组

文章数量:1417045

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.

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.

Share Improve this question asked Jan 31 at 10:49 Umang PachauryUmang Pachaury 4113 bronze badges 2
  • Try writing a query with only abcField and the date field individually, and see which one is not giving the expected results – Charchit Kapoor Commented Feb 3 at 5:45
  • @Umang Pachaury: can you log the exact query from Java code? How do you know they are exactly the same? It is really weird if a query works some time and doesn't work in other times. Maybe there are a variable - for example, the date time. Which timezone the date are in? Java libraries might do some weird conversion if the timezone is not indicated. – Hoàng Long Commented Feb 6 at 5:00
Add a comment  | 

1 Answer 1

Reset to default 2 +50

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!

本文标签: elasticsearchJava Elastic query not returning resultsStack Overflow