{"id":133,"date":"2018-06-23T19:09:03","date_gmt":"2018-06-23T19:09:03","guid":{"rendered":"http:\/\/tonysbit.blog\/?p=133"},"modified":"2018-06-23T19:09:03","modified_gmt":"2018-06-23T19:09:03","slug":"c-time-savers-for-elasticsearch","status":"publish","type":"post","link":"https:\/\/tonysbit.blog\/?p=133","title":{"rendered":"C# Time-savers for Elasticsearch"},"content":{"rendered":"
If you are currently developing using C# (Particularly .NET Core 2.0+) here are some shortcuts I hope will be able to save you time I wish I could have back.<\/p>\n
There is official documentation for C# Elasticsearch development however I found the examples to be quite lacking. I do recommend going through the documentation <\/a>anyway especially for the NEST client as it is essential to understand Elasticsearch with C#.<\/p>\n “The low level client,\u00a0 – ElasticSearch Official Documentation<\/a><\/p><\/blockquote>\n Unfortunately the low level client in particular has very sparse documentation especially examples. The following was discovered through googling and painstaking testing.<\/p>\n JObjects are quite popular way to work with JSON objects in .NET, as such it may be required to parse JObjects to Elasticsearch, this may be a result of one of the following:<\/p>\n The JObject cannot be used as the generic for indexing as you will receive this error:<\/p>\n Instead use “BytesResponse” as the <T> Class<\/p>\n The examples given by the Elasticsearch documentation does not give an example of a bool query using the low-level client. Why is the “Bool” query particularly difficult? Using Query DSL<\/a> in C#, “bool” will automatically resolve to the class and therefore will throw a error:<\/p>\n Not very anonymous type friendly… the solution to this one is quite simple, add a ‘@’ character in-front of the bool.<\/p>\n This one seems a-bit obvious but if you want to define an array for use with DSL, use the anonymous typed Array (Example can be seen in figure 4) Nested fields in Elasticsearch are stored as a full path, The solution is to define a Dictionary and use the dictionary in the anonymous type.<\/p>\n The Dictionary can be passed by the anonymous type and will successfully query the Nested field in Elasticsearch.<\/p>\n “The high level client,\u00a0 –ElasticSearch Official Documentation<\/a><\/p><\/blockquote>\n The NEST documentation is much more comprehensive, the only issue I found was using keyword Term searches.<\/p>\n All string fields are mapped by default to both text and keyword, the documentation can be found here.<\/a>\u00a0Issue is that in the strong typed object used in the Elastic Mapping there is no “.keyword” field to reference therefore a error is thrown.<\/p>\n Example:<\/p>\n For the Object:<\/p>\n1. Low Level Client<\/h1>\n
ElasticLowLevelClient<\/code>, is a low level, dependency free client that has no opinions about how you build and represent your requests and responses.”<\/p>\n
1.1. Using\u00a0 JObjects in Elasticsearch<\/h2>\n
\n
1.2. Running a “Bool” query<\/h2>\n
1.3. Defining Anonymous Arrays<\/h2>\n
new Object[]<\/code>.<\/p>\n
1.4. Accessing nested fields in searches<\/h2>\n
.<\/code> delimited string. This creates a problem when trying to query that field specifically as it creates a invalid type for anonymous types.<\/p>\n
2. NEST Client<\/h1>\n
ElasticClient<\/code>, provides a strongly typed query DSL that maps one-to-one with the Elasticsearch query DSL.”<\/p>\n
2.1. Using Keyword Fields<\/h2>\n