我看到这样的例子:
var result = this._client.Search(s => s
.Index("my-index")
.Type("my-type")
.Query(q=> ....)
.Filter(f=> ....)
);
但是当我使用它时,我得到:
The type arguments for method 'Nest.ElasticClient.Search<T>(System.Func<Nest.SearchDescriptor<T>,Nest.SearchDescriptor<T>>)' cannot be inferred from the usage. Try specifying the type arguments explicitly.
我有很多不同的类型,我不想为所有类型创建类。我可以使用 NEST 吗?它的搜索没有像 Search<MyType>
这样的类型?
谢谢
最佳答案
我已经成功使用了Search<dynamic>
以避免需要依赖于特定类型。然后,当我得到结果时,我可以检查它们并根据需要转换/转换为特定的 POCO。
我使用类似下面的东西:
var result = client.Search<dynamic>(s => s
.Index("myIndex")
.AllTypes()
.Query(q => ...)
.Filter(f => ...)
);
foreach (var hit in result.Hits.Hits)
{
//check hit.Type and then map hit.Source into the appropriate POCO.
// I use AutoMapper for the mapping, but you can do whatever suits you.
if (string.Compare(hit.Type, "myType", StringCompare.OrdinalIgnoreCase) == 0)
{
var myType = AutoMapper.Mapper<MyType>(hit.Source);
}
}
关于c# - 我需要为 Nest Search 创建类型吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24237927/