Code Monkey home page Code Monkey logo

fluentnest's Introduction

FluentNest

Gitter chat

Fluent way to query ElasticSearch from C# based on NEST.

Elastic Search & Nest Version Build Nuget Branch
6.x Build 6.x Nuget Package master
5.x Build 5.x Nuget Package 5.x
2.x Build 2.x Nuget Package 2.x
1.7.x Build 1.7.x Nuget Package 1.x

Complex queries in ElasticSearch JSON query language are barely readable. NEST takes some part of the pain away, but nested lambdas are still painful. FluenNest tries to simplify the query composition. Details are available in the wiki. Motivation and few implementation details are described in this blog post.

Statistics

var result = client.Search<Car>(sc => sc.Aggregations(agg => agg
    .SumBy(x => x.Price)
    .CardinalityBy(x => x.EngineType)
);

var container = result.Aggregations.AsContainer<Car>();
var priceSum = container.GetSum(x => x.Price);
var engines = container.GetCardinality(x => x.EngineType);

Conditional statistics

Conditional sums can be quite complicated with NEST. One has to define a Filter aggregation with nested inner Sum or other aggregation. Here is quicker way with FluentNest:

var result = client.Search<Car>(sc => sc.Aggregations(aggs => aggs
    .SumBy(x=>x.Price, c => c.EngineType == EngineType.Diesel)
    .SumBy(x=>x.Sales, c => c.CarType == "Car1"))
);

Filtering and expressions to queries compilation

Filtering on multiple conditions might be complicated since you have to compose filters using Bool together with Must or Should methods, often resulting in huge lambdas. FluentNest can compile small expressions into NEST query language:

client.Search<Car>(s => s.FilterOn(f => f.Timestamp > startDate && f.Timestamp < endDate));
client.Search<Car>(s => s.FilterOn(f=> f.Ranking.HasValue || f.IsAllowed);
client.Search<Car>(s => s.FilterOn(f=> f.Age > 10 || f.Age < 20 && f.Name == "Peter");

Groups & Histograms

Quite often you might want to calculate an aggregation per group or per histogram bucket:

var result = client.Search<Car>(sc => sc.Aggregations(agg => agg
    .SumBy(s => s.Price)
    .GroupBy(s => s.EngineType)
);
var result = client.Search<Car>(s => s.Aggregations(agg => agg
    .SumBy(x => x.Price, x => x.EngineType == EngineType.Diesel)
    .IntoDateHistogram(date => date.Timestamp, DateInterval.Month)
);

Groups and histograms can be also nested:

var result = client.Search<Car>(sc => sc.Aggregations(agg => agg
    .SumBy(s => s.Price)
    .GroupBy(s => s.CarType)
    .IntoDateHistogram(date => date.Timestamp, DateInterval.Month));

fluentnest's People

Contributors

filpen avatar hoonzis avatar vbfox avatar msarilar avatar

Watchers

James Cloos 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.