Elasticsearch and .NET

本文关键字:NET and Elasticsearch | 更新日期: 2023-09-27 18:06:24

我们正在考虑从Solr/Solr.net切换到Elasticsearch。我们从NEST开始。我们在搜索索引中只有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);
}

上面的代码占用了大约。250ms,而与HttpClientJsonSerializer相同的代码需要30-45ms。对于4个文档来说,250毫秒的时间太长了。

NEST可以用在高流量的新闻网站上吗,还是推荐HttpClient + JsonSerializer组合?这个搜索页面是我们网站2013年访问量最大的页面。

Elasticsearch and .NET

为了使NEST发出第一个请求,必须发生两件事。

  1. Json序列化器(Json.net)在这种情况下必须缓存类型,以便它知道如何序列化和反序列化你来回发送的对象

  2. Nest有自己的流畅查询语言,这些查询必须从表示流畅查询语言的初始类型转换为JSON,并以JSON形式交付给弹性搜索。Json序列化器也必须学习这些文档类型

  3. HTTP客户端必须启动才能发出请求。

我目前在一个索引中有超过400万个文档,我使用NEST,我的搜索从服务器一直到客户端在互联网上使用NEST花费50-70毫秒。但是,和您一样,在冷启动之后,第一个请求也很慢。

我建议你使用https://github.com/ServiceStack/ServiceStack.Text,c#中最快的Json序列化器

对于驱动程序,使用低级驱动程序http://nest.azurewebsites.net/elasticsearch-net/quick-start.html

我开始写的代码下面的

详细记录我的应用程序并分析它们。还得工作,但可以是一个好的开始。

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);
        }
    }
}