Reputation: 11396
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.
Upvotes: 4
Views: 2380
Reputation: 22332
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.
Upvotes: 3
Reputation: 1479
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);
}
Upvotes: 0
Reputation: 690
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
Upvotes: 2