弹性搜索查询第一次慢 [英] elastic search queries slow first time

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

问题描述

在工作中,我建立了一个开发环境,我们正在此环境上测试电子商务网站的弹性搜索.我注意到查询运行得非常快(与sql server相比).但是,第一次执行查询时,要花很多时间才能最终在目录中呈现产品.初始查询后,一切开始很快.

At work I setup a dev environment on which we are testing elastic search for our ecommerce site. I noticed queries run extremely fast (compared to sql server). But, the first time the query is executed it takes quite some time to finally render products in the catalog. After initial query, everything starts working pretty fast.

如果我离开网站并在发生相同的事情后重新进入一段时间.

If I leave the site and re-enter some time after same thing happens.

通过我们的应用程序使用NEST(高级c#客户端)的方式,显然对于第一个查询,它需要执行一些操作来延迟搜索. 反正有防止这种情况发生的方法吗?我可以配置NEST在应用程序启动时执行此操作吗?

By the way our application is using NEST (high level c# client), and apparently for the first query it needs to perform some operations that delay the search. Is there anyway to prevent this? Can I configure NEST to do this operations at application startup?

PD:elasticsearch 5.4

PD: elasticsearch 5.4

更新:

这是我在NinjectModule中初始化ElasticClient和ConnectionSettings的方式:

this is how I'm initializing ElasticClient and ConnectionSettings in my NinjectModule:

public class ElasticRepositoryInjection : NinjectModule
{
  public override void Load()
  {
     var connectionPool = new SingleNodeConnectionPool(new Uri(ConfigurationManager.AppSettings["elastic.server"]));
     var elasticSearchConnectionSettings = new ConnectionSettings(connectionPool)
        .DefaultIndex("isolution")
        .EnableTcpKeepAlive(TimeSpan.FromMilliseconds(2000), TimeSpan.FromMilliseconds(2000))
        .DisableDirectStreaming();

     Bind<ConnectionSettings>().ToConstant(elasticSearchConnectionSettings).InSingletonScope();

     var client = new ElasticClient((Kernel as StandardKernel).Get<ConnectionSettings>());
     Bind<ElasticClient>().ToConstant(client).InSingletonScope();

     client.Search<Domain.Isolution.ElasticSearch.Product>(s => s
        .Query(q => q.MatchAll()));
  }
}

推荐答案

NEST内部使用缓存,例如对于成员访问lambda表达式的字符串,键入JsonContract,等等,这将在第一个请求上建立.

NEST internally uses caches e.g. for member access lambda expressions to strings, type to JsonContract, etc. which will be built on the first request.

NEST内置没有任何东西可以预先执行此启动,这故意是一个懒惰的操作.但是,在应用程序启动时,您可以初始化单身人士IElasticClient 的实例,向Elasticsearch发出搜索请求以准备缓存,然后将客户端实例提供给需要它的那些组件.缓存启动可以通过两种方式进行,但至少这样,至少第一个用户调用不会等待.

There's nothing built into NEST to perform this priming beforehand, it's intentionally a lazy operation. At application startup however, you could initialise a singleton instance of IElasticClient, make a search request to Elasticsearch to prime the caches, then provide the client instance to those components that require it. The priming of caching is performed either way, but this way at least the first user call is not waiting on it.

这篇关于弹性搜索查询第一次慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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