搜索使用ElasticSearch NEST C#客户端 [英] Searching ElasticSearch using NEST C# Client

查看:358
本文介绍了搜索使用ElasticSearch NEST C#客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始四处寻找一个搜索引擎,之后一些阅读,我决定用ElasticSearch去(这是相当惊人的:)),我的项目是在C#,所以我环顾四周,一个客户端和使用的 NEST ,一切都非常简单,但我对搜索部分有点糊涂

I started looking around for a search engine and after some reading I decided going with ElasticSearch (which is quite amazing :)), my project is in C# so I looked around for a client and started using NEST, everything is quite straightforward but I am a bit confused on the searching part.

我要搜索所有领域特定类型什么我想出了如下代码:

I want to search all fields on a specific type what I came up with is the following code:

elasticClient.Search<NewType>(s => s.Query(q => q.QueryString(d => d.Query(queryString))));



我看到那么多的查询字符串搜索已经过时,并希望确保上面是正确的这样做的方式(以上未标记为过时......)也实在是有点长了一个简单的任务,所以也许任何人都知道这样做的另一种方式。

I saw that much of the string query search is deprecated and wanted to make sure the above is the correct way of doing this (the above is not marked as deprecated...) also it is a bit long for a simple task so maybe anyone knows another way of doing this.

感谢

推荐答案

我只是用查询字符串的版本:使用C#匿名类型创建我的查询对象,并将其序列化到JSON

I just use the string query version: create my query object using C# anonymous type and serialize it to JSON.

这样的话,我可以从所有的JSON查询的例子在那里有直接的映射关系,无需翻译成这个查询DSL。

That way, I can have straightforward mapping from all the JSON query examples out there, no need translating into this "query DSL".

Elasticsearch本身的发展相当迅速,并且此查询DSL这样势必会缺少一些功能

Elasticsearch by itself evolves quite rapidly, and this query DSL thus is bound to lack some features.

编辑:例如:

var query = "blabla";
var q = new
        {
            query = new
            {
                text = new
                {
                    _all= query
                }
            }, 
            from = (page-1)*pageSize, 
            size=pageSize
        };
        var qJson = JsonConvert.SerializeObject(q);
        var hits = _elasticClient.Search<SearchItem>(qJson);

这篇关于搜索使用ElasticSearch NEST C#客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆