8

Elasticsearch.Nest 教程系列 9-7 转换:Indices Paths | 索引路径

 2 years ago
source link: https://blog.zhuliang.ltd/2020/01/backend/Elasticsearch-Nest-Indices-Paths.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Elasticsearch.Nest 教程系列 9-7 转换:Indices Paths | 索引路径

create: 2020-01-23 11:01:01 | update: 2020-01-23 12:15:44 本文总阅读量: 198 次  |  文章总字数: 972 字  |  阅读约需: 4 分钟


Elasticsearch 中的某些API在请求的 URI 中回需要使用索引名称,索引名称的集合或特殊的 _all 标记(表示所有索引),以指定在哪个(哪些)索引上进行请求。

在 Nest 中,这些索引的名称通过 Indices 类型来进行指定。

以下几种类型会隐式转换:

  • string。
  • 逗号分隔的 string。
  • string 数组。
  • 已经在 ConnectionSettings 上设置相关类型的索引名称的 CLR 类。
  • IndexName 类
  • IndexName 数组
Nest.Indices singleIndexFromString = "name";
Nest.Indices multipleIndicesFromString = "name1, name2";
Nest.Indices multipleIndicesFromStringArray = new [] { "name1", "name2" };
Nest.Indices allFromString = "_all";

Nest.Indices allWithOthersFromString = "_all, name2"; 

Nest.Indices singleIndexFromType = typeof(Project); 

Nest.Indices singleIndexFromIndexName = IndexName.From<Project>();

singleIndexFromString.Match(
    all => all.Should().BeNull(),
    many => many.Indices.Should().HaveCount(1).And.Contain("name")
);

multipleIndicesFromString.Match(
    all => all.Should().BeNull(),
    many => many.Indices.Should().HaveCount(2).And.Contain("name2")
);

allFromString.Match(
    all => all.Should().NotBeNull(),
    many => many.Indices.Should().BeNull()
);

allWithOthersFromString.Match(
    all => all.Should().NotBeNull(),
    many => many.Indices.Should().BeNull()
);

multipleIndicesFromStringArray.Match(
    all => all.Should().BeNull(),
    many => many.Indices.Should().HaveCount(2).And.Contain("name2")
);

singleIndexFromType.Match(
    all => all.Should().BeNull(),
    many => many.Indices.Should().HaveCount(1).And.Contain(typeof(Project))
);

singleIndexFromIndexName.Match(
    all => all.Should().BeNull(),
    many => many.Indices.Should().HaveCount(1).And.Contain(typeof(Project))
);
  • _all 会覆盖所有指定的索引名称。
  • Project 类已经通过在 ConnectionSettings..DefaultMappingFor 指定索引名称。

使用 Nest.Indices 方法

为了简化创建 IndexName 或者 Indices 实例,Nest.Indices 包含了好几个静态方法供使用。

单索引可以通过使用 CLR 类、字符串或者 .Index() 方法来进行指定。

var client = TestClient.Default;

var singleString = Nest.Indices.Index("name1");  //通过字符串"name1"进行指定
var singleTyped = Nest.Indices.Index<Project>(); //通过 Project 类进行指定。

ISearchRequest singleStringRequest = new SearchDescriptor<Project>().Index(singleString);
ISearchRequest singleTypedRequest = new SearchDescriptor<Project>().Index(singleTyped);

((IUrlParameter)singleStringRequest.Index).GetString(this.Client.ConnectionSettings).Should().Be("name1");
((IUrlParameter)singleTypedRequest.Index).GetString(this.Client.ConnectionSettings).Should().Be("project");

跟单索引类似,可以通过使用多个 CLR 类型或者 多个“字符串”来进行多索引的创建。

var manyStrings = Nest.Indices.Index("name1", "name2"); // 使用字符串
var manyTypes = Nest.Indices.Index<Project>().And<Developer>();  //使用 CLR 类
var client = TestClient.Default;

ISearchRequest manyStringRequest = new SearchDescriptor<Project>().Index(manyStrings);
ISearchRequest manyTypedRequest = new SearchDescriptor<Project>().Index(manyTypes);

((IUrlParameter)manyStringRequest.Index).GetString(this.Client.ConnectionSettings).Should().Be("name1,name2");
((IUrlParameter)manyTypedRequest.Index).GetString(this.Client.ConnectionSettings).Should().Be("project,devs");  //这里的索引名称来自于 TestClient 中配置的 ConnectionSetting

manyStringRequest = new SearchDescriptor<Project>().Index(new[] { "name1", "name2" });
((IUrlParameter)manyStringRequest.Index).GetString(this.Client.ConnectionSettings).Should().Be("name1,name2");

通过 _all 标识,你可以在 ES 的所有索引中进行查询。

Nest 提供了 Indices.All 以及 Indices.AllIndices 来对外暴露 _all 标记。

为什么要提供这 2 个属性而不是一个:Indices.All 或者 Indices.AllIndices?

  • 因为你可能同时使用了 Nest.Indices 和 Nest.Type,并且还使用了 C# 6 的一些特性,这种情况下,Indices.All 和 Types.All 的All 属性就变得摸棱两可了,为了消除这种歧义,应使用 Indices.AllIndices 来代表 _all 标记。
var indicesAll = Nest.Indices.All;
var allIndices = Nest.Indices.AllIndices;

ISearchRequest indicesAllRequest = new SearchDescriptor<Project>().Index(indicesAll);
ISearchRequest allIndicesRequest = new SearchDescriptor<Project>().Index(allIndices);

((IUrlParameter)indicesAllRequest.Index).GetString(this.Client.ConnectionSettings).Should().Be("_all");
((IUrlParameter)allIndicesRequest.Index).GetString(this.Client.ConnectionSettings).Should().Be("_all");

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK