简体   繁体   English

我需要为嵌套搜索创建类型吗?

[英]Do I need to create a Type for Nest Search?

I see examples like: 我看到类似的例子:

var result = this._client.Search(s => s
.Index("my-index")
.Type("my-type")
 .Query(q=> ....)
 .Filter(f=> ....)         
);

But when I use this I get: 但是当我使用它时,我得到:

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.   

I have lots of different types and I don't want to create classes for all of them. 我有很多不同的类型,我不想为所有这些都创建类。 Can I use NEST and it's search without types like Search<MyType> ? 我可以使用NEST进行搜索,而不使用Search<MyType>类的类型吗?

Thanks 谢谢

I have successfully used Search<dynamic> to avoid needing to be dependent upon a specific type. 我已经成功使用Search<dynamic>来避免依赖于特定类型。 Then when I get my results back I can examine them and cast/convert into a specific POCO if desired. 然后,当我取回结果时,可以检查它们并将其转换/转换为特定的POCO(如果需要)。

I use something like the following: 我使用如下内容:

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);
     }
}

There is one more solution. 还有另一种解决方案。 NEST 5.x and less uses Newtonsoft.Json for JSON serialization. NEST 5.x及以下版本使用Newtonsoft.Json进行JSON序列化。 So you can use JObject instead of dynamic . 因此,您可以使用JObject而不是dynamic

var result = client.Search<JObject>(s => s
    .Index("music")
    .AllTypes()
    .Query(q => ...)
    .Filter(f => ...)
);

This is very useful because you can convert it now to any object using Newtonsoft.Json functionality. 这非常有用,因为您现在可以使用Newtonsoft.Json功能将其转换为任何对象。

private static Dictionary<string, Type> TypeMapping = new Dictionary<string, Type>
{
    { "artist", typeof(Artist) },
    { "song", typeof(Song) }
};

...

foreach (var hit in result.Hits)
{
    var type = TypeMapping[hit.Type];
    var result = hit.Source.ToObject(type);
}

NEST 6.x ships with a shaded Json.NET dependency. NEST 6.x附带有阴影的Json.NET依赖项。 So in order to use Newtonsoft.Json for this version you need to install NEST.JsonNetSerializer package and specify JsonNetSerializer.Default . 因此,要在此版本中使用Newtonsoft.Json,您需要安装NEST.JsonNetSerializer软件包并指定JsonNetSerializer.Default

var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
var connectionSettings =
    new ConnectionSettings(pool, sourceSerializer: JsonNetSerializer.Default);
var client = new ElasticClient(connectionSettings);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 试图创建一个搜索,我需要动态地做一个orelse - trying to create a search and I need to do an orelse dynamically 如何在Elasticsearch NEST中序列化JToken或JObject类型的属性? - How do I serialize properties of type JToken or JObject in Elasticsearch NEST? 无法创建搜索响应对象-我们如何在Nest中模拟搜索响应? - Unable to create a search response object - How do we mock search responses in Nest? 为 Elastic Search NEST 客户端创建抽象层。 返回类型应该是什么? - Create Abstraction layer for Elastic Search NEST client. What should be the return type? 使用NEST通过Elastic搜索对多种类型进行排序 - Sorting with multiple type with Elastic search using NEST 如何在Elastic Search中使用Nest创建特定属性的索引? - How can i create index on specific property using Nest in Elastic Search? 我需要创建什么类型才能将 json 键值对从 .Net 传递回 angular - What type do I need to create to pass a json key value pair back to angular from .Net 在ElasticSearch上使用NEST - 如何使用部分填充的对象作为搜索条件 - Using NEST on ElasticSearch - How do i use a partially populated object as a search criteria 如何使用NEST构造来自多个类型的字段且没有魔术字符串的ElasticSearch搜索 - How do I construct an ElasticSearch search using NEST with fields from multiple types without magic strings 我如何使用Nest API 5在Elasticsearch中进行重新索引以从Elastic Search 1.4迁移到ElasticSearch 5 - How can i do reindexing in elasticsearch to migrate from elastic search 1.4 to elasticsearch 5 using Nest api 5
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM