2

I want to change the mapping behavior in C# NEST. My class looks like:

public class Vector
{
  public Guid Guid { get; set; } = Guid.Empty;
  public Dictionary<string, double> Entries { get; set; }
}

The default auto mapping (e.g.)

var client = new ElasticClient(settings);
client.Map<Vector>(m => m.AutoMap());
// index sample data
client.Index(new Vector { Guid = Guid.NewGuid(),Entries = new Dictionary <string, double> { {"A", 5.3}, {"B", 1.3}, {"C", 7.7}}});
client.Index(new Vector { Guid = Guid.NewGuid(), Entries = new Dictionary<string, double> { {"D", 2.3}, {"B", 8.0}, {"F", 5.1}}});
client.Index(new Vector { Guid = Guid.NewGuid(), Entries = new Dictionary<string, double> { {"A", 2.2}, {"B", 0.3}, {"F", 5.9}}});

generates the following Elasticsearch-mapping:

"mappings" : {
  "vector" : {
    "properties" : {
      "entries" : {
        "properties" : {
          "A" : {
            "type" : "float"
          },
          "B" : {
            "type" : "float"
          },
          "C" : {
            "type" : "float"
          },
          "D" : {
            "type" : "float"
          },
          "F" : {
            "type" : "float"
          }
        }
      },
      "guid" : {
        "type" : "text",
        "fields" : {
          "keyword" : {
            "type" : "keyword",
            "ignore_above" : 256
          }
        }
      }
    }
  }
}

As you can see - A, B, C, D, F are float-properties. If you have to many type properties in ES - it will slow down the whole server. So I want to map the dictionary to a key-value-pair, like this:

"mappings" : {
  "vector" : {
    "properties" : {
      "entries" : {
        "properties" : {
          "key" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          },
          "value" : {
            "type" : "float"
          }
        }
      }
      ...
    }
  }
}

So my current solution is:

public class Vector
{
  public Guid Guid { get; set; } = Guid.Empty;
  public KeyValuePair<string, double>[] Entries { get; set; }
}
// ...
client.Index(new Vector { Guid = Guid.NewGuid(), entries = dictionary.ToArray() });

Is there a better solution without .ToArray(); or build a nested class?

notesjor
  • 19
  • 1
  • Could you explain what you mean by _"If you have to many type properties in ES - it will slow down the whole server."_? – Russ Cam Mar 11 '17 at 20:44
  • My expirience is: If you have more than 50000 entries in the dictionary and map them in the "client.Map(m => m.AutoMap());"-way you will recive a very slow ES. – notesjor Mar 11 '17 at 23:30

0 Answers0