使用 NEST 和 .NET 从 Elasticsearch 返回多个派生 class 实例
Returning multiple derived class instances from Elasticsearch using NEST and .NET
我正在尝试使用 NEST return 从公共子 class 派生的各种对象。
在这个例子中,我有一个名为 "Person" 的基础 class,然后我派生了 classes 称为 "Firefighter" 和 "Teacher"。实例存储在名为 "people".
的索引中
我想搜索我的索引和 return 消防员和教师的组合,但我能得到的最好结果是人员列表。
The documentation calls for the use of ISearchResponse.Types, but I don't see that this function exists. This post,在 Whosebug 上,也引用了 .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 获取到 return 从基础 class 派生的混合实例列表?
非常感谢!
-Z
NEST supports Covariant results 和在 NEST 2.x 中,ISearchResponse<T>
上的方法命名已更改为 .Type()
以与它在 URI 中表示的路由参数保持一致向 Elasticsearch 发出请求时(.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; }
}
我正在尝试使用 NEST return 从公共子 class 派生的各种对象。
在这个例子中,我有一个名为 "Person" 的基础 class,然后我派生了 classes 称为 "Firefighter" 和 "Teacher"。实例存储在名为 "people".
的索引中我想搜索我的索引和 return 消防员和教师的组合,但我能得到的最好结果是人员列表。
The documentation calls for the use of ISearchResponse.Types, but I don't see that this function exists. This post,在 Whosebug 上,也引用了 .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 获取到 return 从基础 class 派生的混合实例列表?
非常感谢!
-Z
NEST supports Covariant results 和在 NEST 2.x 中,ISearchResponse<T>
上的方法命名已更改为 .Type()
以与它在 URI 中表示的路由参数保持一致向 Elasticsearch 发出请求时(.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; }
}