我正在使用Elasticsearch 2.3 - Nest API来搜索数据。我正在使用文档的属性映射。我想知道如何使用属性映射来使用Phonetic Analyzer。
文档类:
[ElasticsearchType(Name = "contact", IdProperty = nameof(EntityId))]
public class ESContactSearchDocument
{
[String(Name = "entityid")]
public string EntityId { get; set; }
[String(Name = "id")]
public string Id { get; set; }
[String(Name = "name")]
public string Name { get; set; }
[String(Name = "domain")]
public string Domain { get; set; }
[String(Name = "description")]
public string Description { get; set; }
[String(Name = "email")]
public string Email { get; set; }
[String(Name = "firstname", Analyzer = "phonetic")]
public string FirstName { get; set; }
[String(Name = "lastname", Analyzer = "phonetic")]
public string LastName { get; set; }
}
索引创建和插入:
var createIndexResponse = serviceClient.CreateIndex(indexName, c => c.Mappings(m => m.Map<ESContactSearchDocument>(d => d.AutoMap())));
var indexManyResponse = await serviceClient.IndexManyAsync(ESMapper.Map<TDocument, ESContactSearchDocument>(documentsBatch), indexName, typeName);
ESMapper仅用于从一种类型转换为另一种类型。
结果映射:
{
"contact-uklwcl072": {
"mappings": {
"contact": {
"properties": {
"description": {
"type": "string"
},
"domain": {
"type": "string"
},
"email": {
"type": "string"
},
"entityid": {
"type": "string"
},
"firstname": {
"type": "string"
},
"id": {
"type": "string"
},
"lastname": {
"type": "string"
},
"name": {
"type": "string"
}
}
}
}
}
}
答案 0 :(得分:0)
Take a look at the automapping documentation;您需要将分析器的名称分配给属性
上的Analyzer
属性
public class Document
{
[String(Analyzer="phonetic")]
public string Name {get; set;}
}
然后在映射文档类型时调用AutoMap()
,例如当你创建索引
client.CreateIndex("index-name", c => c
.Mappings(m => m
.Map<Document>(d => d
.AutoMap()
)
)
);
产量
{
"mappings": {
"document": {
"properties": {
"name": {
"type": "string",
"analyzer": "phonetic"
}
}
}
}
}