简体   繁体   English

使用 NEST 使用 linq 查询弹性搜索

[英]Querying elastic search with linq using NEST

Is there any way to query Elasticsearch with NEST client using linq or lambda expressions.有什么方法可以使用 linq 或 lambda 表达式通过 NEST 客户端查询 Elasticsearch。

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.我阅读了一些有关ElasticLinq的内容,但它似乎不再活跃。 the last nuget package were published on october 2015最后一个 nuget package 于 2015 年 10 月发布

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调用者不应依赖 ES 或 NEST API

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. 答案越长, ElasticLINQ最接近我所知道的LINQ提供程序,但不公开Elasticsearch API的所有功能。

Whilst there is some overlap between LINQ , IQueryable<T> et. 虽然LINQIQueryable<T>等之间存在一些重叠。 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 eg what would a completion suggester query look like, or a function score query or a moving average aggregation using Holt-Winters ? al,以及Elasticsearch查询DSL和REST API公开的搜索功能,有许多查询无法用LINQ轻松表达,例如完成建议查询的外观,或者函数得分查询使用Holt-移动平均聚合冬天

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. 您需要扩展LINQ中可用的方法并编写一个非平凡的查询提供程序,所有这些都是为了将​​明确定义的查询DSL纳入LINQ范例。

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 . 就个人而言,我倾向于接受查询DSL和REST API,并希望将您的Expression转换为可以使用NEST,Elasticsearch.Net或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 :) 如果您最终编写LINQ查询提供程序,我会非常感兴趣:)

The exact query you write there today works just great in ElasticLINQ. 你今天在那里写的确切查询在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. 今天有一个点发布用于修复使用嵌套在AND中的OR生成查询(不是过滤器)的极端情况。

I am currently working on the LINQ query provider library to generate NEST method calls.我目前正在使用 LINQ 查询提供程序库来生成 NEST 方法调用。 If someone is interested - here is the link to the GitHub repository - https://github.com/VladislavRybnikov/NEST.Linq如果有人感兴趣 - 这是 GitHub 存储库的链接 - https://github.com/VladislavRybnikov/NEST.Linq

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM