Elasticsearch 和 .NET
-
20-12-2019 - |
题
我们正在考虑从 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);
}
上面的代码大约需要。250ms,而相同的代码 HttpClient
和 JsonSerializer
需要 30-45 毫秒。250 毫秒对于 4 个文档来说太长了。
NEST可以用在高流量的新闻网站上吗,或者你推荐吗 HttpClient
+ JsonSerializer
组合?搜索页面是 2013 年我们网站上访问量最大的页面。
提前致谢。
解决方案
为了让 NEST 发出第一个请求,必须发生两件事。
在这种情况下,Json 序列化器 (Json.net) 必须缓存类型,以便它知道如何序列化和反序列化您来回发送的对象。
Nest 有自己的流畅查询语言,必须从表示流畅查询语言的初始类型进行翻译,并以 JSON 形式传递给弹性搜索。Json 序列化程序还必须了解这些文档类型。
HTTP 客户端必须启动才能发出请求。
目前,我在与 NEST 一起使用的单个索引中有超过 400 万个文档,并且使用 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);
}
}
}
不隶属于 StackOverflow