Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

property totalHits is always 0 on BoolQuery Search response #53267

Closed
durancu opened this issue Mar 9, 2020 · 13 comments
Closed

property totalHits is always 0 on BoolQuery Search response #53267

durancu opened this issue Mar 9, 2020 · 13 comments

Comments

@durancu
Copy link

durancu commented Mar 9, 2020

Describe the feature: totalHits property always returns 0, but hits contains items

Elasticsearch version (bin/elasticsearch --version): 7.6

Plugins installed: []

JVM version (java -version): 8

OS version (uname -a if on a Unix-like system): MacOS

Description of the problem including expected versus actual behavior:
I am using Java REST Client to implement a microservice that integrates to Elasticsearch (via AWS).
When I am debugging, I see totalHits is coming always as zero (0),
If I run same query in Kibana, it returns the right value of totalHits.

I create a boolQuery, with several must queries inside it. For the example I am debugging, I have a BoolQueryBuilder object, and the generated query is:

{
  "bool" : {
    "must" : [
      {
        "match_phrase" : {
          "wellSiteName.keyword" : {
            "query" : "XXXXXXX 501H, 502H",
            "slop" : 0,
            "zero_terms_query" : "NONE",
            "boost" : 1.0
          }
        }
      }
    ],
    "adjust_pure_negative" : true,
    "boost" : 1.0
  }
}

This is my request object toString:

SearchRequest{searchType=QUERY_THEN_FETCH, indices=[], indicesOptions=IndicesOptions[ignore_unavailable=false, allow_no_indices=true, expand_wildcards_open=true, expand_wildcards_closed=false, allow_aliases_to_multiple_indices=true, forbid_closed_indices=true, ignore_aliases=false], types=[], routing='null', preference='null', requestCache=null, scroll=null, maxConcurrentShardRequests=0, batchedReduceSize=512, preFilterShardSize=128, allowPartialSearchResults=null, source={"query":{"bool":{"must":[{"match_phrase":{"wellSiteName.keyword":{"query":"WINNEBAGO 30 STATE COM 501H, 502H","slop":0,"zero_terms_query":"NONE","boost":1.0}}}],"adjust_pure_negative":true,"boost":1.0}}}}

This is the response I get:

Screen Shot 2020-03-09 at 1 19 44 AM

As you can see, despites there are many hits, totalHits property is 0

However, if I run same query in Kibana, I get a number of totalHits matching that query, that is not 0

Screen Shot 2020-03-09 at 1 22 23 AM

Steps to reproduce:

1- Create a BoolQueryBuilder object
2- Add a must query to the object, using matchPhraseQuery, on a keyword column
3- Create a SearchRequest object
4- Create a SearchSourceBuilder object
5- Add BoolQueryBuilder object to SearchSourceBuilder object as query
6- Set SearchSourceBuilder object as source of SearchRequest.
7- Call Elasticsearch client using SearchRequest object and RequestOptions.DEFAULT

@durancu durancu changed the title totalHits is always 0 when search property totalHits is always 0 on BoolQuery Search response Mar 9, 2020
@martijnvg
Copy link
Member

Thanks for opening this issue @durancu. Maybe the default (10000) for track_total_hits gets changed from interacting via the hlrc.

If you add: searchSourceBuilder .trackTotalHitsUpTo(10000); does total hits get returned via hlrc?

I'm not sure yet why you always get zero total hits via hlrc if you don't alter the search source build's track total hits.

@elasticmachine
Copy link
Collaborator

Pinging @elastic/es-core-features (:Core/Features/Java High Level REST Client)

@durancu
Copy link
Author

durancu commented Mar 9, 2020

Hi @martijnvg, thanks for your quick response
I can't call that method. Here is what intelliJ shows on autocomplete:

Screen Shot 2020-03-09 at 4 53 32 AM

@martijnvg
Copy link
Member

That is odd. What version of hlrc are you using on the client side?

@durancu
Copy link
Author

durancu commented Mar 9, 2020

I am using 7.6

Screen Shot 2020-03-09 at 5 10 19 AM

But I just realized I was using 6.4.2 before. Recently updated to 7.6, but still getting same autocomplete

@martijnvg
Copy link
Member

martijnvg commented Mar 9, 2020

In 7.6 this is a public method: https://github.com/elastic/elasticsearch/blob/7.6/server/src/main/java/org/elasticsearch/search/builder/SearchSourceBuilder.java#L584

In older 6.x releases this method didn't exist.
Perhaps re-import your project in intellij?

@durancu
Copy link
Author

durancu commented Mar 9, 2020

I purged my local Maven repository, and reimport all dependencies.
This is how my dependencies graph looks like:

Screen Shot 2020-03-09 at 12 25 10 PM

And have noted that elasticsearch elasticsearch-rest-high-level-client (v. 7.6.0) depends on elasticsearch-rest-client (v. 6.4.2).

Since I am using objects from rest-client, like SearchSourceBuilder, that's why I can access latest version of rest-client.

See dependencies tree from Intellij:

Screen Shot 2020-03-09 at 12 23 31 PM

@dadoonet
Copy link
Member

dadoonet commented Mar 9, 2020

Are you using spring boot by any chance ?

@durancu
Copy link
Author

durancu commented Mar 9, 2020

Yes, I am using Spring Boot. Sorry forgot to mention

@dadoonet
Copy link
Member

dadoonet commented Mar 9, 2020

When using springboot with
elasticsearch, you need to be explicit with some transitive dependencies as SpringBoot declares a version 6.4...

Basically you can put this in your pom.xml:

<properties>
  <elasticsearch.version>7.6.1<elasticsearch.version>
</properties>

See documentation here: https://docs.spring.io/spring-boot/docs/current/reference/html/howto.html#howto-customize-dependency-versions

@durancu
Copy link
Author

durancu commented Mar 9, 2020

Thanks @dadoonet for your reply.
I applied that change but also add:

           <dependency>
                <groupId>org.elasticsearch</groupId>
                <artifactId>elasticsearch</artifactId>
                <version>7.6.1</version>
            </dependency>

to section, and it didn't work.

I am see this is something related to SpringBoot more than ElasticSearch.
I understand they tie SpringBoot version to ElasticSearch version, and I would need to find a way to override it.
Do you have any idea about how to do it?

@dadoonet
Copy link
Member

dadoonet commented Mar 9, 2020

I'm just aware that spring data elasticsearch has a new beta version IIRC which is compatible with 7.x so may be you need to use that?

@durancu
Copy link
Author

durancu commented Mar 19, 2020

Solution was to override version of spring-boot dependencies (elasticsearch, elasticsearch-client):

1- Define property for elasticsearch version:

<properties>
   <elasticsearch.version>7.6.0</elasticsearch.version>
</properties>

2- Exclude autoconfigured dependencies from elasticsearch-rest-high-level-client

<dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-high-level-client</artifactId>
            <version>${elasticsearch.version}</version>
            <exclusions>
                <exclusion>
                    <groupId>org.elasticsearch</groupId>
                    <artifactId>elasticsearch</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.elasticsearch.client</groupId>
                    <artifactId>elasticsearch-rest-client</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

3- Call elasticsearch dependencies by themselves, using same version:

<dependency>
          <groupId>org.elasticsearch</groupId>
          <artifactId>elasticsearch</artifactId>
          <version>${elasticsearch.version}</version>
      </dependency>
      <dependency>
          <groupId>org.elasticsearch.client</groupId>
          <artifactId>elasticsearch-rest-client</artifactId>
          <version>${elasticsearch.version}</version>
      </dependency>

This is a way to override the version, but need to be careful to prevent incompatibilities with forced dependency version.
This luckily worked for me to use Elasticsearch 7.6 in Springboot 2.1

@durancu durancu closed this as completed Mar 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants