gpt4 book ai didi

c# - 使用 NEST 2.x 使用多字段映射语法创建索引

转载 作者:太空狗 更新时间:2023-10-29 20:56:17 26 4
gpt4 key购买 nike

我似乎无法在 NEST 2.0 中获得正确的多字段映射语法——如果这是正确的术语。我找到的每个映射示例似乎都是 <= NEST 的 1.x 版本。我是 Elasticsearch 和 NEST 的新手,我一直在阅读他们的文档,但 NEST 文档尚未针对 2.x 完全更新。

基本上,我不需要索引或存储整个类型。有些字段我只需要索引,有些字段我需要索引和检索,有些我不需要索引,只用于检索。

MyType
{
// Index this & allow for retrieval.
int Id { get; set; }

// Index this & allow for retrieval.
// **Also**, in my searching & sorting, I need to sort on this **entire** field, not just individual tokens.
string CompanyName { get; set; }

// Don't index this for searching, but do store for display.
DateTime CreatedDate { get; set; }

// Index this for searching BUT NOT for retrieval/displaying.
string CompanyDescription { get; set; }

// Nest this.
List<MyChildType> Locations { get; set; }
}

MyChildType
{
// Index this & allow for retrieval.
string LocationName { get; set; }

// etc. other properties.
}

已经能够使用以下示例按原样索引整个对象和子对象:

client.Index(item, i => i.Index(indexName));

然而,实际对象比这大很多,我真的不需要大部分。我找到了这个,看起来像我想做的,但在旧版本中:multi field mapping elasticsearch

我认为“映射”是我的目标,但正如我所说,我是 Elasticsearch 和 NEST 的新手,我正在尝试学习术语。

温柔一点! :) 这是我第一次就 SO 提问。谢谢!

最佳答案

除了Colin'sSelçuk's答案,您还可以通过流畅的(和对象初始化语法)映射 API 完全控制映射。这是一个基于您的要求的示例

void Main()
{
var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
var connectionSettings = new ConnectionSettings(pool);

var client = new ElasticClient(connectionSettings);

client.Map<MyType>(m => m
.Index("index-name")
.AutoMap()
.Properties(p => p
.String(s => s
.Name(n => n.CompanyName)
.Fields(f => f
.String(ss => ss
.Name("raw")
.NotAnalyzed()
)
)
)
.Date(d => d
.Name(n => n.CreatedDate)
.Index(NonStringIndexOption.No)
)
.String(s => s
.Name(n => n.CompanyDescription)
.Store(false)
)
.Nested<MyChildType>(n => n
.Name(nn => nn.Locations.First())
.AutoMap()
.Properties(pp => pp
/* properties of MyChildType */
)
)
)
);
}

public class MyType
{
// Index this & allow for retrieval.
public int Id { get; set; }

// Index this & allow for retrieval.
// **Also**, in my searching & sorting, I need to sort on this **entire** field, not just individual tokens.
public string CompanyName { get; set; }

// Don't index this for searching, but do store for display.
public DateTime CreatedDate { get; set; }

// Index this for searching BUT NOT for retrieval/displaying.
public string CompanyDescription { get; set; }

// Nest this.
public List<MyChildType> Locations { get; set; }
}

public class MyChildType
{
// Index this & allow for retrieval.
public string LocationName { get; set; }

// etc. other properties.
}

这会产生映射

{
"properties": {
"id": {
"type": "integer"
},
"companyName": {
"type": "string",
"fields": {
"raw": {
"type": "string",
"index": "not_analyzed"
}
}
},
"createdDate": {
"type": "date",
"index": "no"
},
"companyDescription": {
"type": "string",
"store": false
},
"locations": {
"type": "nested",
"properties": {
"locationName": {
"type": "string"
}
}
}
}
}

调用 .AutoMap()使 NEST 根据属性类型和应用于它们的任何属性推断映射。那么.Properties()覆盖任何推断的映射。例如

  • CompanyName被映射为 multi_field与现场 companyName使用标准分析仪和 companyName.raw 进行分析不分析。您可以使用 .Field(f => f.CompanyName.Suffix("raw")) 在查询中引用后者
  • Locations被映射为 nested类型(默认情况下自动映射会将其推断为 object 类型映射)。然后,您可以为 MyChildType 定义任何特定映射使用 .Properties()Nested<MyChildType>()里面打电话。

关于c# - 使用 NEST 2.x 使用多字段映射语法创建索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35350490/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com