4

I'm using the Java API to interact with Elasticsearch, but am generally finding most of the documentation and examples use raw curl + javascript/json, leaving me at a loss for how to translate this into the Java API equivalent.

So what I'm wondering is whether the Elasticsearch Java Client API offers some way to drop down to "native" json when needed. Obviously I could create my own HttpClient and execute a curl-style call in Java, but before doing that I wondered if there's already something more elegant built into Elasticsearch?

UPDATE: I finally got fed up with Elasticsearch for this reason and many others. I switched to Solr and have been very happy -- delivered an awesome finished app on time that's been rock solid and super performant in production! Solr has a great java client, great docs, super performance, and versatile features, and a totally free GUI monitoring/troubleshooting tool. I'll be sticking with Solr from now on.

2

3 Answers 3

3

It depends on the Java API in question, but in most cases, the answer is loosely yes:

Look for the source(String) method on the appropriate ActionRequest (or ActionRequestBuilder, which generally will call it setSource).

For example, if you wanted to send the JSON query associated with a SearchRequest, then you could do the following:

SearchRequest request =
    Requests.searchRequest("my-index1", "my-index2")
            .types("my-type1", "my-type2")
            .source("{\"query\":{\"match\":{\"user\":\"xyz\"}}}");

SearchResponse response = client.search(request).actionGet();

While it is certainly convenient, this is probably best used for debugging rather than production code.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for this, I find it absolutely infuriating that the documentation doesn't make it clear there is a way to supply a raw query (including aggregations). If you use the client.prepareSearch() way, it acts like it works, but refuses to return aggregations.
2

In elasticsearch java api document they provide minimal document. Go through the test cases in elasticsearch github account. They covered all functionality in elasticsearch as test cases.

https://github.com/elasticsearch/elasticsearch/tree/master/src/test/java/org/elasticsearch.

It would be useful

Comments

0

You should have a look on Spring Data Elasticsearch: https://github.com/spring-projects/spring-data-elasticsearch There you could make use of ElasticsearchRepository and therefore build your own custom Repository and make use of @Query annotation.

public interface CustomRepository extends ElasticsearchCrudRepository<CustomEntity, Long> {
   @Query("{\"match_all\": {}}")
   List<CustomEntity> findAllPersons();
   // prefix Query
   @Query("{\"bool\" : {\"should\" : [{\"prefix\" : {\"lastName\" : \"?0\"}}, {\"prefix\" : {\"firstName\" : \"?0\"}}]}}")
   List<CustomEntity> findByLastNameOrFirstNameStartsWith(String prefix);
}

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.