#Jest
Jest is a Java HTTP Rest client for ElasticSearch.
ElasticSearch is an Open Source (Apache 2), Distributed, RESTful, Search Engine built on top of Apache Lucene.
ElasticSearch already has a Java API which is also used by ElasticSearch internally, but Jest fills a gap, it is the missing client for ElasticSearch Http Rest interface.
Read great introduction to ElasticSearch and Jest from IBM Developer works.
Jest maven repository is hosted on Sonatype.
Add Sonatype repository definition to your root pom.xml
<repositories>
.
.
<repository>
<id>sonatype</id>
<name>Sonatype Groups</name>
<url>https://oss.sonatype.org/content/groups/public/</url>
</repository>
.
.
</repositories>
Add Jest as a dependency to your project.
<dependency>
<groupId>io.searchbox</groupId>
<artifactId>jest</artifactId>
<version>0.0.3</version>
</dependency>
Ensure to check Changelog.
Jest has a sample application can be found here.
To start using Jest first we need a JestClient;
// Configuration
ClientConfig clientConfig = new ClientConfig();
LinkedHashSet<String> servers = new LinkedHashSet<String>();
servers.add("http://localhost:9200");
clientConfig.getProperties().put(ClientConstants.SERVER_LIST, servers);
clientConfig.getProperties().put(ClientConstants.IS_MULTI_THREADED, true);
// Construct a new Jest client according to configuration via factory
JestClientFactory factory = new JestClientFactory();
factory.setClientConfig(clientConfig);
JestClient client = factory.getObject();
JestClient is designed to be signleton, don't construct it for each request!
You can create an index via Jest with ease;
client.execute(new CreateIndex("articles"));
Index setting can be passed as a JSON file or ElasticSearch Settings;
via JSON;
String settings = "\"settings\" : {\n" +
" \"number_of_shards\" : 5,\n" +
" \"number_of_replicas\" : 1\n" +
" }\n";
client.execute(new CreateIndex("articles"), settings)
via SetingsBuilder;
import org.elasticsearch.common.settings.ImmutableSettings;
.
.
ImmutableSettings.Builder settingsBuilder = ImmutableSettings.settingsBuilder();
settings.put("number_of_shards",5);
settings.put("number_of_replicas",1);
client.execute(new CreateIndex("articles"), settingsBuilder.build().getAsMap());
Add ElasticSearch dependency to use Settings api
ElasticSearch requires index data as JSON. There are several ways to create documents to index via Jest. From now on, we will refer documents as source. Source objects can be String, Map or POJOs.
as JSON String;
String source = "{\"user\":\"kimchy\"}";
or creating JSON via ElasticSearch JSONBuilder;
String source = jsonBuilder()
.startObject()
.field("user", "kimchy")
.field("postDate", "date")
.field("message", "trying out Elastic Search")
.endObject().string();
as Map;
Map<String, String> source = new LinkedHashMap<String,String>()
source.put("user", "kimchy");
as POJO;
Article source = new Article();
source.setAuthor("John Ronald Reuel Tolkien");
source.setContent("The Lord of the Rings is an epic high fantasy novel");
An example of indexing given source to twitter index with type tweet;
Index index = new Index.Builder(source).index("twitter").type("tweet").build();
client.execute(index);
Index id can be typed explicitly;
Index index = new Index.Builder(source).index("twitter").type("tweet").id("1").build();
client.execute(index);
@JestId annotation can be used to mark a property of a bean as id;
class Article {
@JestId
private Long documentId;
}
Now whenever an instance of Article is indexed, index id will be value of documentId.
If @JestId value is null, it will be set the value of ElasticSearch generated "_id".
Search queries can be either JSON String or created by ElasticSearch SourceBuilder Jest works with default ElasticSearch queries, it simply keeps things as is.
As JSON;
String query = "{\n" +
" \"query\": {\n" +
" \"filtered\" : {\n" +
" \"query\" : {\n" +
" \"query_string\" : {\n" +
" \"query\" : \"test\"\n" +
" }\n" +
" },\n" +
" \"filter\" : {\n" +
" \"term\" : { \"user\" : \"kimchy\" }\n" +
" }\n" +
" }\n" +
" }\n" +
"}";
Search search = new Search(query);
// multiple index or types can be added.
search.addIndex("twitter");
search.addType("tweet");
JestResult result = client.execute(search);
By using SearchSourceBuilder;
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.matchQuery("user", "kimchy"));
Search search = new Search(searchSourceBuilder.toString());
search.addIndex("twitter");
search.addType("tweet");
JestResult result = client.execute(search);
Add ElasticSearch dependency to use SearchSourceBuilder
Result can be cast to List of domain object;
JestResult result = client.execute(search);
List<Article> articles = result.getSourceAsObjectList(Article.class);
Please refer ElasticSearch Query DSL documentation to work with complex queries.
Get get = new Get.Builder("1").index("twitter").type("tweet").build();
JestResult result = client.execute(get);
Result can be cast to domain object;
Get get = new Get.Builder("1").index("twitter").type("tweet").build();
JestResult result = client.execute(get);
Article article = result.getSourceAsObject(Article.class);
String script = "{\n" +
" \"script\" : \"ctx._source.tags += tag\",\n" +
" \"params\" : {\n" +
" \"tag\" : \"blue\"\n" +
" }\n" +
"}";
client.execute(new Update.Builder(script).index("twitter").type("tweet").id("1").build());
client.execute(new Delete.Builder("1").index("twitter").type("tweet").build());
ElasticSearch's bulk API makes it possible to perform many index/delete operations in a single API call. This can greatly increase the indexing speed.
Bulk bulk = new Bulk("twitter", "tweet");
bulk.addIndex(new Index.Builder(article1).build());
bulk.addIndex(new Index.Builder(article2).build());
bulk.addDelete(new Delete.Builder("1").build());
client.execute(bulk);
List of objects can be indexed via bulk api
Bulk bulk = new Bulk("twitter", "tweet");
Article article1 = new Article("tweet1");
Article article2 = new Article("tweet1");
bulk.addIndexList(Arrays.asList(article1, article2););
client.execute(bulk);
ElasticSearch offers request parameters to set properties like routing, versioning, operation type etc.
For instance you can set "refresh" property to "true" while indexing a document as below;
Index index = new Index.Builder("{\"user\":\"kimchy\"}").index("cvbank").type("candidate").id("1").build();
index.addParameter(Parameters.REFRESH, true);
client.execute(index);
Jest http client support execution of action with non blocking IO asynchronously.
Following example illustrates how to execute action with jest asynchronous call.
client.executeAsync(action,new JestResultHandler<JestResult>() {
@Override
public void completed(JestResult result) {
... do process result ....
}
@Override
public void failed(Exception ex) {
... catch exception ...
}
});
You need to configure the discovery options in the client config as follows:
//enable host discovery
clientConfig.getProperties().put(ClientConstants.DISCOVERY_ENABLED, true); //boolean
clientConfig.getProperties().put(ClientConstants.DISCOVERY_FREQUENCY, 1l); //long
clientConfig.getProperties().put(ClientConstants.DISCOVERY_FREQUENCY_TIMEUNIT, TimeUnit.MINUTES); //timeunit
This will enable new node discovery and update the list of servers in the client periodically.
Integration Tests are best place to see things in action.
Jest is using slf4j for logging and expects you to plug in your own implementation, so log4j dependency is in "provided" scope.
For instance to use log4j implementation, add below dependency to your pom.xml
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.1</version>
</dependency>
Please read slf4j manual here.
If you want to use ElasticSearch's QueryBuilder or Settings classes, ensure to add ElasticSearch dependency.
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
Jest is developed by @dogukansonmez and SearchBox.io team.
Copyright 2012 SearchBox.io
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this work except in compliance with the License. You may obtain a copy of the License in the LICENSE file, or at:
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.