Skip to content

ahmetcanik/elasticsearch-helper

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

elasticsearch-helper

elasticsearch-helper makes life easier when using Elasticsearch Java High Level REST Client.

With the help of builder design pattern, ElasticQueryBuilder builds the query with almost all possible options.

General code snippet is

try (ElasticHelperClient helperClient = new ElasticHelperClient()) {
    MatchAllQueryBuilder query = QueryBuilders.matchAllQuery();
    ElasticQueryBuilder builder = new ElasticQueryBuilder(helperClient, query)
        .indices("twitter");
    String json = helperClient.querySingle(builder);
} catch (IOException e) {
    e.printStackTrace();
}

which return one document of the twitter index.

Install

Helper capabilities:

Sorting

Pagination

Aggregation

Scrolling

Highlighting

Masking

Sorting

To sort twitter index by the field created_at and order DESC:

try (ElasticHelperClient helperClient = new ElasticHelperClient()) {
    MatchAllQueryBuilder query = QueryBuilders.matchAllQuery();
    ElasticQueryBuilder builder = new ElasticQueryBuilder(helperClient, query)
            .indices("twitter")
            .sortFieldName("created_at")
            .sortOrder(SortOrder.DESC);
    String json = helperClient.querySingle(builder);
} catch (IOException e) {
    e.printStackTrace();
}

Pagination

To get 10 search hits starting from 20 (records 20,21,..,29):

try (ElasticHelperClient helperClient = new ElasticHelperClient()) {
    MatchAllQueryBuilder query = QueryBuilders.matchAllQuery();
    ElasticQueryBuilder builder = new ElasticQueryBuilder(helperClient, query)
            .indices("twitter")
            .from(20) // .from(previousResult.getFrom() + HITS_PER_PAGE)
            .size(10) // .size(HITS_PER_PAGE);
    SearchResult result = helperClient.queryMultiple(builder);
} catch (IOException e) {
    e.printStackTrace();
}

SearchResult class holds from and size, so pagination can be resumed with these values from previous searches.

Aggregation

Aggregation discards query hit results, it just returns aggregation results.

To get max value of created_at field:

try (ElasticHelperClient helperClient = new ElasticHelperClient()) {
    MatchAllQueryBuilder query = QueryBuilders.matchAllQuery();
    AggregationBuilder aggregation = AggregationBuilders.max("max_created_at").field("created_at");
    ElasticQueryBuilder builder = new ElasticQueryBuilder(helperClient, query)
            .indices("twitter")
            .aggregation(aggregation);
    SearchResult result = helperClient.queryMultiple(builder);
} catch (IOException e) {
    e.printStackTrace();
}

Scrolling

Scrolling is used from getting massive amount of documents at once.

Following code tries to get all documents within 10 seconds. For each scroll, it gets 1000 hits.

try (ElasticHelperClient helperClient = new ElasticHelperClient()) {
    MatchAllQueryBuilder query = QueryBuilders.matchAllQuery();
    ElasticQueryBuilder builder = new ElasticQueryBuilder(helperClient, query)
            .indices("twitter")
            .size(1000)
            .scroll("10s");
    SearchResult result = helperClient.queryMultiple(builder);
} catch (IOException e) {
    e.printStackTrace();
}

Highlighting

About

Helper classes to be used with Java High Level REST Client

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages