如何使用 NEST 为 Elasticsearch 指定索引?
How do I specify an index for Elasticsearch using NEST?
如果我 运行 下面的代码将在所有索引上创建一个映射,这是我不想要的。我找不到仅指定所需索引的文档。
如何指定将此映射应用到哪个索引?
var client = new ElasticClient();
var response = client.Map<Company>(m => m
.Properties(props => props
.Number(n => n
.Name(p => p.ID)
.Type(NumberType.Integer)
)
)
);
将.Index()
添加到put映射描述符
var response = client.Map<Company>(m => m
.Index("index-name")
.Properties(props => props
.Number(n => n
.Name(p => p.ID)
.Type(NumberType.Integer)
)
)
);
这会将映射放入现有索引中。如果索引尚不存在,您可以创建它并在一个请求中为它定义一个映射。例如
var createIndexResponse = client.CreateIndex("index-name", c => c
// settings for the index
.Settings(s => s
.NumberOfShards(3)
.NumberOfReplicas(1)
.RefreshInterval("5s")
)
// mappings for the index
.Mappings(m => m
.Map<Company>(mc => mc
.Properties(props => props
.Number(n => n
.Name(p => p.ID)
.Type(NumberType.Integer)
)
)
)
)
);
这样也行。
string IndexName = "my_index";
this.client.CreateIndex(IndexName, c =>
c.AddMapping<CForm>
(m => m.Properties(ps => ps.Attachment
(a => a.Name(o => o.Document)
.TitleField(t => t.Name(x => x.Name)
.TermVector(TermVectorOption.WithPositionsOffsets))))));
// Create Mappings for the fields with specific properties.
// You can also make field1 a multi-field and make it both analyzed and not_analyzed
// to get the best of both worlds (i.e. text matching on the analyzed field + aggregation on the exact value
// of the not_analyzed raw sub-field).
// Field: Plan
var result = this.client.Map<CForm>(m => m
.Properties(props => props
.MultiField(s => s
.Name(p => p.Plan)
.Fields(pprops => pprops
.String(ps => ps.Name(p => p.Plan).Index(FieldIndexOption.NotAnalyzed))
.String(ps => ps.Name("original").Index(FieldIndexOption.Analyzed))
)
)
)
);
如果我 运行 下面的代码将在所有索引上创建一个映射,这是我不想要的。我找不到仅指定所需索引的文档。
如何指定将此映射应用到哪个索引?
var client = new ElasticClient();
var response = client.Map<Company>(m => m
.Properties(props => props
.Number(n => n
.Name(p => p.ID)
.Type(NumberType.Integer)
)
)
);
将.Index()
添加到put映射描述符
var response = client.Map<Company>(m => m
.Index("index-name")
.Properties(props => props
.Number(n => n
.Name(p => p.ID)
.Type(NumberType.Integer)
)
)
);
这会将映射放入现有索引中。如果索引尚不存在,您可以创建它并在一个请求中为它定义一个映射。例如
var createIndexResponse = client.CreateIndex("index-name", c => c
// settings for the index
.Settings(s => s
.NumberOfShards(3)
.NumberOfReplicas(1)
.RefreshInterval("5s")
)
// mappings for the index
.Mappings(m => m
.Map<Company>(mc => mc
.Properties(props => props
.Number(n => n
.Name(p => p.ID)
.Type(NumberType.Integer)
)
)
)
)
);
这样也行。
string IndexName = "my_index";
this.client.CreateIndex(IndexName, c =>
c.AddMapping<CForm>
(m => m.Properties(ps => ps.Attachment
(a => a.Name(o => o.Document)
.TitleField(t => t.Name(x => x.Name)
.TermVector(TermVectorOption.WithPositionsOffsets))))));
// Create Mappings for the fields with specific properties.
// You can also make field1 a multi-field and make it both analyzed and not_analyzed
// to get the best of both worlds (i.e. text matching on the analyzed field + aggregation on the exact value
// of the not_analyzed raw sub-field).
// Field: Plan
var result = this.client.Map<CForm>(m => m
.Properties(props => props
.MultiField(s => s
.Name(p => p.Plan)
.Fields(pprops => pprops
.String(ps => ps.Name(p => p.Plan).Index(FieldIndexOption.NotAnalyzed))
.String(ps => ps.Name("original").Index(FieldIndexOption.Analyzed))
)
)
)
);