- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试使用 NEST 返回从公共(public)子类派生的各种对象。
在这个例子中,我有一个名为“Person”的基类,然后我有一个名为“Firefighter”和“Teacher”的派生类。实例存储在名为“people”的索引中。
我想搜索我的索引并返回 Firefighters 和 Teachers 的组合,但我能得到的最好结果是 Persons 列表。
The documentation要求使用 ISearchResponse.Types,但我没有看到此函数存在。 This post ,在 StackOverflow 上,也引用了 .Types 函数,但我只是不认为它是一个选项。
这是我要解决的问题的示例。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Nest;
using Elasticsearch;
using Elasticsearch.Net;
namespace ElasticSearchTest
{
class Program
{
static void Main(string[] args)
{
var myTeacher = new Teacher { id="1", firstName = "Steve", lastName = "TheTeacher", gradeNumber = 8 };
var myFiregighter = new Firefighter { id="2", firstName = "Frank", lastName = "TheFirefighter", helmetSize = 12 };
SingleNodeConnectionPool esPool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
ConnectionSettings esSettings = new ConnectionSettings(esPool);
esSettings.DefaultIndex("people");
Nest.ElasticClient esClient = new ElasticClient(esSettings);
esClient.DeleteIndex("people");
esClient.Index<Teacher>(myTeacher);
esClient.Index<Firefighter>(myFiregighter);
System.Threading.Thread.Sleep(2000);
ISearchResponse<Person> response = esClient.Search<Person>(s => s
.AllTypes()
.MatchAll()
);
foreach (Person person in response.Documents)
Console.WriteLine(person);
}
public class Person
{
public string id { get; set; }
public string firstName { get; set; }
public string lastName { get; set; }
public override string ToString() { return String.Format("{0}, {1}", lastName, firstName);}
}
public class Firefighter : Person
{
public int helmetSize { get; set; }
}
public class Teacher : Person {
public int gradeNumber { get; set; }
}
}
}
数据在 Elasticsearch 中的结构似乎正确:
{
took: 1,
timed_out: false,
_shards: {
total: 5,
successful: 5,
failed: 0
},
hits: {
total: 2,
max_score: 1,
hits: [{
_index: "people",
_type: "firefighter",
_id: "2",
_score: 1,
_source: {
helmetSize: 12,
id: "2",
firstName: "Frank",
lastName: "TheFirefighter"
}
}, {
_index: "people",
_type: "teacher",
_id: "1",
_score: 1,
_source: {
gradeNumber: 8,
id: "1",
firstName: "Steve",
lastName: "TheTeacher"
}
}]
}
}
如何让 NEST 返回从基类派生的实例的混合列表?
非常感谢!
-Z
最佳答案
NEST supports Covariant results在 NEST 2.x 中,方法的命名在 ISearchResponse<T>
上改为.Type()
在向 Elasticsearch 发出请求时与它在 URI 中表示的路由参数对齐(.Type()
可以是一种或多种类型)。
NEST 2.x 的协变结果示例是
static void Main(string[] args)
{
var myTeacher = new Teacher { id = "1", firstName = "Steve", lastName = "TheTeacher", gradeNumber = 8 };
var myFiregighter = new Firefighter { id = "2", firstName = "Frank", lastName = "TheFirefighter", helmetSize = 12 };
SingleNodeConnectionPool esPool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
ConnectionSettings esSettings = new ConnectionSettings(esPool);
esSettings.DefaultIndex("people");
Nest.ElasticClient esClient = new ElasticClient(esSettings);
if (esClient.IndexExists("people").Exists)
{
esClient.DeleteIndex("people");
}
esClient.Index<Teacher>(myTeacher, i => i.Refresh());
esClient.Index<Firefighter>(myFiregighter, i => i.Refresh());
// perform the search using the base type i.e. Person
ISearchResponse<Person> response = esClient.Search<Person>(s => s
// Use .Type to specify the derived types that we expect to return
.Type(Types.Type(typeof(Teacher), typeof(Firefighter)))
.MatchAll()
);
foreach (Person person in response.Documents)
Console.WriteLine(person);
}
public class Person
{
public string id { get; set; }
public string firstName { get; set; }
public string lastName { get; set; }
public override string ToString() { return String.Format("{0}, {1}", lastName, firstName); }
}
public class Firefighter : Person
{
public int helmetSize { get; set; }
}
public class Teacher : Person
{
public int gradeNumber { get; set; }
}
关于c# - 使用 NEST 和 .NET 从 Elasticsearch 返回多个派生类实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35994649/
在查看Thermostat Guide时,我没有提到湿度读数。这些数据可以通过API获得吗?会在以后的版本中吗? 最佳答案 从NestAPI v1.1开始,似乎湿度数据可以作为只读字段使用。 可以在v
我从以下位置获取了授权码 https://home.nest.com/login/oauth2?client_id=CLIENT_ID&state=FOO . 我想获取access_token,但是出
使用1.1.2版本的Nest,似乎至少有3种索引多个文档的方法: IndexMany client.IndexMany(documents, "index_name", "type_name"); 使
我有2个POJO(人和车),其中一个由另一个人引用 @Document(indexName = "person", type = "user") public class Person { @
Nest Protect(烟雾和一氧化碳警报)可以测量温度和湿度,但官方或非官方 API 似乎都不允许访问这些数据。有人在工作吗?任何视线访问这些数据?建议?谢谢! 最佳答案 $infos = (o
自从我得到 Nest 以来,我所想要的就是能够在很长一段时间内汇总我的能量使用情况以寻找模式,主要是满足我的好奇心。当然有每月使用报告,而且网络和设备只能追溯到 10 天。 API 是否公开了这些数据
我在获取和更改 Nest 恒温器提供的一些基本数据时遇到了问题。 使用命令行,我如何获取或更改恒温器上的个别设置或值? 最佳答案 这是几个用户的汇编,解释了如何检索或更改一些基本信息,并添加了我自己的
我和我的一位同事就以下最佳实践问题进行了讨论。 大多数函数/方法都从一些参数检查开始。 我提倡以下风格,避免嵌套。 if (parameter one is ugly) return ERROR; i
我有 4 个表: ChatRooms Participants Messages Users 聊天室有很多参与者 聊天室有很多消息 用户有很多消息 用户有很多参与者 参与者属于用户 消息属于用户 我正
我编写了以下函数并得到如下所示的响应: public async Task ShowMapping(string indexname) { var resu
我正在使用 NEST 为我的对象编制索引,但在序列化时遇到了 Newtonsoft 错误。我的一个对象有一个自引用循环。有没有一种方法可以让我访问 JsonSerializer 并更改它处理自引用的方
设置目标温度会引发如下错误。我正在使用 php curl 来设置温度。任何帮助表示赞赏。 object(stdClass)#72 (2) { ["cmd"]=> stri
我编写了以下函数并得到如下所示的响应: public async Task ShowMapping(string indexname) { var resu
我正在使用 Nest Developer https://developer.nest.com/用于创建 iOS 应用程序的 API。有谁知道如何将 Firebase 框架链接到我的应用程序中?我不断
我是名为 Feather (featherapp.co) 的综合性 Apple TV 应用程序的开发者。我遇到的一个问题是用户希望能够查看他们相机的实时画面。 有没有人确定访问摄像头实时画面的方法?我
免责声明 - 我担心这个问题可能是重复的,因为功能看起来很基本,所以我知道我可能会得到一个骗局。但我找不到有效的解决方案 所以我有一个可观察的数组设置如下 self.meetingAttendees
我正试着给我的模块提供一个令牌。该模块如下所示:。我将AuthGuardModule导入到的父模块如下所示:。而gardConfigProvider本身如下所示:。但我得到一个错误:Error[Exc
我正试着给我的模块提供一个令牌。该模块如下所示:。我将AuthGuardModule导入到的父模块如下所示:。而gardConfigProvider本身如下所示:。但我得到一个错误:Error[Exc
每 10 秒使用一次访问 token 时遇到太多请求错误。在重新阅读文档时,我意识到这是一个禁忌(“为了避免错误,我们建议您以每分钟一次或更短的速度提出请求”)。 有谁知道我会被限制多久? 最佳答案
我使用嵌套 plist 来创建对象结构(CLOS 类型),将嵌套的对象传递给它的部分。我想以迭代方式 append 嵌套的 plist,但因此我想在时间和内存方面高效地完成它。 以下示例显示了由于一次
我是一名优秀的程序员,十分优秀!