简体   繁体   English

与Elasticsearch 5.x的连接需要很长时间。 NEST 5.0 rc

[英]Connection to Elasticsearch 5.x is taking to long. NEST 5.0 rc

I am new in Elasticsearch and I have problems with the connection to the elasticsearch server. 我是Elasticsearch的新手,与Elasticsearch服务器的连接有问题。

I am using Elasticsearch 5.0.1, and I am running my code under .NET 4.5.2. 我正在使用Elasticsearch 5.0.1,并且正在.NET 4.5.2下运行我的代码。 I am using NEST 5.0 rc lib. 我正在使用NEST 5.0 rc lib。

I also installed Kibana and x-pack in my pc. 我还在我的电脑上安装了Kibana和x-pack。

My code to connect to elasticsearch: 我要连接到elasticsearch的代码:

var nodes = new Uri[] { new Uri("http://localhost:9200") };
var pool = new StaticConnectionPool(nodes);
var settings = new ConnectionSettings(pool).DefaultIndex("visitor_index");            
var client =  ElasticClient(settings);

My Search code: 我的搜索代码:

var result = client.Search<VisitorTest>(s => s.Index("visitor_index")
    .Query(q => q.Match(mq => mq.Field(f => f.Name).Query("Visitor 1"))));

Basically the problem that I am having is that each time I create a new ElasticClient it take between 40-80 milliseconds to establish the connection. 基本上,我遇到的问题是,每次创建新的ElasticClient时,都要花费40-80毫秒才能建立连接。 I created a UT for this in which I am creating a connection and running the search query twice, and then I am creating a second connection in the same test and run again the search query two times. 为此,我创建了一个UT,在其中创建一个连接并运行两次搜索查询,然后在同一测试中创建第二个连接,然后再次运行搜索查询两次。 The result is that the first query after the connection takes between 40-80 millisecond and the second query with the same connection take 2 milliseconds that is what I expect. 结果是,连接后的第一个查询需要40-80毫秒,而具有相同连接的第二个查询需要2毫秒,这是我期望的。

I tried changing the connection string to use a domain (added the domain to my local host file). 我尝试将连接字符串更改为使用域(将该域添加到了本地主机文件中)。 I also tried removing xpack security so I do not need to authenticate. 我也尝试删除xpack安全性,因此不需要进行身份验证。

xpack.security.enabled: false

But I always get the same result. 但是我总是得到相同的结果。

A few observations 一些观察

  1. A single instance of ConnectionSettings should be reused for the lifetime of the application. 应在应用程序的生命周期内重用ConnectionSettings的单个实例。 ConnectionSettings makes heavy use of caching so should be reused. ConnectionSettings大量使用缓存,因此应重用。
  2. ElasticClient is thread-safe. ElasticClient是线程安全的。 A single instance can be safely used for the lifetime of an application 一个实例可以在应用程序的生命周期内安全使用
  3. Unless you have a collection of nodes, I would recommend using SingleNodeConnectionPool instead of StaticConnectionPool . 除非您有一组节点,否则我建议您使用SingleNodeConnectionPool而不是StaticConnectionPool The latter has logic to round-robin over nodes which is unneeded for a single node. 后者具有在单个节点上不需要的节点上循环的逻辑。
  4. The client takes advantage of connection pooling within the .NET framework; 客户端利用.NET框架中的连接池; you can adjust KeepAlive behaviour on ConnectionSettings with EnableTcpKeepAlive() 您可以使用EnableTcpKeepAlive()调整ConnectionSettings上的KeepAlive行为
  5. If you have a web proxy configured on your machine, you could have a look at disabling automatic proxy detection with .DisableAutomaticProxyDetection() on ConnectionSettings . 如果在计算机上配置了Web代理,则可以使用ConnectionSettings上的.DisableAutomaticProxyDetection()禁用自动代理检测。

I'll add my few coins here. 我将在这里添加一些硬币。

Had exactly same issue with 40 ms requests. 40毫秒请求有完全相同的问题。 However from Kibana dev tools it was taking 1 ms. 但是,从Kibana开发人员工具那里花费了1毫秒。

Fixed by tweaking two things: 通过调整两点来解决:

Ninject part: 注入部分:

kernel.Bind<IEsClientProvider>().To<EsClientProvider>().InSingletonScope().WithConstructorArgument("indexName", "items");

And in client provider: 并在客户提供者中:

public ElasticClient GetClient()
    {
        if (this.client == null)
        {
            settings = new ConnectionSettings(nodeUri).DefaultIndex(indexName);
            this.client = new ElasticClient(settings);
        }

        return client;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM