Ole*_*our 5 .net elasticsearch
我正在使用 .NET nuget 包“Elastic.Clients.Elasticsearch”(版本 8)并尝试基于以下模型创建索引映射。如何映射 Employee 成员及其 JobRole 成员?我尝试使用“对象”和“嵌套”,但没有运气。
此外,如何排除属性被索引?属性映射如:
[Text(Name = "last_name")]
Run Code Online (Sandbox Code Playgroud)
...版本 8 不再支持。唯一的选项是“流畅映射”。
不幸的是,只有版本 7 可用的文档, https://www.elastic.co/guide/en/elasticsearch/client/net-api/7.17/ Fluent-mapping.html
public class Company
{
public string CompanyName { get; set; }
public Employee EmployeeInfo { get; set; }
}
public class Employee
{
public string EmployeeName { get; set; }
public JobRole[] JobRoles { get; set; }
}
public class JobRole
{
public string RoleName { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这是我的代码,正如你所看到的,我中途迷路了..
var createIndexResponse = client.Indices.Create<Company>("myindex", c => c
.Mappings(m => m
.Properties(p => p
.Keyword(s => s.CompanyName)
.Object<Employee> (x=>x.EmployeeInfo.EmployeeName // Got lost here...
)
)
);
Run Code Online (Sandbox Code Playgroud)
任何人?
创建新属性时,它请求的 IProperty 是您分配给字段的属性类型。如 TextProperty、ObjectProperty、NestedProperty、LongProperty:
var response = await _client.Indices.CreateAsync<Company>("myindex", c => c
.Mappings(map => map
.Properties(p => p
.Text(t => t.CompanyName)
.Object(o => o.EmployeeInfo, objConfig => objConfig
.Properties(p => p
.Text(t => t.EmployeeInfo.EmployeeName)
.Object(n => n.EmployeeInfo.JobRoles, objConfig => objConfig
.Properties(p => p
.Text(t => t.EmployeeInfo.JobRoles.First().RoleName)
)
)
)
)
)
));
Run Code Online (Sandbox Code Playgroud)
ObjectProperty jobRolesProperty = new ();
jobRolesProperty.Properties.Add("roleName", new TextProperty());
var response = await _client.Indices.CreateAsync<Company>("myindex", c => c
.Mappings(map => map
.Properties(
new Properties<Company>()
{
{ "companyName", new TextProperty() },
{ "employeeInfo", new ObjectProperty()
{
Properties = new Properties<Employee>()
{
{ "employeeName", new TextProperty() },
{ "jobRoles", jobRolesProperty }
}
}
}
}
)
)
);
Run Code Online (Sandbox Code Playgroud)
//Another syntax usage
ObjectProperty jobRolesProperty = new ();
jobRolesProperty.Properties.Add("roleName", new TextProperty());
var response = await _client.Indices.CreateAsync<Company>("myindex", c => c
.Mappings(map => map
.Properties(p => p
.Text(t => t.CompanyName)
.Object(o => o.EmployeeInfo, objConfig => objConfig
.Properties(
new Properties<Employee>()
{
{ "employeeName", new TextProperty() },
{ "jobRoles", jobRolesProperty }
}
)
)
)
));
Run Code Online (Sandbox Code Playgroud)
创建的映射:
{
"mappings": {
"properties": {
"companyName": {
"type": "text"
},
"employeeInfo": {
"properties": {
"employeeName": {
"type": "text"
},
"jobRoles": {
"properties": {
"roleName": {
"type": "text"
}
},
"type": "object"
}
},
"type": "object"
}
}
}
Run Code Online (Sandbox Code Playgroud)
}
要管理设置,您可以检查此答案: Elastic Search 8 Defining Analyticsrs