Elasticsearch and .NET

We plan to move from Solr / Solr.net to Elasticsearch. We started with NEST . We have only 4 documents in the 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 above code takes approx. 250 ms, while the same code with HttpClient and JsonSerializer takes 30-45 ms. 250 ms - too much time for just 4 documents.

Is it possible to use NEST on a high-traffic news website, or do you recommend the HttpClient + JsonSerializer combo? The search page was the most visited page on our website in 2013.

Thanks in advance.

+6
source share
2 answers

There are two things that must happen for NEST to complete the first request.

  • The Json Serializer (Json.net) in this case must cache the type so that it knows how to serialize and deserialize the object you send back and forth.

  • Nest has its own free language for queries, which must be translated from the main types, which are a free query language, and delivered as JSON for elasticity searches. These types of documents should also be studied using the Json Serializer.

  • The HTTP client must be deployed to complete the request.

Currently, I have more than 4 million documents in one index, which I use with NEST, and my search queries from server to client via the Internet take 50-70 ms using NEST. Like you, however, after a cold start, the first request is slow.

+4
source

I suggest you use https://github.com/ServiceStack/ServiceStack.Text , the fastest Json serializer for C #.

For the driver, use the low-level, http://nest.azurewebsites.net/elasticsearch-net/quick-start.html

below the code that I started writing to register my applications in detail and analyze them. Still having to work, but may be a good start.

 using System; using System.Configuration; using Elasticsearch.Net; using Elasticsearch; using Elasticsearch.Net.Connection; using Elasticsearch.Net.ConnectionPool; namespace Common { /// <summary> /// Elastic search. Singletone, open connection and thread safe to be open for all the time /// the app is running, so we send ours logs to ealsticsearch to be analyzed, assychronly /// See the fourth version; /// http://csharpindepth.com/articles/general/singleton.aspx /// </summary> public sealed class ElasticSearch { // our instance of ourself as a singleton private static readonly ElasticSearch instance = new ElasticSearch(); ElasticsearchClient client; string connectionString = ConfigurationManager.ConnectionStrings["Elasticsearch"].ConnectionString; /// <summary> /// Initializes a new instance of the <see cref="Common.ElasticSearch"/> class. /// Follow this: http://nest.azurewebsites.net/elasticsearch-net/connecting.html /// We use a ConnectionPool to make the connection fail-over, that means, if the /// connection breaks, it reconnects automatically /// </summary> private ElasticSearch() { var node = new Uri(connectionString); var connectionPool = new SniffingConnectionPool(new[] { node }); var config = new ConnectionConfiguration(connectionPool); client = new ElasticsearchClient(config); // exposed in this class } static ElasticSearch() { } /// <summary> /// Gets the instance of our singleton class /// </summary> /// <value>The instance.</value> public static ElasticSearch Instance { get { return instance; } } /// <summary> /// Log the specified module, id and json. /// </summary> /// <param name="type">Here the entity you want to save your log, /// let use it based on classes and StateMachines</param> /// <param name="id">Identifier. alwayes the next</param> /// <param name="json">Json.</param> public void Log(string type, string id, string json) { client.Index("mta_log", type, id, json); } } } 
+2
source

Source: https://habr.com/ru/post/970209/


All Articles