Elastic search Nest C# - 是否可以通过自动映射对字符串字段进行不区分大小写的排序
Elastic search Nest C# - Is it possible a case insensitive sort on a string field through automap
我有一个elasticsearch嵌套模型class
public class Data
{
public long Id { get; set; }
public string DeviceCode { get; set; }
}
当使用 automap 索引此文档时,nest 将为 属性 DeviceCode 创建两个字段 - DeviceCode 和 DeviceCode.keyword。我想基于 DeviceCode.keyword 字段进行排序,不区分大小写 - 是否可以通过自动映射实现这一点(通过在模型 class 中使用属性映射应用分析器或过滤器)。我想在弹性搜索中保留文本和关键字字段。
问候
葡萄藤
可以将 normalizer 应用于 keyword
字段以在索引时将值小写以进行不区分大小写的查询和聚合
var client = new ElasticClient(settings);
var createIndexResponse = client.Indices.Create("my-data", c => c
.Settings(s => s
.Analysis(a => a
.Normalizers(n => n
.Custom("lowercase", cn => cn
.Filters("lowercase")
)
)
)
)
.Map<Data>(m => m
.AutoMap()
.Properties(p => p
.Text(t => t
.Name(n => n.DeviceCode)
.Fields(f => f
.Keyword(k => k
.Name("keyword")
.IgnoreAbove(256)
)
.Keyword(k => k
.Name("keyword_lowercase")
.Normalizer("lowercase")
.IgnoreAbove(256)
)
)
)
)
)
);
然后使用keyword_lowercase
multi-field
var searchResponse = client.Search<Data>(s => s
.Index("my-data")
.Sort(so => so
.Ascending(f => f.DeviceCode.Suffix("keyword_lowercase"))
)
);
我有一个elasticsearch嵌套模型class
public class Data
{
public long Id { get; set; }
public string DeviceCode { get; set; }
}
当使用 automap 索引此文档时,nest 将为 属性 DeviceCode 创建两个字段 - DeviceCode 和 DeviceCode.keyword。我想基于 DeviceCode.keyword 字段进行排序,不区分大小写 - 是否可以通过自动映射实现这一点(通过在模型 class 中使用属性映射应用分析器或过滤器)。我想在弹性搜索中保留文本和关键字字段。
问候 葡萄藤
可以将 normalizer 应用于 keyword
字段以在索引时将值小写以进行不区分大小写的查询和聚合
var client = new ElasticClient(settings);
var createIndexResponse = client.Indices.Create("my-data", c => c
.Settings(s => s
.Analysis(a => a
.Normalizers(n => n
.Custom("lowercase", cn => cn
.Filters("lowercase")
)
)
)
)
.Map<Data>(m => m
.AutoMap()
.Properties(p => p
.Text(t => t
.Name(n => n.DeviceCode)
.Fields(f => f
.Keyword(k => k
.Name("keyword")
.IgnoreAbove(256)
)
.Keyword(k => k
.Name("keyword_lowercase")
.Normalizer("lowercase")
.IgnoreAbove(256)
)
)
)
)
)
);
然后使用keyword_lowercase
multi-field
var searchResponse = client.Search<Data>(s => s
.Index("my-data")
.Sort(so => so
.Ascending(f => f.DeviceCode.Suffix("keyword_lowercase"))
)
);