1

I have a product model something like this:

public string Id {get; set;}
public List<FieldValue> Fields { get; set; }

where object FieldValue is

public string FieldName {get; set;}
public string FieldType {get; set;}
public List<FieldData> Data  {get;set;}

and FieldData

public string Value {get; set;}

User can add as many custom Fields as he wants for ex:

Id: 638f1304f2d753647be09c1a
Fields:[{
        FieldName: "Price",
        FieldType: "double"
        Data: [
               {"14.50"}]
       },

       {
        FieldName: "Description",
        FieldType: "string"
        Data: [
               {"Some text here"}]
       }]

FieldType can be different (string, double, int).

Now I need to index each of these products to allow advanced search on these fields. For "Price" field to map it as a double field in elastic search and allow range queries on it, for "Description" as "keyword" and similar.

Is it possible to have some method when product comes for indexing first will check type of the field and will create mappings on fly for each (if FieldType == "string" => map as "keyword", if FieldType == "double" => map as "double")?

Im working with .Net Core 6, and latest NEST client.

Flow is: on event product is created, get product from db(mongo) => (prepare mapping?) => index that product in els.

I use only AutoMap(). I read about dynamic templates, looks like something what I need here but not sure how to implement in net core with NEST client.

Avatar
  • 25
  • 4
  • You can check my two answers about cases like yours, maybe you will find those helpful. [1](https://stackoverflow.com/questions/72801933/elasticsearch-nest-create-index-mapping-dynamically/72802051#72802051) and [2](https://stackoverflow.com/a/72802051/290460). – Rob Dec 09 '22 at 13:18

1 Answers1

1

I think I can use this:

Dictionary<string, object> IndexedFields { get; set; } 

I have info in db which type should it be and then I have one custom method to convert this string fields in appropriate type before sending document on indexing.

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
Avatar
  • 25
  • 4