NEST 中的协变

Convariant search results

NEST 直接支持返回协变结果结果集合。这意味着,可以将搜索结果的类型指定为一个接口或者基类,但是其真实类型仍然是接口或基类的一个子类型。

让我们来看一个例子,假设我们想要搜索的多个类型都实现了 ISearchResult 接口

public interface ISearchResult
{
    string Name { get; set; }
}

public abstract class BaseX : ISearchResult
{
    public string Name { get; set; }
}

针对 ISearchResult ,我们提供了三种实现,分别叫 A, BC

public class A : BaseX
{
    public int PropertyOnA { get; set; }
}

public class B : BaseX
{
    public int PropertyOnB { get; set; }
}

public class C : BaseX
{
    public int PropertyOnC { get; set; }
}

使用 Types

要想搜索多个类型,最直接的方式是,先将响应的类型指定为接口或者基类,然后使用 .Type() 传递我们想搜索的实际类型

var result = this._client.Search<ISearchResult>(s => s
    .Type(Types.Type(typeof(A), typeof(B), typeof(C)))
    .Size(100)
);

NEST 会把这份逻辑代码转化为 /index/a,b,c/_search ,有 "_type" : "a" 的命中结果会被序列化为 A 类型的对象,以此类推。

在这里,我们假设所有的响应都是有效的,并且我们将接收到预期的 100 份文档。记住, result.Document 是一个 IReadOnlyCollection<ISearchResult> 类型的对象

result.ShouldBeValid();
result.Documents.Count.Should().Be(100);

为了证明返回的结果集合是协变的,我们根据它们的真实类型过滤文档,并断言这些子集是我们期望的大小

var aDocuments = result.Documents.OfType<A>();
var bDocuments = result.Documents.OfType<B>();
var cDocuments = result.Documents.OfType<C>();

aDocuments.Count().Should().Be(25);
bDocuments.Count().Should().Be(25);
cDocuments.Count().Should().Be(50);

另外,假设只存在于子类本身的属性会被正确填充

aDocuments.Should().OnlyContain(a => a.PropertyOnA > 0);
bDocuments.Should().OnlyContain(a => a.PropertyOnB > 0);
cDocuments.Should().OnlyContain(a => a.PropertyOnC > 0);

使用 ConcreteTypeSelector

一个相对较低级别的方式是,自己检查命中结果,并确定要反序列化的 CLR 类型

var result = this._client.Search<ISearchResult>(s => s
    .ConcreteTypeSelector((d, h) => h.Type == "a" ? typeof(A) : h.Type == "b" ? typeof(B) : typeof(C))
    .Size(100)
);

对于每一个命中结果,我们会调用传递给 ConcreteTypeSelector 的委托

  • d 表示 _source 公开为一个 dynamic 类型
  • 带有类型的 h 代表着对命中结果的封装(例如:Hit<dynamic>

我们假设响应是有效的,并且接收到了预期的 100 份文档。记住, result.Document 是一个 IReadOnlyCollection<ISearchResult> 类型的对象

result.ShouldBeValid();
result.Documents.Count.Should().Be(100);

为了证明返回的结果集合是协变的,我们根据它们的真实类型过滤文档,并断言这些子集是我们期望的大小

var aDocuments = result.Documents.OfType<A>();
var bDocuments = result.Documents.OfType<B>();
var cDocuments = result.Documents.OfType<C>();

aDocuments.Count().Should().Be(25);
bDocuments.Count().Should().Be(25);
cDocuments.Count().Should().Be(50);

另外,假设只存在于子类本身的属性会被正确填充

aDocuments.Should().OnlyContain(a => a.PropertyOnA > 0);
bDocuments.Should().OnlyContain(a => a.PropertyOnB > 0);
cDocuments.Should().OnlyContain(a => a.PropertyOnC > 0);

使用 CovariantTypes

Scroll API 是上一个搜索示例的延伸,我们不再使用 Types() 。你可以使用 .CovariantTypes() 来指示类型。

var result = this._client.Scroll<ISearchResult>(TimeSpan.FromMinutes(60), "scrollId", s => s
    .CovariantTypes(Types.Type(typeof(A), typeof(B), typeof(C)))
);

NEST 会把这份逻辑代码转化为 /index/a,b,c/_search ,有 "_type" : "a" 的命中结果会被序列化为 A 类型的对象,以此类推。

后续的验证过程同上(也许你已经发现前面的两节内容里存在者相同的步骤)

相对较低级别的 concrete type selector 也可以用在 scroll 中

var result = this._client.Scroll<ISearchResult>(TimeSpan.FromMinutes(1), "scrollid", s => s
    .ConcreteTypeSelector((d, h) => h.Type == "a" ? typeof(A) : h.Type == "b" ? typeof(B) : typeof(C))
);

重复验证工作

协变不仅仅在接口方式中工作,下面便是子类型协变的示例:

var result = this._client.Search<BaseX>(s => s
    .Type(Types.Type(typeof(A), typeof(B), typeof(C)))
    .Size(100)
);
result.ShouldBeValid();
result.Documents.Count.Should().Be(100);

var aDocuments = result.Documents.OfType<A>();
var bDocuments = result.Documents.OfType<B>();
var cDocuments = result.Documents.OfType<C>();

aDocuments.Count().Should().Be(25);
bDocuments.Count().Should().Be(25);
cDocuments.Count().Should().Be(50);

aDocuments.Should().OnlyContain(a => a.PropertyOnA > 0);
bDocuments.Should().OnlyContain(a => a.PropertyOnB > 0);
cDocuments.Should().OnlyContain(a => a.PropertyOnC > 0);

相关推荐