gpt4 book ai didi

elasticsearch - 使用Nest Client在Elasticsearch中将嵌套属性复制到父对象

转载 作者:行者123 更新时间:2023-12-02 22:56:12 25 4
gpt4 key购买 nike

如何在索引映射定义中将嵌套属性复制到包含POCO的字段中?

当两个字段处于同一对象级别时,我可以使用.CopyTo将一个属性成功复制到另一个属性中。

但是,我正在努力将嵌套对象上的属性复制到父对象上的属性。

给定下面的对象,我想将“街道”从“人”的“地址”属性复制到“人”的“搜索”属性中

Person
{
public string Search { get; set; }
public string LastName { get; set; }
public Address Address { get; set; }
}

Address
{
public string Street { get; set; }
}

如下所示,将“姓氏”映射到“搜索”很简单。

.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”

最佳答案

这是你怎么做

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属性
  • 明确映射Address上的Person属性
  • 自动映射Address属性
  • 显式映射Street属性并设置CopyTo(...)。此时,通用类型参数是Address类型,因此您需要使用Nest.Infer.Field<T>来访问SearchPerson属性,或使用字符串。

  • 搜索返回期望的文档
    {
    "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不会包含它们的值。但是,作为属性公开对于强类型字段访问可能很有用。

    关于elasticsearch - 使用Nest Client在Elasticsearch中将嵌套属性复制到父对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54011586/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com