gpt4 book ai didi

elasticsearch - 将Copy_To的属性/流畅映射与Nest结合起来

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

我想在Nest中使用copy_to功能。我读过我需要使用流畅的映射(Elasticsearch Nest and CopyTo)。

是否可以使用基于属性的映射,然后在其上进行流畅的映射以添加copy_to?如果是这样,有什么例子吗?我很难找到答案。

我要复制到的字段在我的模型类中不存在。我只想在elasticsearch中进行搜索。

[ElasticType(IdProperty = "CustomerId", Name = "customer_search")]
public class CustomerSearchResult : BindableBase
{
[ElasticProperty(Name = "customer_id", Index = FieldIndexOption.NotAnalyzed, Type = FieldType.String)]
public int CustomerId { get; set; }
[ElasticProperty(Name = "account_type", Index = FieldIndexOption.NotAnalyzed, Type = FieldType.String)]
public string AccountType { get; set; }
[ElasticProperty(Name = "short_name", Index = FieldIndexOption.NotAnalyzed, Type = FieldType.String)]
public string ShortName { get; set; }
[ElasticProperty(Name = "legacy_name", Index = FieldIndexOption.NotAnalyzed, Type = FieldType.String)]
public string LegacyName { get; set; }
[ElasticProperty(Name = "legacy_contact_name", Index = FieldIndexOption.NotAnalyzed, Type = FieldType.String)]
public string LegacyContactName { get; set; }
[ElasticProperty(Name = "city", Index = FieldIndexOption.NotAnalyzed, Type = FieldType.String)]
public string City { get; set; }
[ElasticProperty(Name = "state_abbreviation", Index = FieldIndexOption.NotAnalyzed, Type = FieldType.String)]
public string StateAbbreviation { get; set; }
[ElasticProperty(Name = "country", Index = FieldIndexOption.NotAnalyzed, Type = FieldType.String)]
public string Country { get; set; }
[ElasticProperty(Name = "postal_code", Index = FieldIndexOption.NotAnalyzed, Type = FieldType.String)]
public string PostalCode { get; set; }
}

在上面的类中,我想使用ShortName,LegacyName和LegacyContactName以及copy_to一个名为“search”的字段,这将是一个已分析的字段。

最佳答案

像下面这样的事情

void Main()
{
var settings = new ConnectionSettings(new Uri("http://localhost:9200"));
var connection = new InMemoryConnection(settings);
var client = new ElasticClient(connection: connection);


var indexResponse = client.CreateIndex("customer_searches", c => c
.AddMapping<CustomerSearchResult>(m => m
.MapFromAttributes()
.Properties(p => p
.String(s => s.Name("short_name").CopyTo("search").Index(FieldIndexOption.NotAnalyzed))
.String(s => s.Name("legacy_name").CopyTo("search").Index(FieldIndexOption.NotAnalyzed))
.String(s => s.Name("legacy_contact_name").CopyTo("search").Index(FieldIndexOption.NotAnalyzed))
.String(s => s.Name("search").Index(FieldIndexOption.Analyzed))
)
)
);

Console.WriteLine(Encoding.UTF8.GetString(indexResponse.RequestInformation.Request));
}

哪个输出
{
"settings": {
"index": {}
},
"mappings": {
"customer_search": {
"properties": {
"customer_id": {
"index": "not_analyzed",
"type": "string"
},
"account_type": {
"index": "not_analyzed",
"type": "string"
},
"short_name": {
"index": "not_analyzed",
"copy_to": [
"search"
],
"type": "string"
},
"legacy_name": {
"index": "not_analyzed",
"copy_to": [
"search"
],
"type": "string"
},
"legacy_contact_name": {
"index": "not_analyzed",
"copy_to": [
"search"
],
"type": "string"
},
"city": {
"index": "not_analyzed",
"type": "string"
},
"state_abbreviation": {
"index": "not_analyzed",
"type": "string"
},
"country": {
"index": "not_analyzed",
"type": "string"
},
"postal_code": {
"index": "not_analyzed",
"type": "string"
},
"search": {
"index": "analyzed",
"type": "string"
}
}
}
}
}

Properties()的调用将覆盖默认约定和属性映射,因此您需要指定字段也为 not_analyzed

关于elasticsearch - 将Copy_To的属性/流畅映射与Nest结合起来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33851566/

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