4

I am new to Elasticsearch. I read Elasticsearch's Java client APIs and am able to build query and send it to the Elasticsearch server via the transport client.

Because my query is quite complex with multi-level filters and I notice that it is cumbersome to build a query via the Java client. I feel that it is much simpler to build a JSON query string and then send it over to the Elasticsearch server via a Java client.

Is this something Elasticsearch offers?

I do like what Elasticsearch Java API can do after receiving results such as scrolling over the results. I want to keep these features.

Thanks for any input and links!

Regards.

4 Answers 4

9

Did further research on Elasticsearch API and found out that Elasticsearch does offer this capability. Here is how:

SearchResponse scrollResp = client.prepareSearch("my-index")
        .setTypes("my-type")
        .setSearchType(SearchType.SCAN)
        .setQuery(query) // **<-- Query string in JSON format**
        .execute().actionGet();
Sign up to request clarification or add additional context in comments.

2 Comments

I just want to thanks for "<-- Query string in JSON format" comment :)
SearchType.SCAN is no longer part of the API (at least not in 5.1.2, which is what I'm currently using)
3

You can no longer pass in string to the .setQuery function, however you can use a WrapperQueryBuilder like this:

WrapperQueryBuilder builder = QueryBuilders.wrapperQuery(searchQuery);
SearchRequestBuilder sr = client.prepareSearch().setIndices(index).setTypes(mapping).setQuery(builder);

Comments

1

I'd recommend using the Java API, it is very good once you get used to it and in most cases it is less cumbersome. If you look through the Elasticsearch source code you will see that the Java API Builds the JSON under the hood. Here is an example from the MatchAllQueryBuilder:

@Override
public void doXContent(XContentBuilder builder, Params params) throws IOException {
    builder.startObject(MatchAllQueryParser.NAME);
    if (boost != -1) {
        builder.field("boost", boost);
    }
    if (normsField != null) {
        builder.field("norms_field", normsField);
    }
    builder.endObject();
}

1 Comment

This doesn't answer the question.
1

ElasticSearch has built in capabilities to do exactly what you need, in an organized manner.

To answer your question, please see this link (the material is gone on elastic's site, so it might no longer work):

https://web.archive.org/web/20150906215934/https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/search.html

All you have to do is build a simple file which contains your search template i.e complex search query. It can be a simple json file, or a text file.

Now you simply pass in your parameters, through your java code. See the example in the link, it makes things amply clear.

Bhargav.

Comments

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.