Xel*_*lom 1 c# database elasticsearch nest
我看到如下例子:
var result = this._client.Search(s => s
.Index("my-index")
.Type("my-type")
.Query(q=> ....)
.Filter(f=> ....)
);
Run Code Online (Sandbox Code Playgroud)
但是当我使用它时,我得到:
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.
Run Code Online (Sandbox Code Playgroud)
我有很多不同的类型,我不想为所有类型创建类.我可以使用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);
}
}
Run Code Online (Sandbox Code Playgroud)