Giter Site home page Giter Site logo

elasticsearch-client's Introduction

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");        
}

elasticsearch-client's People

Contributors

hengoldburd avatar itaiag avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.