使用Nest Client在Elasticsearch中将嵌套属性复制到父对象 [英] Copying a nested property to parent object in elasticsearch with Nest client
问题描述
如何在索引映射定义中将嵌套属性复制到包含POCO的字段中?
How can I copy a nested property into a field of the containing POCO in an index mapping definition?
当两个字段处于同一对象级别时,我可以使用.CopyTo
成功地将一个属性复制到另一个属性中.
I am able to successfully copy a property into another property with .CopyTo
when both fields are at the same object level.
但是我正在努力将嵌套对象的属性复制到父对象的属性.
However I am struggling with copying a property on a nested object into a property on the parent object.
鉴于以下对象,我想将街道"从人"的地址"属性复制到人"的搜索"属性
Given the objects below, I would like to copy 'Street' from the Address property on a Person into the 'Search' property on Person
Person
{
public string Search { get; set; }
public string LastName { get; set; }
public Address Address { get; set; }
}
Address
{
public string Street { get; set; }
}
将姓氏"映射到搜索"很简单,如下所示.
Mapping 'LastName' into 'Search' is simple, as below.
.Map<Person>(map => map
.AutoMap()
.Properties(properties => properties
.Text(text =>
text.Name(name => name.LastName)
.CopyTo(copyTo =>
copyTo.Field(field => field.Search)
)
)
)
但是我无法弄清楚要复制的Nest语法 将"Person.Address.Street"转换为"Person.Search"
However I cannot figure out the Nest syntax to copy 'Person.Address.Street' into 'Person.Search'
推荐答案
这是您的操作方法
private static void Main()
{
var defaultIndex = "my_index";
var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
var settings = new ConnectionSettings(pool)
.DefaultIndex(defaultIndex);
var client = new ElasticClient(settings);
if (client.IndexExists(defaultIndex).Exists)
client.DeleteIndex(defaultIndex);
var createIndexResponse = client.CreateIndex(defaultIndex, c => c
.Settings(s => s
.NumberOfShards(1)
.NumberOfReplicas(0)
)
.Mappings(m => m
.Map<Person>(mm => mm
.AutoMap()
.Properties(p => p
.Object<Address>(o => o
.Name(n => n.Address)
.AutoMap()
.Properties(pp => pp
.Text(t => t
.Name(nn => nn.Street)
.CopyTo(co => co
.Field(Infer.Field<Person>(ff => ff.Search))
)
)
)
)
)
)
)
);
var indexResponse = client.Index(new Person
{
LastName = "foo",
Address = new Address
{
Street = "bar"
}
} , i => i
.Refresh(Refresh.WaitFor)
);
var searchResponse = client.Search<Person>(s => s
.Query(q => q
.Match(m => m
.Field(f => f.Search)
.Query("bar")
)
)
);
}
public class Person
{
public string Search { get; set; }
public string LastName { get; set; }
public Address Address { get; set; }
}
public class Address
{
public string Street { get; set; }
}
本质上
- 自动映射
Person
属性 - 明确映射
Person
上的Address
属性 - 自动映射
Address
属性 - 明确映射
Street
属性并设置CopyTo(...)
.此时,泛型类型参数为Address
类型,因此您需要使用Nest.Infer.Field<T>
来访问Person
的Search
属性,或使用字符串.
- Automap the
Person
properties - Explicitly map the
Address
property onPerson
- Automap the
Address
properties - Explicitly map the
Street
property and set upCopyTo(...)
. At this point, the generic type parameter is theAddress
type, so you either need to useNest.Infer.Field<T>
to access theSearch
property ofPerson
, or use a string.
搜索返回预期的文档
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 0.2876821,
"hits" : [
{
"_index" : "my_index",
"_type" : "person",
"_id" : "5tQDEWgBrKRHlz9qAve8",
"_score" : 0.2876821,
"_source" : {
"lastName" : "foo",
"address" : {
"street" : "bar"
}
}
}
]
}
}
Elasticsearch中的
copy_to
字段不一定需要作为C#POCO上的属性公开,因为_source
不会包含它们的值.但是,作为属性公开可能对于强类型字段访问很有用.
copy_to
fields in Elasticsearch don't necessarily need to be exposed as a property on the C# POCO, since the _source
won't contain a value for them. Exposing as a property however may be useful for strong typing field access.
这篇关于使用Nest Client在Elasticsearch中将嵌套属性复制到父对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!