c# - 如何获取 ElasticSearch 索引中的现有映射

标签 c# .net elasticsearch nest

我想使用 Nest 和 C# 检查索引中存在的映射。

var settings = new ConnectionSettings(new Uri("http://localhost:9200"));
var client = new ElasticClient(settings);
var status = client.Status();

这将返回 ES 服务器的可用索引。但我也想知道这些索引中映射了哪些类型。 我尝试使用:

var mapping = client.GetMapping(???);

但是这些方法和重载似乎需要映射的名称。这正是我想要找出的。对于这种情况,我找不到合适的文档。

最佳答案

您可以使用任一重载来下拉特定索引/类型的映射(但目前不是复数索引或类型)

client.GetMapping(g => g.Index("myindex").Type("mytype")));

对比

client.GetMapping(new GetMappingRequest {Index = "myindex", Type = "mytype"});

我不确定当你隐式提供 <object> 时会发生什么(它可能会爆炸;我不是在 Windows 机器上测试它),但您显然不知道要放在那里的类型 ( T) 并且需要一些东西。

不幸的是,上面的当前限制(假设它适用于 <object> )是你必须提供一个 Index ,以及可选的 Type .如果不指定 Type ,但是 Index包含多个类型,那么它只会选择第一个返回的类型。我怀疑那是你想要的,这就是为什么 I created an issue for it on GitHub在与 Greg(NEST 开发人员之一)讨论后。

幸运的是,在 NEST 中总有一个退路,那就是转到较低级别的 Elasticsearch.NET API。在那里你可以制作你的 IndicesGetMapping要求。查看generated tests can be found here ,这可能有助于更好地理解生成的请求。

var response = client.IndicesGetMapping("test_1,test_2");

// Deserialized JSON:
//
// response.test_1.mappings.type_1.properties
// response.test_1.mappings.type_2.properties
// response.test_2.mappings.type_a.properties

注意,也可以使用这些重载:

// First parameter is indices (comma separated, potentially wildcarded list)
// _all is a special placeholder to [shockingly] specify all
var response = client.IndicesGetMapping("_all", "_all");
var response = client.IndicesGetMapping("index1,index2", "_all");
// Enjoy the loops:
var response = client.IndicesGetMappingForAll();

这些可以是found in IElasticsearchClient.Generated (巨大的文件,所以搜索“IndicesGetMapping”)。

关于c# - 如何获取 ElasticSearch 索引中的现有映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27043965/

相关文章:

c# - 我认为我的 SerialPort 对此 ObjectDisposedException 负责,但我不知道它发生在哪里

c# - Azure 服务总线属性筛选器

elasticsearch - 如何使用Elasticsearch 7对字段进行排序

python - 如果注册的查询包含术语,则由ElasticSearch的Percolate API匹配的文档始终不返回任何匹配项

c# - 如何使用 ASP.NET 显示消息并发送文件

c# - 如何从组中获取第一项并准备数据类对象

c# - 可以在应用程序中强制使用特定的方法签名吗?

c# - 20 位整数数学

c# - 即时生成自签名证书

curl - Elasticsearch文档TTL无法正常工作