简体   繁体   English

使用NEST C#Client搜索ElasticSearch

[英]Searching ElasticSearch using NEST C# Client

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. 我开始四处寻找搜索引擎,经过一些阅读后我决定选择ElasticSearch(这非常棒:)),我的项目是在C#中所以我四处寻找客户并开始使用NEST ,一切都很简单,但我是在搜索部分有点困惑。

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. 我看到很多字符串查询搜索已被弃用,并希望确保以上是正确的方法(上面没有标记为已弃用...)对于一个简单的任务也有点长,所以也许任何人知道这样做的另一种方式。

Thanks 谢谢

I just use the string query version: create my query object using C# anonymous type and serialize it to JSON. 我只使用字符串查询版本:使用C#匿名类型创建我的查询对象并将其序列化为JSON。

That way, I can have straightforward mapping from all the JSON query examples out there, no need translating into this "query DSL". 这样,我可以直接从所有JSON查询示例中进行映射,无需转换为此“查询DSL”。

Elasticsearch by itself evolves quite rapidly, and this query DSL thus is bound to lack some features. Elasticsearch本身发展得非常迅速,因此这种查询DSL必然缺乏一些功能。

Edit: Example: 编辑:示例:

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

Just to confirm 只是为了确认

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

Is the preferred way to search and the fact it feels a bit long is because there are alot of options you can play with that are not used here. 搜索的首选方式和感觉有点长的事实是因为您可以使用很多可以在这里使用的选项。 I'm always open on suggestions to make it shorter! 我总是愿意建议缩短它!

The string overload is deprecated but wont be removed from NEST. 不推荐使用字符串重载,但不会从NEST中删除。 I'll update the obsolete message to explicitly mention this. 我将更新过时的消息以明确提及此。

If the anonymous types above aren't your thing, you can just use JObjects from json.net and build your query that way. 如果上面的匿名类型不是你的东西,你可以只使用json.net中的JObjects并以这种方式构建你的查询。 Then you can run it the same way above. 然后你可以用上面相同的方式运行它。

JObject query = new JObject();
query["query"] = new JObject();
query["query"]["text"] = new JObject();
query["query"]["text"]["_all"] = searchTerm;
query["from"] = start;
query["size"] = maxResults;
string stringQuery = JsonConvert.SerializeObject(query);
var results = connectedClient.SearchRaw<SearchItem>(stringQuery);

I like this way better because the DSL in ES uses reserved keywords in C#, like bool, so I don't have to do any escaping. 我更喜欢这种方式,因为ES中的DSL使用C#中的保留关键字,比如bool,所以我不必进行任何转义。

With ElasticSearch 2.0, I have to use a SearchResponse< NewType > in the Search method like this : 使用ElasticSearch 2.0,我必须在Search方法中使用SearchResponse <NewType>,如下所示:

var json = JsonConvert.SerializeObject(searchQuery);
var body = new PostData<object>(json);
var res = _elasticClient.Search<SearchResponse<NewType>>(body);
IEnumerable<NewType> result = res.Body.Hits.Select(h => h.Source).ToList();

Hope it help. 希望它有所帮助。

Note: I found very few documentation about PostData class and its generic type parameter 注意:我发现很少有关于PostData类及其泛型类型参数的文档

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

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