如何强制 NEST 使用属性映射
How to force NEST to use attribute mappings
好吧,如果这是一个愚蠢的问题,我很抱歉,但我花了一天时间浏览文档并尝试了 3 个不同的 NEST 版本,最终结果是一样的。
基本上,当我使用 Elasticsearch 的 REST api 创建类型的映射时,我可以在我的映射上使用 GET 请求,我收到的正是我想要的:
"properties": {
"date": {
"type": "date",
"format": "basic_date"
},
"id": {
"type": "long",
"index": "no"
},
"name": {
"type": "string"
},
"slug": {
"type": "string",
"index": "no"
}
}
但是,如果我从头开始,并在 c# 中使用以下 class:
[Number(Index = NonStringIndexOption.No)]
public long Id { get; set; }
[String(Name = "name")]
public string Name { get; set; }
[String(Name = "slug", Index = FieldIndexOption.No)]
public string Slug { get; set; }
[Date(Format = "dd-MM-yyyy", Index = NonStringIndexOption.No)]
public DateTime Date { get; set; }
并创建并填充索引:
node = new Uri("http://localhost:9200");
settings = new ConnectionSettings(node);
settings.DefaultIndex("searchable-items");
//retrieve stuff from relational db
client.IndexMany(allItemsRetrievedFromRelDb);
我的类型默认如下(基本上忽略所有属性值,除了 Name=)
"date": {
"type": "date",
"format": "strict_date_optional_time||epoch_millis"
},
"id": {
"type": "long"
},
"name": {
"type": "string"
},
"slug": {
"type": "string"
}
基本上我希望达到的是:
- "basic_date"
类型的日期格式
- 只应分析和搜索 "name"
- 最终,"name"
的自定义分析器
我的问题是 - 我做错了什么,为什么 NEST 无视我在属性中输入的内容?当前代码是 v2.4.4,尽管我也尝试了 5.0.0 预发布版(那里的语法略有不同,但结果相同)。
为了使属性映射生效,您需要在使用 .CreateIndex()
创建索引时或在创建索引之后和索引任何文档之前将映射告知 Elasticsearch , .Map()
.
下面是使用 NEST 2.4.4 进行演示的示例
void Main()
{
var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
var defaultIndex = "searchable-items";
var connectionSettings = new ConnectionSettings(pool)
// specify the default index to use if
// 1. no index is specified on the request
// 2. no index can be inferred from the C# POCO type
.DefaultIndex(defaultIndex);;
var client = new ElasticClient(connectionSettings);
client.CreateIndex(defaultIndex, c => c
.Mappings(m => m
.Map<MyDocument>(mm => mm
// map MyDocument, letting
// NEST infer the mapping from the property types
// and attributes applied to them
.AutoMap()
)
)
);
var docs = new[] {
new MyDocument
{
Id = 1,
Name = "name 1",
Date = new DateTime(2016,08,26),
Slug = "/slug1"
},
new MyDocument
{
Id = 2,
Name = "name 2",
Date = new DateTime(2016,08,27),
Slug = "/slug2"
}
};
client.IndexMany(docs);
}
public class MyDocument
{
[Number(Index = NonStringIndexOption.No)]
public long Id { get; set; }
[String(Name = "name")]
public string Name { get; set; }
[String(Name = "slug", Index = FieldIndexOption.No)]
public string Slug { get; set; }
[Date(Format = "dd-MM-yyyy", Index = NonStringIndexOption.No)]
public DateTime Date { get; set; }
}
The Automapping documentation has more details on how you can control the mapping for a POCO type using the fluent API and the visitor pattern (for applying conventions).
好吧,如果这是一个愚蠢的问题,我很抱歉,但我花了一天时间浏览文档并尝试了 3 个不同的 NEST 版本,最终结果是一样的。
基本上,当我使用 Elasticsearch 的 REST api 创建类型的映射时,我可以在我的映射上使用 GET 请求,我收到的正是我想要的:
"properties": {
"date": {
"type": "date",
"format": "basic_date"
},
"id": {
"type": "long",
"index": "no"
},
"name": {
"type": "string"
},
"slug": {
"type": "string",
"index": "no"
}
}
但是,如果我从头开始,并在 c# 中使用以下 class:
[Number(Index = NonStringIndexOption.No)]
public long Id { get; set; }
[String(Name = "name")]
public string Name { get; set; }
[String(Name = "slug", Index = FieldIndexOption.No)]
public string Slug { get; set; }
[Date(Format = "dd-MM-yyyy", Index = NonStringIndexOption.No)]
public DateTime Date { get; set; }
并创建并填充索引:
node = new Uri("http://localhost:9200");
settings = new ConnectionSettings(node);
settings.DefaultIndex("searchable-items");
//retrieve stuff from relational db
client.IndexMany(allItemsRetrievedFromRelDb);
我的类型默认如下(基本上忽略所有属性值,除了 Name=)
"date": {
"type": "date",
"format": "strict_date_optional_time||epoch_millis"
},
"id": {
"type": "long"
},
"name": {
"type": "string"
},
"slug": {
"type": "string"
}
基本上我希望达到的是:
- "basic_date" 类型的日期格式
- 只应分析和搜索 "name"
- 最终,"name" 的自定义分析器
我的问题是 - 我做错了什么,为什么 NEST 无视我在属性中输入的内容?当前代码是 v2.4.4,尽管我也尝试了 5.0.0 预发布版(那里的语法略有不同,但结果相同)。
为了使属性映射生效,您需要在使用 .CreateIndex()
创建索引时或在创建索引之后和索引任何文档之前将映射告知 Elasticsearch , .Map()
.
下面是使用 NEST 2.4.4 进行演示的示例
void Main()
{
var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
var defaultIndex = "searchable-items";
var connectionSettings = new ConnectionSettings(pool)
// specify the default index to use if
// 1. no index is specified on the request
// 2. no index can be inferred from the C# POCO type
.DefaultIndex(defaultIndex);;
var client = new ElasticClient(connectionSettings);
client.CreateIndex(defaultIndex, c => c
.Mappings(m => m
.Map<MyDocument>(mm => mm
// map MyDocument, letting
// NEST infer the mapping from the property types
// and attributes applied to them
.AutoMap()
)
)
);
var docs = new[] {
new MyDocument
{
Id = 1,
Name = "name 1",
Date = new DateTime(2016,08,26),
Slug = "/slug1"
},
new MyDocument
{
Id = 2,
Name = "name 2",
Date = new DateTime(2016,08,27),
Slug = "/slug2"
}
};
client.IndexMany(docs);
}
public class MyDocument
{
[Number(Index = NonStringIndexOption.No)]
public long Id { get; set; }
[String(Name = "name")]
public string Name { get; set; }
[String(Name = "slug", Index = FieldIndexOption.No)]
public string Slug { get; set; }
[Date(Format = "dd-MM-yyyy", Index = NonStringIndexOption.No)]
public DateTime Date { get; set; }
}
The Automapping documentation has more details on how you can control the mapping for a POCO type using the fluent API and the visitor pattern (for applying conventions).