Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Querying elastic search with linq using NEST

Is there any way to query Elasticsearch with NEST client using linq or lambda expressions.

I Want to do somthing like this :

client.Search<MyClass>(s => s.MyProperty.Contains("value") &&
                            (s.MySecondProperty == 123 ||
                             s.ThirdProperty > 12)) ;

Or

var query = from m in MyContext.MyClass
            where ...
            select m

I read a little about ElasticLinq but it seems that it is no more active. the last nuget package were published on october 2015

What i want to do is to create a method that get an Expression as parameter from the caller and search on elastic with it. the caller should not depend on ES or NEST API

like image 473
Oussama Avatar asked Mar 14 '16 17:03

Oussama


2 Answers

In short, no.

The longer answer is, ElasticLINQ is the closest to a LINQ provider that I'm aware of but does not expose all of the capabilities of the Elasticsearch API.

Whilst there is some overlap between LINQ, IQueryable<T> et. al, and the search capabilities exposed by the Elasticsearch query DSL and REST API, there are many queries that cannot be easily expressed with LINQ e.g. what would a completion suggester query look like, or a function score query or a moving average aggregation using Holt-Winters?

You would need to extend the methods available in LINQ and write a non-trivial query provider, all for the desire to fit a well defined query DSL into the LINQ paradigm.

Personally, I would be inclined to embrace the query DSL and REST API and look to transform your Expression into something that you can send with NEST, Elasticsearch.Net or HttpClient. The caller still doesn't need to know how the request is made.

If you do end up writing a LINQ query provider, I'd be very interested :)

like image 60
Russ Cam Avatar answered Oct 28 '22 14:10

Russ Cam


The exact query you write there today works just great in ElasticLINQ.

The project is still alive - just sometimes there is no reason for a new release. There was a point release today to fix a corner case in generating Queries (not filters) using OR's nested within an AND.

like image 33
DamienG Avatar answered Oct 28 '22 15:10

DamienG