Read/Modify/Send QueryDSL in Java

Hello, I am having trouble finding Elasticsearch Java code that will accept a full QueryDSL object as a String. My basic use case is as follows:

  1. Our UI code is set up to generate QueryDSL based on user input and user searches
  2. The search does not call Elasticsearch directly. Instead, the search calls an internal API that first does some user authentication and other validations before calling Elasticsearch
  3. Based on the authentication/validation, the API may also need to add additional search criteria to the QueryDSL and then call Elasticsearch

I checked the Elasticsearch documentation and searched forum threads, and also attempted to draft up this code, but I cannot find anywhere in the Elasticsearch libraries (including spring-data-elasticsearch) that allows for direct handling or manipulation of a QueryDSL String object. Does such a class/method exist? For example, I was hoping that QueryBuilder would have such a function as:
QueryBuilders.fullDslQuery(myQueryDsl);
where I could feed this String and get results:
{
"query": {
"match_all": {}
}
}

I found a partial answer: QueryBuilders.wrapperQuery() accepts a full query as input. The problem is that it only accepts query data, and does not accept pagination entries nor aggregations. Using this call as an example:
{
"from" : 0, "size" : 20,
"query": { "match": { "account.id": "123" } }
}

QueryBuilder cannot handle the pagination values, because wrapperQuery() assumes you are inside the "query" method already. The java code for the above function is therefore:
QueryBuilders.wrapperQuery("\"query\": { \"match\": { \"account.id\": \"123\" } }")

Is there a way to accept the exact text from cURL? I am hoping that the pagination values can be sent from the client end without having to handle it on the Java side.

Is there a way to accept the exact text from cURL?

Not with QueryBuilders.wrapperQuery.

You can use the low level client: https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-low-usage-requests.html

But it will give you back just a JSON response that you will have to parse then.

Thanks so much, this is exactly what I was looking for. I will likely use a conversion library to handle the string response.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.