在NEST(ElasticSearch)中使用searchResponse中的storedFields时出错

时间:2019-05-12 12:34:38

标签: c# asp.net elasticsearch nest

我需要将NEST(ElasticSearch)的搜索结果绑定到ASP.NET(Webform)中的Gridview。

代码,我使用NEST从ElasticSearch中得到结果:

public class Address
{
    public int SN { get; set; }
    public string JLN { get; set; }
}

protected void BtnSearch_Clicked(object sender, EventArgs e)
{
    string SearchValue = txtSearchValue.Text;
    string es_host = System.Configuration.ConfigurationManager.AppSettings["cnStringIP"];
    string es_port = System.Configuration.ConfigurationManager.AppSettings["cnStringPort"];
    string es_index = System.Configuration.ConfigurationManager.AppSettings["cnStringIndex"];

    var settings = new ConnectionSettings(new Uri("http://" + es_host + ":" + es_port + ""))
        .DefaultIndex("masterlist*");

    var client = new ElasticClient(settings);

    var searchResponse = client.Search<Address>(s => s
            .Index("masterlist*")
            .From(0)
            .Size(10)
            .Query(q => q
                 .QueryString(qs => qs
                    .Query("JLN:\""+ SearchValue +"\"")
                )
            )
        );

    var address = searchResponse.Documents.ToList();

    ESGridview.DataSource = address;
    ESGridview.DataBind();
}

使用此代码,gridview可以自动生成正确标题的两个字段“ SN”和“ JLN”,并且它可以自动生成10行(我在搜索语法中将大小限制为最大10行),但是列中的数据为空。

我确实找到了另一个带有此链接的POST

https://www.elastic.co/guide/en/elasticsearch/client/net-api/6.x/returned-fields.html#returned-fields

使用此链接检查后, 我将代码更改为:

string SearchValue = txtSearchValue.Text;
string es_host = System.Configuration.ConfigurationManager.AppSettings["cnStringIP"];
string es_port = System.Configuration.ConfigurationManager.AppSettings["cnStringPort"];
string es_index = System.Configuration.ConfigurationManager.AppSettings["cnStringIndex"];

var settings = new ConnectionSettings(new Uri("http://" + es_host + ":" + es_port + ""))
    .DefaultIndex("masterlist*");

var client = new ElasticClient(settings);

var searchResponse = client.Search<Address>(s => s
    .StoredFields(sf => sf
        .Fields(
            f => f.SN,
            f => f.JLN
            )
         )
        .From(0)
        .Size(10)
        .Query(q => q
             .QueryString(qs => qs
                .Query("JLN:\""+ SearchValue +"\"")
            )
        )
    );

foreach (var fieldValues in searchResponse.Fields)
{
    var document = new
    {
        SN = fieldValues.ValueOf<Address, int>(p => p.SN),
        JLN = fieldValues.Values<Address, string>(p => p.JLN)
    };
}

var address = searchResponse.Documents;
var count = "MaxScore" + searchResponse.MaxScore;

ESGridview.DataSource = address;
ESGridview.DataBind();

但是在整个 foreach(var ...)区域中从头开始运行代码时出现错误:

  

System.NullReferenceException:“对象引用未设置为对象的实例。”

有人能教我如何解决这个问题,或者我做错了什么吗? 非常感谢〜

ElasticSearch 7.0.1 NEST 7.0.0 C# ASP.NET(网络表单)

1 个答案:

答案 0 :(得分:0)

我已经解决了我的问题。 下面的代码是如何通过使用NEST从ElasticSearch获取searchResult并将数据绑定到ASP.NET中的Gridview的方法。

public class Address
{
    [Text(Name = "SN")]
    public string SN { get; set; }

    [Text(Name = "JLN")]
    public string JLN { get; set; }
}

protected void BtnSearch_Clicked(object sender, EventArgs e)
{
    string SearchValue = txtSearchValue.Text;
    string es_host = System.Configuration.ConfigurationManager.AppSettings["cnStringIP"];
    string es_port = System.Configuration.ConfigurationManager.AppSettings["cnStringPort"];
    string es_index = System.Configuration.ConfigurationManager.AppSettings["cnStringIndex"];

    var settings = new ConnectionSettings(new Uri("http://" + es_host + ":" + es_port + ""))
        .DefaultIndex("masterlist*");

    var client = new ElasticClient(settings);

    var searchResponse = client.Search<Address>(s => s
            .From(0)
            .Size(100)
            .Query(q => q
                 .QueryString(qs => qs
                    .Query("JLN:\"" + SearchValue + "\"")
                )
            )
        );

    var address = searchResponse.Documents.ToList();

    ESGridview.DataSource = address;
    ESGridview.DataBind();
}