c# - ASP.Net弹性搜寻

标签 c# .net visual-studio elasticsearch nest

您好,所以我尝试使用asp.net和NEST库编写一个简单的Web View ,该 View 将使用我的ElasticSearch数据库,并在单击按钮时将其显示在textview中。

这是我单击按钮时输入的代码,
您能看看吗,告诉我我走的路是好还是不好?

using Elasticsearch.Net;
using Nest;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ElasticsearchWeb
{
    public class shekspir
    {
        public string type { get; set; }
        public int line_id { get; set; }
        public string play_name { get; set; }
        public int speech_number { get; set; }
        public float line_number { get; set; }
        public string speaker { get; set; }
        public string text_entry { get; set; }
    }

    public partial class Default : System.Web.UI.Page
    {
        public static Uri GetElasticHost()
        {
            var host = "http://localhost:9200";
            return new Uri(host);
        }

        public static ElasticClient GetElasticClient(ConnectionSettings settings = null)
        {
            if (settings == null)
            {
                var node = GetElasticHost();
                var pool = new SingleNodeConnectionPool(node);
                settings = new ConnectionSettings(pool);
            }
            settings.DisableDirectStreaming(true);
            var client = new ElasticClient(settings);
            return client;
        }

        public static List<shekspir> GetAllShekspir(int ID)
        {
            var workОfShakespeare = GetElasticClient();

            ISearchResponse<shekspir> result = null;

            result = workОfShakespeare.Search<shekspir>(x => x
            .Index("shekspir")
            .Query(q => q
                .MatchAll())
                .Size(100)
            );

            List<shekspir> list = new List<shekspir>();
            foreach (var r in result.Hits)
            {
                shekspir a = r.Source;
                list.Add(a);
            }

            return list;
        }
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            List<shekspir> list = GetAllShekspir (1);

            foreach (shekspir u in list)
            {
                litInfo.Text += u.play_name + ": " + u.text_entry + "<br>";
            }
        }
    }
}

最佳答案

List<shekspir> list = new List<shekspir>();
foreach (var r in result.Hits)
{
    shekspir a = r.Source;
    list.Add(a);
}

如果您只想将上面返回的文档替换为
var list= result.Documents

关于c# - ASP.Net弹性搜寻,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61817383/

相关文章:

c# - 派生类的打印没有被调用,为什么?

c# - ComboBox onChange 后 TextBox 不显示值(WPF、C#、Binding)

c# - 我什么时候应该使用中性文化?它有什么必要?为什么不强制使用特定文化?

.net - 如何记录异常情况?

c++ - boost C++ : how to return file from folder via tcp?

c# - 在 C# 中更改标签的背景颜色

c# - 如何使用 Filehelpers 从 Azure 文件存储读取流

c# - 比较两个阶乘而不计算

visual-studio - 如何在 Visual Studio 选项中禁用设计模式

c++ - Visual Studio 2017 的 CMake 多项目设置