Monday, 23 November 2015

Elasticsearch: Java: Search API

Search API is used to execute a search query and get back search hits that match the query. You can execute search query across more than one index and types. You can built a query using Query API (or) filter API. Later post explains how to use Query and filter apis. In this post I am going to explain how to get all the documents
         a. from a cluster
         b. from given index
         c. from given indexes
         d. from given indexes and types etc.,

Return all documents from a cluster
public static SearchResponse getAllDocuemnts(Client client) {
         Preconditions.checkNotNull(client, "client shouldn't be null");
         return client.prepareSearch().execute().actionGet();
}

Return all documents from given indices
public static SearchResponse getAllDocuments(Client client,
                           String... indices) {
         Preconditions.checkNotNull(client, "client shouldn't be null");
         Preconditions.checkNotNull(indices, "indeices shouldn't be null");
         return client.prepareSearch(indices).execute().actionGet();
}

Return all documents from given indexes and types
public static SearchResponse getAllDocuments(Client client,String[] indices, String[] types) {
         Preconditions.checkNotNull(client, "client shouldn't be null");
         Preconditions.checkNotNull(indices, "indeices shouldn't be null");
         Preconditions.checkNotNull(types, "types shouldn't be null");
                 
         return client.prepareSearch(indices).setTypes(types).execute().actionGet();

}


Prevoius                                                 Next                                                 Home

No comments:

Post a Comment