gpt4 book ai didi

elasticsearch - 在Elasticsearch Nest中,将一种类型映射到另一种类型

转载 作者:行者123 更新时间:2023-12-03 01:39:36 24 4
gpt4 key购买 nike

我正在使用elasticsearch nest C#库来映射文档。除了自定义类型TypeX之外,一切都进行得很顺利,我将其定义为对象并存储为{}。

此自定义类型是v1 uuid,本质上被视为Guid。老实说,我想将其作为Guid进行存储,因为它隐式地来回转换。因此,elastic会将其视为Guid而不是TypeX。

根据attribute mapping部分,看来我可以以这种方式更改类型,但是我真的不想将Nest公开为我的类型的依赖项,因为它在很多地方都使用过。

是否可以从连接或索引中设置此映射以将TypeX映射到Guid,将Guid映射到TypeX?

巢:6.0.1

ES:6.2.2

最佳答案

使用NEST自动映射将System.Guid映射为keyword数据类型。鉴于以下文件

public class MyDocument
{
public Guid UserId { get; set; }
}

和以下映射
var client = new ElasticClient();

var createIndexResponse = client.CreateIndex("foo", c => c
.Mappings(m => m
.Map<MyDocument>(mm => mm
.AutoMap()
)
)
);

将产生
{
"mappings": {
"mydocument": {
"properties": {
"userId": {
"type": "keyword"
}
}
}
}
}

现在,要将您自己的类型映射为 keyword类型,如果您不想为POCO分配属性,则可以使用流畅的映射。给定以下POCO和自定义 Uuid类型
public class Uuid
{
private string _value;

public Uuid(string value) => _value = value;

public override string ToString() => _value;
}

public class MyDocument
{
public Uuid UserId { get; set; }
}

这些可以与
var createIndexResponse = client.CreateIndex("foo", c => c
.Mappings(m => m
.Map<MyDocument>(mm => mm
.AutoMap()
.Properties(p => p
.Keyword(k => k
.Name(n => n.UserId)
)
)
)
)
);

产生与以前相同的映射。但是,这只是故事的一半,因为我们还需要控制 Uuid的序列化和反序列化方式,以便将其序列化为JSON string并可以从 string构造实例。在NEST 6.x中,我们需要 use our own serializer for this,因为NEST使用的序列化器是内部的。

NEST.JsonSerializer nuget package包含一个使用Json.NET的自定义序列化程序,因此您可以编写 JsonConverter来处理 Uuid类型的序列化
public class UuidConverter : JsonConverter
{
public override bool CanConvert(Type objectType) =>
typeof(Uuid).IsAssignableFrom(objectType);

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) =>
reader.TokenType == JsonToken.String
? new Uuid((string)reader.Value)
: null;

public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
if (value != null)
writer.WriteValue(value.ToString());
else
writer.WriteNull();
}
}

private static void Main()
{
var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));

// configure NEST to use Json.NET for serialization of your documents
// and register a custom converter to handle Uuid type
var settings = new ConnectionSettings(pool, (builtin, s) =>
new JsonNetSerializer(builtin, s, contractJsonConverters:
new [] { new UuidConverter() }
))
.DefaultIndex("foo");

var client = new ElasticClient(settings);

var createIndexResponse = client.CreateIndex("foo", c => c
.Mappings(m => m
.Map<MyDocument>(mm => mm
.AutoMap()
.Properties(p => p
.Keyword(k => k
.Name(n => n.UserId)
)
)
)
)
);

var document = new MyDocument
{
UserId = new Uuid("123e4567-e89b-12d3-a456-426655440000")
};

var indexResponse = client.IndexDocument(document);
}

然后,文档索引请求将 Uuid序列化为字符串
POST http://localhost:9200/foo/mydocument
{
"userId": "123e4567-e89b-12d3-a456-426655440000"
}

关于elasticsearch - 在Elasticsearch Nest中,将一种类型映射到另一种类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48940246/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com