Elasticsearch和.NET
我们正在考虑从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(1); Console.WriteLine(stopwatch.ElapsedMilliseconds); }
上面的代码大约需要。 250ms,而与HttpClient
和JsonSerializer
相同的代码需要30-45ms。 只有4个文件,250毫秒是太多的时间。
可以在高流量新闻网站上使用NEST,还是推荐HttpClient
+ JsonSerializer
组合? 搜索页面是2013年我们网站上访问量最大的页面。
提前致谢。
为了让NEST做出第一个请求,必须要做两件事。
-
在这种情况下,Json Serializer(Json.net)必须缓存该类型,以便它知道如何序列化和反序列化您来回发送的对象。
-
Nest有自己流利的语言用于查询,这些查询必须从表示流畅查询语言的初始类型转换,并作为JSON提供给弹性搜索。 这些文档类型也必须由Json Serializer学习。
-
必须启动HTTP客户端才能发出请求。
我目前在单个索引中有超过4M文档,我使用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 { /// /// 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 /// 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; /// /// Initializes a new instance of the 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 /// 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() { } /// /// Gets the instance of our singleton class /// /// The instance. public static ElasticSearch Instance { get { return instance; } } /// /// Log the specified module, id and json. /// /// Here the entity you want to save your log, /// let's use it based on classes and StateMachines /// Identifier. alwayes the next /// Json. public void Log(string type, string id, string json) { client.Index("mta_log", type, id, json); } } }