How to find Java API equivalents for Elasticearch curl + json examples

I use the Java API to interact with Elasticsearch, but generally most of the documentation and examples use raw curl + javascript / json, leaving me wondering how to translate this into the Java API equivalent.

So I'm wondering if the Elasticearch Java Client API can somehow drop the "native" json when needed. Obviously, I could create my own HttpClient and do a curl call in Java, but before doing that, I wondered if there is something more elegant built into Elasticsearch?

UPDATE: I am finally tired of Elasticsearch for this reason and many others. I switched to Solr and was very pleased - delivered an awesome out-of-the-box app on time that was solid and super-performing! Solr has a great java client, great docs, super performance and versatile features, and a completely free GUI monitoring / troubleshooting tool. For now, I will stick with Solr.

+3


source to share


3 answers


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

Find the source(String)

method
in the appropriate one ActionRequest

(or ActionRequestBuilder

which will usually call it setSource

).

For example, if you want to send a JSON request related to SearchRequest

, you can 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 this is certainly handy, it is probably best used for debugging rather than production code.

+3


source


In elasticsearch java api doc they provide a minimal doc. Pass test cases on gystub elasticsearch account. They covered all elasticsearch functionality as test cases.

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



It would be helpful

+1


source


You should take a look at Spring Data Elasticsearch: https://github.com/spring-projects/spring-data-elasticsearch There you can use ElasticsearchRepository and hence create your own repository and use @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);
}

      

0


source







All Articles