连接与NEST弹性搜索库 [英] Connection Pooling with NEST ElasticSearch Library

查看:140
本文介绍了连接与NEST弹性搜索库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用NEST ElasticSearch C#库与ElasticSearch进行交互。我的项目是一个MVC 4 WebAPI项目,基本上构建了一个用于访问目录帮助信息的RESTful Web服务。

I'm currently using the NEST ElasticSearch C# Library for interacting with ElasticSearch. My project is an MVC 4 WebAPI project that basically builds a RESTful webservice for accessing directory assistance information.

我们刚刚开始使用NEST,并且一直在绊倒缺乏文件。什么是有用的,但它有一些非常大的洞。目前,我们所需要的一切工作,但是,我们正在遇到一个连接问题,有时甚至需要一秒钟。我们要做的是使用某种连接池,类似于与SQL Server的交互。

We've only just started working with NEST, and have been stumbling over the lack of documentation. What's there is useful, but it's got some very large holes. Currently, everything we need works, however, we're running into an issue with connections sometimes taking up to a full second. What we'd like to do is use some sort of connection pooling, similar to how you'd interact with SQL Server.

以下是有关如何使用巢: http://mpdreamz.github.com/NEST/concepts/connecting.html

Here is the documentation on how to connect using nest: http://mpdreamz.github.com/NEST/concepts/connecting.html

以下是我们项目的相关代码段:

Here is the relevant code snippet from our project:

public class EOCategoryProvider : IProvider
{
    public DNList ExecuteQuery(Query query)
    {
        //Configure the elastic client and it's settings
        ConnectionSettings elasticSettings = new ConnectionSettings(Config.server, Config.port).SetDefaultIndex(Config.index);
        ElasticClient client = new ElasticClient(elasticSettings);

        //Connect to Elastic
        ConnectionStatus connectionStatus;
        if (client.TryConnect(out connectionStatus))
        {
            // Elastic Search Code here ...
        } // end if
    } // end ExecuteQuery
} // end EOCategoryProvider

从查看文档,我看不到任何连接池的规定。我一直在考虑实现自己的(有,说3或4个ElasticClient对象存储,并选择它们的循环风格),但我想知道是否有人有更好的解决方案。如果没有,有没有人建议最好的手段实现连接池?任何文章指向?

From looking at the documentation, I can't see any provisions for a connection pool. I've been thinking about implementing my own (having, say 3 or 4 ElasticClient objects stored, and selecting them round-robin style), but I was wondering if anyone had a better solution. If not, does anyone have advice on the best way to implement a connection pool by hand? Any articles to point to?

感谢你们想出来的任何东西。

Thanks for anything you guys come up with.

更新:这似乎与每个请求调用TryConnect以及特定的网络设置有关。在与弹性盒子相同的网络上使用机器时,问题完全消失;我的开发机器(平均值为弹性框的350ms)似乎无法使http连接有时,这导致了TryConnect的长时间。

Update: This seems to have been related to calling TryConnect on every request, and the particular network setup. The problem completely disappeared when using a machine on the same network as the Elastic box; My development machine (which averages 350ms to the Elastic box) seemed to fail to make http connections sometimes, which caused the long times in TryConnect.

推荐答案

每次打电话到Elasticsearch时,您都不需要调用 TryConnect()。当您的应用程序启动时,它基本上是一个理智的检查呼叫。

You don't have to call TryConnect() each time you do a call to Elasticsearch. It's basically a sanity check call for when your application starts.

NEST是Elasticsearch的C#REST客户端,默认的 IConnection 使用已经存储TCP连接的 WebRequest.Create

NEST is the C# REST client for Elasticsearch and the default IConnection uses WebRequest.Create which already pools TCP connections.

查看实际的实现: https://github.com/elastic/elasticsearch-net/blob/master/src/Elasticsearch.Net/Connection/HttpConnection.cs

重用 ElasticClient 不会提供任何性能提升,因为每个调用已经拥有自己的 HttpWebRequest 。整个客户端是无意义的。

Reusing ElasticClient won't offer any performance gains since each call already gets its own HttpWebRequest. The whole client is built stateless on purpose.

然而,我非常感兴趣的是为什么电话需要1秒钟的时间。您可以发布实际的NEST代码,您是如何衡量通话和描述您的数据。

I am however very interested in why calls are taking 1 second for you. Could you post the actual NEST code, how you are are measuring the calls and describe your data.

免责声明:我是NEST的作者。

Disclaimer: I'm the author of NEST.

这篇关于连接与NEST弹性搜索库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆