Daniel
Daniel

Reputation: 2534

elastic search queries slow first time

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.

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

UPDATE:

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()));
  }
}

Upvotes: 0

Views: 1803

Answers (2)

Russ Cam
Russ Cam

Reputation: 125488

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.

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.

Upvotes: 1

ryanlutgen
ryanlutgen

Reputation: 3041

Elasticsearch uses query caching to cache the results of queries (https://www.elastic.co/guide/en/elasticsearch/reference/current/query-cache.html). The first time your query is run, it is doing a "cold fetch" of sorts and caching the results, which is why your subsequent queries are much faster.

If your first query execution is much slower, depending on how much data you are trying to pull back, consider using scrolling (https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-scroll.html) to boost performance.

Upvotes: 0

Related Questions