elasticsearch 正确设置映射ElasticSearch和NEST
我在NEST中的映射有一些问题。
elasticsearch 正确设置映射ElasticSearch和NEST,
elasticsearch,nest,
elasticsearch,Nest,我在NEST中的映射有一些问题。 让我描述一下我的设置 我将ElasticClient设置为ioc容器中的单例,如下所示: ElasticClient client = new ElasticClient(settings); client.CreateIndex("elasticsearch", c => c .AddMapping<ProductDocument
让我描述一下我的设置 我将ElasticClient设置为ioc容器中的单例,如下所示:
ElasticClient client = new ElasticClient(settings);
client.CreateIndex("elasticsearch", c =>
c
.AddMapping<ProductDocument>(m => m.MapFromAttributes())
.AddMapping<PageDocument>(m => m.MapFromAttributes())
.AddMapping<MediaAsset>(ma => ma.MapFromAttributes()));
要索引文档,我只需在属性中输入值并使用
_elasticClient.Index(product);
但是,当我在ElasticSearch中检查映射时,我看到以下内容:
{
"elasticsearch" : {
"mappings" : {
"productdocument" : {
"properties" : {
...
"groupId" : {
"type" : "string"
},
"language" : {
"type" : "string"
}
}
}
}
}
我希望语言和groupId的索引是:“NotAnalyzed”。
我做错了什么?试试下面的类级注释:
[ElasticType(Name = "ContentDocument")]
public class ContentDocument
{
public string Id { get; set; }
[ElasticProperty(Index = FieldIndexOption.NotAnalyzed)]
public string DocumentKey { get; set; }
public string Title { get; set; }
public string Description { get; set; }
[ElasticProperty(Index = FieldIndexOption.NotAnalyzed)]
public string Language { get; set; }
public string Url { get; set; }
}
[ElasticType(Name = "ProductDocument")]
public class ProductDocument : ContentDocument
{
[ElasticProperty(Index = FieldIndexOption.NotAnalyzed)]
public string GroupId { get; set; }
}
嗯,很有趣。您是否在不使用IoC容器的情况下尝试过相同的代码?我试着在一个简单的控制台应用程序中复制你的代码,效果如预期,即在索引映射中将带注释的字段标记为
未分析
。可能会缩小问题的范围。您发布的代码看起来不错。在create index调用之后,您是否获得了成功响应?在这之后,索引的映射是什么样子的?我已经尝试了一下,每次请求IElasticClient时,将ioc容器设置为新建一个,这似乎会减少问题的发生频率。我会尝试一下,看看这是否完全解决了问题。(我有一些客户端访问索引(使用较旧的代码),这可能会改变映射)@coolmcgrr ElasticClient作为单例是安全的
[ElasticType(Name = "ContentDocument")]
public class ContentDocument
{
public string Id { get; set; }
[ElasticProperty(Index = FieldIndexOption.NotAnalyzed)]
public string DocumentKey { get; set; }
public string Title { get; set; }
public string Description { get; set; }
[ElasticProperty(Index = FieldIndexOption.NotAnalyzed)]
public string Language { get; set; }
public string Url { get; set; }
}
[ElasticType(Name = "ProductDocument")]
public class ProductDocument : ContentDocument
{
[ElasticProperty(Index = FieldIndexOption.NotAnalyzed)]
public string GroupId { get; set; }
}