Skip to content

Fluent query language for ElasticSearch built on top of Nest

License

Notifications You must be signed in to change notification settings

delort/fluentnest

 
 

Repository files navigation

FluentNest

LINQ-like query language for ElasticSearch built on top of NEST, aimed at simplifying analytical queries. Compatible with ElastichSearch 1 and 2.

Elastic Search & Nest Version Build Nuget Branch
1.7.* and greater Build 2.0.0 Nuget Package 1.0.0
2.0.* and greater Build 2.0.0 Nuget Package master

NEST for querying ElasticSearch is great, but complex queries are hard to read and reason about. The same can be said about the basic JSON ElasticSearch query language. This library contains set of methods that give you more LINQ-like feeling. Currently mainly aggregations and filters are covered. This page contains few examples, more details are available in the wiki. Motivation and few more implementation details are described in this blog post..

Statistics

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

var priceSum = result.Aggs.GetSum<Car, decimal>(x => x.Price);
var engines = result.Aggs.GetCardinality<Car>(x => x.EngineType);

Since all the queries are always done on the same entity type, one can also convert the result into a typed container:

var container = result.Aggs.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>(search => search.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 Or, And, Range methods, often resulting in huge lambdas. FluentNest can compile small expressions into NEST query language. Examples:

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.Ranking!=null || f.IsAllowed == true);

HasValue or null-checks are compiled into an Exists filter. Boolean values or expressions are compiled into Bool filters. Note that the same expressions can be used for conditional statistics as well as for general filters which affect the whole query. Comparisons of values are compiled into Terms filters.

Grouped statistics

Quite often you might want to calculate a sum per group. With FluentNest you can write:

var result = client.Search<Car>(search => search.Aggregations(agg => agg
	.SumBy<Car>(s => s.Price)
	.GroupBy(s => s.EngineType)
);

Nested groups are very easy as well:

agg => agg
	.SumBy<Car>(s => s.Price)
	.GroupBy(s => s.CarType)
	.GroupBy(s => s.EngineType)

Hitograms

Histogram is another useful aggregation supported by ElasticSearch. Here is a way to get a Sum by month.

var result = client.Search<Car>(s => s.Aggregations(a =>agg.
	.SumBy<Car>(x => x.Price)
	.IntoDateHistogram(date => date.Timestamp, DateInterval.Month)
);
var histogram = result.Aggs.GetDateHistogram<Car>(x => x.Timestamp);

About

Fluent query language for ElasticSearch built on top of Nest

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 99.2%
  • Other 0.8%