C# 是否需要为嵌套搜索创建类型?
我看到的例子如下:C# 是否需要为嵌套搜索创建类型?,c#,database,elasticsearch,nest,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.Search
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.ElasticClient.Search(System.Func)”的类型参数。尝试显式指定类型参数。
我有很多不同的类型,我不想为所有类型创建类。我可以使用NEST及其搜索而不使用搜索
谢谢我成功地使用了
搜索
来避免依赖特定类型。然后,当我得到我的结果时,我可以检查它们,并根据需要转换成特定的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);
}
}
var result=client.Search(s=>s
.索引(“myIndex”)
.AllTypes()
.Query(q=>…)
.Filter(f=>…)
);
foreach(var命中结果.Hits.Hits)
{
//选中hit.Type,然后将hit.Source映射到相应的POCO中。
//我使用AutoMapper进行映射,但您可以做任何适合您的事情。
if(string.Compare(hit.Type,“myType”,StringCompare.OrdinalIgnoreCase)==0)
{
var myType=AutoMapper.Mapper(hit.Source);
}
}
我成功地使用了搜索
,以避免依赖特定类型。然后,当我得到我的结果时,我可以检查它们,并根据需要转换成特定的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);
}
}
var result=client.Search(s=>s
.索引(“myIndex”)
.AllTypes()
.Query(q=>…)
.Filter(f=>…)
);
foreach(var命中结果.Hits.Hits)
{
//选中hit.Type,然后将hit.Source映射到相应的POCO中。
//我使用AutoMapper进行映射,但您可以做任何适合您的事情。
if(string.Compare(hit.Type,“myType”,StringCompare.OrdinalIgnoreCase)==0)
{
var myType=AutoMapper.Mapper(hit.Source);
}
}
还有一个解决方案。NEST 5.x及更低版本使用Newtonsoft.Json进行Json序列化。因此,您可以使用JObject
而不是dynamic
var result = client.Search<JObject>(s => s
.Index("music")
.AllTypes()
.Query(q => ...)
.Filter(f => ...)
);
还有一个解决办法。NEST 5.x及更低版本使用Newtonsoft.Json进行Json序列化。因此,您可以使用
JObject
而不是dynamic
var result = client.Search<JObject>(s => s
.Index("music")
.AllTypes()
.Query(q => ...)
.Filter(f => ...)
);
试过但需要一个例子:(编辑答案以包含一个例子。试过但需要一个例子:(编辑答案以包含一个例子)。