Elasticsearch and .NET

šljaker picture šljaker · Jun 2, 2014 · Viewed 20.1k times · Source

We're considering to switch from Solr/Solr.net to Elasticsearch. We started with NEST. We have only 4 documents in search index.

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

The code above takes approx. 250ms, while the same code with HttpClient and JsonSerializer takes 30-45ms. 250ms is too much time for just 4 documents.

Can NEST be used on high-traffic news website, or do you recommend HttpClient + JsonSerializer combo? The search page was the most visited page on our website in 2013.

Thanks in advance.

Answer

C Tierney picture C Tierney · Jun 17, 2014

There are two things that have to happen in order for NEST to make the first request.

  1. The Json Serializer (Json.net) in this case has to cache the type so that it knows how to serialize and deserialize the object you are sending back and forth.

  2. Nest has its own fluent language for queries that must be translated from the intial types that represent the fluent query language and the delivered as JSON to elastic search. These document types also must be learned by the Json Serializer.

  3. The HTTP client has to be spun up to make the request.

I currently have over 4M documents in a single index that I use with NEST and my searches from server all the way to client over the internet are taking 50-70 ms using NEST. Like you, however, after a cold start the first request is slow.