How can I create a simple match query using Java client?

I am using the Java Client to form match queries but unable to do so. I found the same issue had been raised in ElasticSearch project.

Here is the link to the original issue.

I have run into the same issue. How can I perform a simple match query like below?

{
    "query": {
        "match": {
            "lastName": "Bruce"
        }
    }
}
SearchRequest searchRequest = new SearchRequest.Builder()
        .index("sample-index")
        .query(q -> q
                .match(
                        // what to use here ??
                ))
        .build();

The solution suggests to use SearchSourceBuilder class which is not even available in the OpenSource client jars.

Kindly let me know the solution.

1 Like

Hey!

This is super interesting, but it seems like the only thing you could do is:

SearchRequest searchRequest = new SearchRequest.Builder()
    .index("sample-index")
    .query(q -> q.match(new NamedQuery.Builder<JsonValue>().name("Bruce").build()))
    .build();

But that would generated the following query (obviously, not what you want):

{ 
  "query": {
    "match": {
      "_name":"firstName"
    }
  }
}

Out of all options, probably you could use the different type of query, like queryString, for example:

SearchRequest searchRequest = new SearchRequest.Builder()
    .index("sample-index")
    .query(q -> q.queryString(qs -> qs.fields("lastName").query("Bruce")))
    .build();

It seems like issues with autogeneration are still not solved sadly.
Hope it helps.