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

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (19)

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

代码我使用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(Webform)

提问于
用户回答回答于

我已经解决了我的问题。下面的代码是如何从ElasticSearch获取searchResult并使用NEST将数据绑定到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();
}

扫码关注云+社区

领取腾讯云代金券