Skip to content

Latest commit

 

History

History
218 lines (161 loc) · 3.95 KB

README.md

File metadata and controls

218 lines (161 loc) · 3.95 KB

Elasticsearch Java Client

A simple Elasticseach client based on the 5.x HTTP API. This is by no means a full blown client and it support only a small sub set of the Elasticseach operations. Furethemore, when the Elasticteam will release a full Java client, this client will probably become redundent. Nevertheless, the client is very simple for use, supports the basic operations and is designed to easily be extended.

The supported operations are:

  • Index creation and deletion
  • Document creation and deletion
  • Search by term and by query
  • Aggregations with query

Getting started

prerequisites

  • Java 8

Maven

    <repositories>
        <repository>
            <id>topq</id>
            <url>http://maven.top-q.co.il/content/groups/public</url>
        </repository>
    </repositories>
    .
    .
    .
    <dependencies>
    .
    .
        <dependency>
            <groupId>il.co.topq.elastic</groupId>
            <artifactId>elasticseach-client</artifactId>
            <version>1.0.00</version>
        </dependency>

    .
    .

    </dependencies>

Usage Examples

Index Operations

Create and delete

String settings = "{\"settings\": { \"index\": { \"number_of_shards\": 3, \"number_of_replicas\": 1  }}}";
String index = "reddit";

try (ESClient client = new ESClient("localhost", 9200)) {
    client
       .index(index)
       .create(settings);

    client
       .index(index)
       .delete();
}       

Document Operations

Add

String index = "reddit";
String doc = "post";

Post post0 = new Post();
post0.setId(1212);
post0.setOp("Itai");
post0.setPoints(100);
post0.setSubreddit("all");

try (ESClient client = new ESClient("localhost", 9200)) {
    client
        .index(index)
        .document(doc)
        .add()
        .single("100", post0);
}

Delete

String index = "reddit";
String doc = "post";

try (ESClient client = new ESClient("localhost", 9200)) {
    Map<String, Object> response = client
        .index(index)
        .document(doc)
        .delete()
        .single("100");
    
    Assert.assertEquals("deleted", response.get("result").toString());
}

Search Operations

Search by term

String index = "reddit";
String doc = "post";

try (ESClient client = new ESClient("localhost", 9200)) {
    List<Post> posts = client
        .index(index)
        .document(doc)
        .search()
        .byTerm("id", "1212")
        .asClass(Post.class);
}

Search by string query

String index = "reddit";
String doc = "post";

try (ESClient client = new ESClient("localhost", 9200)) {
    List<Post> posts = client
        .index(index)
        .document(doc)
        .search()
        .byQuery("id:1212")
        .asClass(Post.class);
}

Search by range

String index = "reddit";
String doc = "post";
Map<String, Object> rangeParams = new HashMap<String, Object>();
rangeParams.put("gte", "now-30d");

try (ESClient client = new ESClient("localhost", 9200)) {
    List<Post> posts = client
        .index(index)
        .document(doc)
        .search()
        .byRange("timeStamp",rangeParams)
        .asClass(Post.class);
}

Aggregation operations

Min aggregation

String index = "reddit";
String doc = "post";

try (ESClient client = new ESClient("localhost", 9200)) {
    Double min = client
        .index(index)
        .document(doc)
        .aggs()
        .min("id");        
}

Max aggregation

String index = "reddit";
String doc = "post";

try (ESClient client = new ESClient("localhost", 9200)) {
    Double max = client
        .index(index)
        .document(doc)
        .aggs()
        .max("id");        
}

Max aggregation with string query

String index = "reddit";
String doc = "post";

try (ESClient client = new ESClient("localhost", 9200)) {
    Double max = client
        .index(index)
        .document(doc)
        .aggs()
        .max("id","foo:bar");        
}