1

Is there an easy way to get from a kibana query language string to a java elasticsearch query?

EDIT: For example kibana query language string: foo:bar or test:123

could be solved with java like BoolQueryBuilder queryBuilder1 = QueryBuilders.boolQuery().should(matchQuery("foo","bar")).minimumShouldMatch(1); BoolQueryBuilder queryBuilder2 = QueryBuilders.boolQuery().should(matchQuery("test,"123")).minimumShouldMatch(1); BoolQueryBuilder queryBuilder1 = QueryBuilders.boolQuery().filter(List of queryBuilder1 and 2)

2 Answers 2

1

You can create an object of QueryBuider:

QueryBuilder queryBuilder = QueryBuilders.queryStringQuery("foo:bar or test:123");

After that just add it to the your SearchSourceBuilder:

SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(queryBuilder);

After add source to your SearchRequest and it should work fine:

searchRequest.source(searchSourceBuilder);
Sign up to request clarification or add additional context in comments.

Comments

0

yes, there is:

  1. Open Kibana and execute your KQL
  2. Click on Inspect just above the query input field enter image description here
  3. In the slider appeared on the right side, click Request enter image description here
  4. Find the "query" section in the shown JSON enter image description here

4 Comments

Thanks for your detailed explanation of the kibana ui feature to look at the request against the elastic api. I think it is verys helpfull for people who dont know about. But my question is for the java query. So i need something that translate cloud.maschine.type:"n1-standard-32" to QueryBuilders.boolQuery.should("n1-standard-32").minimumShouldMatch(1); Do you know what i mean?
Got your point. The High Level Rest Client is based on elastic Query-DLS and the KQL is just a kibana thing. I would try to translate the KQL to Query-DSL in the first step - I can´t find a java lib for this, but if you manage this, you'll have a Query-DSL JSON. In the next step use the XContent API to parse the JSON (see XContentParser, XContentFactory classes) and at the very end, use the SearchSourceBuilder classes to get your SearchQuery by the XContent. I'm curious what is the use case behind this question? Maybe there is another approach we can find?
Yes maybe we can find another approach, because the general overall question was, if i could integrate the Kibana UI searchbar into my application. Therefore i tried in the first step to translate the KQL to java. And then i would try to manage that autocompletion from the ui etc. And yes i could try to reverse engineer the Kibana github project, but i thought that was a bit over the top.
doesn't really answer the question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.