C# Elasticsearch和.NET
我们正在考虑从Solr/Solr.net切换到Elasticsearch。我们从一开始。我们在搜索索引中只有4个文档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");
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提出第一个请求,必须做两件事
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几乎不会增加原始弹性响应时间的开销