Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Elasticsearch和.NET_C#_.net_Asp.net Mvc_<img Src="//i.stack.imgur.com/RUiNP.png" Height="16" Width="18" Alt="" Class="sponsor Tag Img">elasticsearch_Nest - Fatal编程技术网 elasticsearch,nest,C#,.net,Asp.net Mvc,elasticsearch,Nest" /> elasticsearch,nest,C#,.net,Asp.net Mvc,elasticsearch,Nest" />

C# Elasticsearch和.NET

C# Elasticsearch和.NET,c#,.net,asp.net-mvc,elasticsearch,nest,C#,.net,Asp.net Mvc,elasticsearch,Nest,我们正在考虑从Solr/Solr.net切换到Elasticsearch。我们从一开始。我们在搜索索引中只有4个文档 private static void Main(string[] args) { var node = new Uri("http://localhost:9200"); var settings = new ConnectionSettings( node, "my-application");

我们正在考虑从Solr/Solr.net切换到Elasticsearch。我们从一开始。我们在搜索索引中只有4个文档

private static void Main(string[] args)
{
    var node = new Uri("http://localhost:9200");
    var settings = new ConnectionSettings(
        node, "my-application");

    var client = new ElasticClient(settings);

    var stopwatch = Stopwatch.StartNew();
    var sr = client.Get<Movie>(1);

    Console.WriteLine(stopwatch.ElapsedMilliseconds);
}
private static void Main(字符串[]args)
{
var node=新Uri(“http://localhost:9200");
var设置=新连接设置(
节点,“我的应用程序”);
var客户端=新的ElasticClient(设置);
var stopwatch=stopwatch.StartNew();
var sr=client.Get(1);
控制台写入线(秒表延时百万秒);
}
上面的代码大约需要250毫秒,而与
HttpClient
JsonSerializer
相同的代码需要30-45毫秒。250毫秒对于4个文档来说太长了

NEST可以在高流量新闻网站上使用,或者您推荐
HttpClient
+
JsonSerializer
combo?该搜索页面是2013年我们网站上访问量最大的页面


提前感谢。

为了让NEST提出第一个请求,必须做两件事

  • 在本例中,Json序列化程序(Json.net)必须缓存该类型,以便它知道如何序列化和反序列化来回发送的对象

  • Nest有自己的fluent语言用于查询,这些查询必须从表示fluent查询语言的初始类型转换而来,并以JSON的形式传递给弹性搜索。Json序列化程序还必须学习这些文档类型

  • 必须旋转HTTP客户端才能发出请求

  • 我目前在一个索引中有超过400万个文档,我使用NEST进行搜索,从服务器到客户端,通过internet进行搜索,使用NEST需要50-70毫秒。但是,与您一样,冷启动后,第一个请求的速度很慢。

    我建议您使用, C#最快的Json序列化程序

    对于驾驶员,使用低级别

    下面是我开始编写的详细记录我的应用程序并对其进行分析的代码。仍然需要工作,但可以是一个良好的开端

    using System;
    using System.Configuration;
    using Elasticsearch.Net;
    using Elasticsearch;
    using Elasticsearch.Net.Connection;
    using Elasticsearch.Net.ConnectionPool;
    
    namespace Common {
    
        /// <summary>
        /// Elastic search. Singletone, open connection and thread safe to be open for all the time
        /// the app is running, so we send ours logs to ealsticsearch to be analyzed, assychronly
        /// See the fourth version;
        /// http://csharpindepth.com/articles/general/singleton.aspx
        /// </summary>
        public sealed class ElasticSearch {
            // our instance of ourself as a singleton
            private static readonly ElasticSearch instance = new ElasticSearch();
    
            ElasticsearchClient client;
            string connectionString = ConfigurationManager.ConnectionStrings["Elasticsearch"].ConnectionString;
    
            /// <summary>
            /// Initializes a new instance of the <see cref="Common.ElasticSearch"/> class.
            /// Follow this: http://nest.azurewebsites.net/elasticsearch-net/connecting.html
            /// We use a ConnectionPool to make the connection fail-over, that means, if the 
            /// connection breaks, it reconnects automatically
            /// </summary>
            private ElasticSearch() {
                var node = new Uri(connectionString);
                var connectionPool = new SniffingConnectionPool(new[] { node });
                var config = new ConnectionConfiguration(connectionPool);
                client = new ElasticsearchClient(config);   // exposed in this class
            }
    
            static ElasticSearch() {
            }
    
            /// <summary>
            /// Gets the instance of our singleton class
            /// </summary>
            /// <value>The instance.</value>
            public static ElasticSearch Instance {
                get {
                    return instance;
                }
            }
    
            /// <summary>
            /// Log the specified module, id and json.
            /// </summary>
            /// <param name="type">Here the entity you want to save your log,
            /// let's use it based on classes and StateMachines</param>
            /// <param name="id">Identifier. alwayes the next</param>
            /// <param name="json">Json.</param>
            public void Log(string type, string id, string json) {
                client.Index("mta_log", type, id, json);
            }
    
        }
    }
    
    使用系统;
    使用系统配置;
    使用Elasticsearch.Net;
    使用弹性搜索;
    使用Elasticsearch.Net.Connection;
    使用Elasticsearch.Net.ConnectionPool;
    名称空间公用{
    /// 
    ///弹性搜索。单音,开放式连接和线程安全,可随时打开
    ///应用程序正在运行,因此我们会将日志发送到ealsticsearch进行分析
    ///见第四版;
    /// http://csharpindepth.com/articles/general/singleton.aspx
    /// 
    公共密封类弹性搜索{
    //我们作为独生子女的例子
    私有静态只读ElasticSearch实例=新ElasticSearch();
    弹性搜索客户端;
    string connectionString=ConfigurationManager.connectionString[“Elasticsearch”]。connectionString;
    /// 
    ///初始化类的新实例。
    ///遵循以下步骤:http://nest.azurewebsites.net/elasticsearch-net/connecting.html
    ///我们使用ConnectionPool进行连接故障转移,也就是说,如果
    ///连接断开,它会自动重新连接
    /// 
    私人ElasticSearch(){
    var节点=新Uri(connectionString);
    var connectionPool=new SniffingConnectionPool(new[]{node});
    var config=新连接配置(connectionPool);
    client=new ElasticsearchClient(config);//在此类中公开
    }
    静态弹性搜索(){
    }
    /// 
    ///获取singleton类的实例
    /// 
    ///实例。
    公共静态ElasticSearch实例{
    得到{
    返回实例;
    }
    }
    /// 
    ///记录指定的模块、id和json。
    /// 
    ///这里是要保存日志的实体,
    ///让我们基于类和状态机来使用它
    ///标识符。总是下一个
    ///Json。
    公共无效日志(字符串类型、字符串id、字符串json){
    Index(“mta_日志”,类型,id,json);
    }
    }
    }
    
    尝试执行多个请求,我认为NEST必须在第一个请求之后进行某种缓存,以显著提高性能,否则没有人会使用它。你是对的。首先是
    client.Get
    client.Search
    请求速度较慢,但所有后续请求都要快得多。这是事实,NEST只需要缓存强类型属性访问,即
    p=>p.Name
    和类型/索引推断。最大的延迟因素是JSON.NET的序列化缓存需要预热。只需添加一个数据点,我就在多个服务器上的单个分片中拥有超过1亿个数据点,NEST几乎不会增加原始弹性响应时间的开销