elasticsearch 使用NEST 5.0为动态对象编制索引
我在两年前发现了这个问题 我基本上有完全相同的问题,但使用的是NEST 5.0。提议的解决方案在最新版本中显然不起作用
elasticsearch 使用NEST 5.0为动态对象编制索引,
elasticsearch,nest,
elasticsearch,Nest,我在两年前发现了这个问题 我基本上有完全相同的问题,但使用的是NEST 5.0。提议的解决方案在最新版本中显然不起作用 强制转换到对象,然后建立索引,结果是一个elasticsearch文档,源标记中没有字段 缺少esClient.Raw.Index api 在NEST 5.x中处理动态类型与在NEST 1.x中类似;在这两个版本之间,一些客户机API有一些变化,但前提仍然是一样的 这里有一个例子 var pool = new SingleNodeConnectionPool(new Uri(
- 强制转换到对象,然后建立索引,结果是一个elasticsearch文档,源标记中没有字段
- 缺少esClient.Raw.Index api
var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
var defaultIndex = "default-index";
var connectionSettings = new ConnectionSettings(pool)
.DefaultIndex(defaultIndex);
var client = new ElasticClient(connectionSettings);
// delete the index if it already exists
if (client.IndexExists(defaultIndex).Exists)
client.DeleteIndex(defaultIndex);
client.CreateIndex(defaultIndex);
// create an anonymous type assigned to a dynamically typed variable
dynamic instance = new
{
Name = "Russ",
CompanyName = "Elastic",
Date = DateTimeOffset.UtcNow
};
// cast the instance to object to index, explicitly
// specify the document type and index
var indexResponse = client.Index((object)instance, i => i
.Type("my_type")
.Index(defaultIndex)
);
// fetch the document just indexed
var getResponse = client.Get<dynamic>(indexResponse.Id, g => g
.Type(indexResponse.Type)
.Index(indexResponse.Index)
);
这表明文档已按预期编制索引,并且可以检索原始源文档
可以通过.LowLevel
属性在NEST 2.x和5.x中的高级客户机上访问低级客户机,因此您可以使用执行类似于链接问题的操作
dynamic instance = new
{
Id = "id",
Index = defaultIndex,
Type = "my_type",
Document = new
{
Name = "Russ",
CompanyName = "Elastic",
Date = DateTimeOffset.UtcNow
}
};
string documentJson = client.Serializer.SerializeToString((object)instance.Document);
var result = client.LowLevel.Index<string>(instance.Index, instance.Type, instance.Id, documentJson);
动态实例=新建
{
Id=“Id”,
索引=默认索引,
Type=“我的类型”,
文件=新
{
Name=“Russ”,
CompanyName=“弹性”,
日期=DateTimeOffset.UtcNow
}
};
string documentJson=client.Serializer.SerializeToString((对象)instance.Document);
var result=client.LowLevel.Index(instance.Index,instance.Type,instance.Id,documentJson);
您是否收到不当行为/错误消息?你试过什么方法来修复它吗?请仔细阅读,有很多格言
dynamic instance = new
{
Id = "id",
Index = defaultIndex,
Type = "my_type",
Document = new
{
Name = "Russ",
CompanyName = "Elastic",
Date = DateTimeOffset.UtcNow
}
};
string documentJson = client.Serializer.SerializeToString((object)instance.Document);
var result = client.LowLevel.Index<string>(instance.Index, instance.Type, instance.Id, documentJson);