1

In our app, we are synchronizing some of the data to elasticsearch, and some of this data is users' records. The app is grails 5.1 and we are using Elasticsearch Java API Client for elasticsearch integration.

The indexing is working perfectly fine, and an example of user data looks like this: enter image description here

Now, we have this following function that suppose to get the list of users by their ids:

PublicUser[] getAllByIds(Long[] ids) {
        MgetRequest request = new MgetRequest.Builder()
                .ids(ids.collect { it.toString() }.toList())
                .index("users")
                .build()
        MgetResponse<PublicUser> response = elasticSearchClientProviderService.getClient().mget(
                request,
                PublicUser.class
        )

        response.docs().collect {
            it.result().source()
        }
    }

And when the response holds at least one user record, we are getting a list of PulicUser objects -> as expected.

However, if the search result is empty, the eventual return from this function is a list with one null element.

Some investigation

response.docs() holds a single non-existing document (looks like this one is filled with the request data). enter image description here

And, as a result, the return from this function is (as I mentioned above) list of one null element. enter image description here

Another observation:

I expected that response object will have .hits(), for the actual results are accessible through: response.hits().hits(). But now of that exist.

The only season I started looking into docs() directly is because if this documentation: https://www.elastic.co/guide/en/elasticsearch/reference/master/docs-multi-get.html

There is a lack of Elasticsearch Java API Client docs. They mostly refer to REST API docs.

What is the correct way to get the list of results from mget request?

Shurik Agulyansky
  • 2,607
  • 2
  • 34
  • 76

1 Answers1

0

For now, I am solving this the following way. Will be glad to see if there is a better way, though.

PublicUser[] getAllByIds(Long[] ids) {
        MgetRequest request = new MgetRequest.Builder()
                .ids(ids.collect { it.toString() }.toList())
                .index("users")
                .build()
        MgetResponse<PublicUser> response = elasticSearchClientProviderService.getClient().mget(
                request,
                PublicUser.class
        )


        List<PublicUser> users = []
        response.docs().each {
            if (it.result().found()) {
                users.add(it.result().source())
            }
        }

        users
    }
Shurik Agulyansky
  • 2,607
  • 2
  • 34
  • 76