简体   繁体   English

使用Mpdreamz / NEST遍历Elasticsearch查询结果

[英]looping through elasticsearch queryresult using Mpdreamz/NEST

Iam using Mpdreamz/NEST as .net client for searching elasticsearch. Iam使用Mpdreamz / NEST作为.net客户端来搜索elasticsearch。

I am getting the result count of search result using result.Hits.Total . 我正在使用result.Hits.Total获取搜索结果的结果计数。 But I am wondering how I can display the value of a field from the result. 但是我想知道如何从结果中显示字段的值。

Looking at NEST source code, it seems that you could do something like: 查看NEST源代码,看来您可以执行以下操作:

foreach (var item in result.Documents.ToList())
{
    string msg = "Name: " + item.Name + " | Value: " + item.Text;
    alert(msg);
}

var result = client.Search(....)

Will return aa QueryResult<dynamic> with a Documents property of type IEnumerable<dynamic> You can loop over this like so: 将返回一个QueryResult<dynamic>并具有IEnumerable<dynamic>类型的Documents属性。您可以像这样循环遍历:

foreach(var d in result.Documents)
{
    Console.WriteLine(d.title);
}

The casing matters here d.Title will result in a RuntimeBinderException. 此处的大小写很重要d.Title将导致RuntimeBinderException。 Unless your elasticsearch field actually exactly matches 'Title` 除非您的elasticsearch字段实际上与“标题”完全匹配

You can also map to POCO's 您也可以映射到POCO

public class MyResult
{
    public string Title { get; set; }
}

... ...

var result = client.Search<MyResult>(....)

Now Documents is a IEnumerable<MyResult> which will give you compile time guarantees the property exists. 现在Documents是一个IEnumerable<MyResult> ,它将为您提供编译时保证该属性存在的条件。

You should not have to access .Fields["field"] unless you are dealing with user input. 除非您要处理用户输入,否则不必访问.Fields["field"]

result.Total is also preferred over result.Hits.Total result.Total也优于result.Hits.Total

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

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