Querying elastic search with linq using NEST
Asked Answered
L

3

8

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

Langland answered 14/3, 2016 at 17:2 Comment(0)
P
4

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 :)

Panayiotis answered 15/3, 2016 at 3:45 Comment(0)
M
4

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.

Maryrose answered 23/3, 2016 at 7:7 Comment(2)
any ETA for supporting v5.x?Cassatt
My job no longer involves .NET or Elasticsearch so I'm not sure there will be any movement without help.Maryrose
M
0

I am currently working on the LINQ query provider library to generate NEST method calls. If someone is interested - here is the link to the GitHub repository - https://github.com/VladislavRybnikov/NEST.Linq

Maxilliped answered 9/2, 2022 at 8:49 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Pirouette

© 2022 - 2024 — McMap. All rights reserved.