Elasticsearch Type mappings with ElasticsearchCRUD

This article shows how to define mappings for types in Elasticsearch using ElasticsearchCRUD. The Core Types definitions in Elasticsearch can be defined using attributes with ElasticsearchCRUD.

Code: https://github.com/damienbod/ElasticsearchCrudTypeMappingsExample

Other Tutorials:

Part 1: ElasticsearchCRUD introduction
Part 2: MVC application search with simple documents using autocomplete, jQuery and jTable
Part 3: MVC Elasticsearch CRUD with nested documents
Part 4: Data Transfer from MS SQL Server using Entity Framework to Elasticsearch
Part 5: MVC Elasticsearch with child, parent documents
Part 6: MVC application with Entity Framework and Elasticsearch
Part 7: Live Reindex in Elasticsearch
Part 8: CSV export using Elasticsearch and Web API
Part 9: Elasticsearch Parent, Child, Grandchild Documents and Routing
Part 10: Elasticsearch Type mappings with ElasticsearchCRUD
Part 11: Elasticsearch Synonym Analyzer using ElasticsearchCRUD
Part 12: Using Elasticsearch German Analyzer
Part 13: MVC google maps search using Elasticsearch
Part 14: Search Queries and Filters with ElasticsearchCRUD
Part 15: Elasticsearch Bulk Insert
Part 16: Elasticsearch Aggregations With ElasticsearchCRUD
Part 17: Searching Multiple Indices and Types in Elasticsearch
Part 18: MVC searching with Elasticsearch Highlighting
Part 19: Index Warmers with ElasticsearchCRUD

Mapping Attributes

Mapping definitions can be defined using attributes in ElasticsearchCRUD. Most of the Elasticsearch Core Types definitions are supported. Here’s an example of a class with Elasticsearch mapping definitions.

public class AmazingThisMapping
{
	public int Id { get; set; }

	[ElasticsearchInteger(Coerce=true)]
	public int NumberOf { get; set; }

	[ElasticsearchString(CopyTo = "data")]
	public string Name { get; set; }

	public string Description { get; set; }

	public string Data { get; set; }

	[ElasticsearchInteger]
	public short SmallAmount { get; set; }

	[ElasticsearchString(Boost = 1.4, Fields = typeof(FieldDataDefNotAnalyzed), Index = StringIndex.analyzed)]
	public string DescriptionBothAnayzedAndNotAnalyzed { get; set; }

	[ElasticsearchDouble(Boost = 2.0,Store=true)]
	public double Cost { get; set; }

	[ElasticsearchDate]
	public DateTime Timestamp { get; set; }

	[ElasticsearchDate]
	public DateTimeOffset TimestampWithOffset { get; set; }
}

Once the class with the mappings has been defined, it can be created in Elasticsearch with the context.CreateIndex method.

using (var context = new ElasticsearchContext(ConnectionString, new ElasticsearchSerializerConfiguration(ElasticsearchMappingResolver)))
{
 context.TraceProvider = new ConsoleTraceProvider();
 context.CreateIndex<AmazingThisMapping>();
}

Example of not_analyzed, analyzed string fields

Sometimes it is required to save both the analyzed and also the non-analyzed strings for search requests. This is achieved with fields properties in Elasticsearch. This can be defined in ElasticsearchCRUD with The Fields property in the ElasticsearchString attribute. This attribute property requires a Type. The type then defines all the required properties for the fields definition.

public class Whatever
{
  [ElasticsearchString(Boost = 1.4, Fields = typeof(FieldDataDefNotAnalyzed), Index = StringIndex.analyzed)]
  string DescriptionBothAnayzedAndNotAnalyzed { get; set; }
}

This class is used to define the field mappings in the fields property. This example has just one example, but you can define as many as required.
public class FieldDataDefNotAnalyzed
{
  [ElasticsearchString(Index = StringIndex.not_analyzed)]
  public string Raw { get; set; }
}

This creates the following mapping:

 "descriptionbothanayzedandnotanalyzed": {
   "type": "string",
   "boost": 1.4,
   "fields": {
   "raw": {
      "type": "string",
      "index": "not_analyzed"
   }
  }
}

copy_to Definition

The copy_to field mapping can be defined using the CopyTo property or the CopyToList property.

public class Whatever
{
  [ElasticsearchString(CopyTo = "data")]
  public string Name { get; set; }
  public string Description { get; set; }
  public string Data { get; set; }
}

This can then be used in a search query. The following query searches the data field for matching “World” string.

{
 "query": {
   "bool": {
      "must": [
      {
         "match" : {
            "data" : "World"
         }
      }
     ]
   }
 }
}

To see the full list of possible type and mapping definitions and also the meaning of each attribute property, refer to the Core Types documentation in Elasticsearch. These are defined in ElasticsearchCRUD in the ElasticsearchCRUD.ContextAddDeleteUpdate.CoreTypeAttributes namespace. These mapping definitions can then be used for any kind of document structure in ElasticsearchCRUD.

Links:

https://www.nuget.org/packages/ElasticsearchCRUD/

http://www.elasticsearch.org/

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.