c# - 我需要为 Nest Search 创建类型吗?

标签 c# database elasticsearch nest

我看到这样的例子:

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/

相关文章:

php - 为什么我的 PHP 代码在运行时返回空白页?

elasticsearch - 用于Elasticsearch的自定义插件以更改默认相关性

Elasticsearch聚合顺序与搜索结果相同

c# - 如何停止在整数除法时抛出 OverflowException?

c# - 如何使用线程模拟 Task.Wait

mysql - 查询有什么问题吗?错误语法错误?按无工作分组

java - 如何知道从另一个框架中选择了哪一行?

elasticsearch - Elasticsearch-HTTP设置如何在Kubernetes上进行设置

c# - OutOfMemoryException - 如何发现内存泄漏?

c# - 未处理的异常未被 `Application.ThreadException` 捕获?