Elasticsearch - C#NEST - 更改字典<k,v>映射行为

时间:2017-03-11 15:05:49

标签: c# json elasticsearch nest elasticsearch-net

我想更改C#NEST中的映射行为。 我的班级看起来像:

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

默认自动映射(例如)

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}}});

生成以下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
          }
        }
      }
    }
  }
}

如您所见 - A,B,C,D,F是浮动属性。如果ES中有许多类型属性 - 它会降低整个服务器的速度。所以我想将字典映射到键值对,如下所示:

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

所以我目前的解决方案是:

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() });

没有.ToArray(),是否有更好的解决方案;或者构建一个嵌套类?

0 个答案:

没有答案