如何解决如何使用弹性搜索创建另一个索引?
我是使用 c# 的 Elasticsearch 和 nesT 等新手。所以,到目前为止,我已经学会并设法编写了一个代码来创建索引,但问题是如何创建第二个表(类型)。如果我以同样的方式创建它,那么它只会创建一个表而不是第二个。
代码:
public static void CreateIndex()
{
ConnectionSettings settings = new ConnectionSettings(new Uri("http://localhost:9200"));
settings.DefaultIndex("store");
Elasticclient client = new Elasticclient(settings);
client.Indices.Delete(Indices.Index("store"));
var indexSettings = client.Indices.Exists("store");
if (!indexSettings.Exists)
{
var response = client.Indices.Create(Indices.Index("store"));
}
}
public static void CreateSeed()
{
int seedValue = 1;
int limitValue = 20000;
IList<stores> List = new List<stores>();
ConnectionSettings settings = new ConnectionSettings(new Uri("http://localhost:9200"));
settings.DefaultIndex("store");
Elasticclient esClient = new Elasticclient(settings);
var item = new store() { ID = seedValue,Title = "item" + seedValue.ToString(),IsPublished = true };
var response = esClient.IndexAsync(item,idx => idx.Index("store"));
}
/// <summary>
///
/// </summary>
public static void CreateMappings()
{
ConnectionSettings settings = new ConnectionSettings(new Uri("http://localhost:9200"));
settings.DefaultIndex("store");
Elasticclient esClient = new Elasticclient(settings);
esClient.Map<stores>(m =>
{
var putMappingDescriptor = m.Index(Indices.Index("store")).AutoMap();
return putMappingDescriptor;
});
}
这会创建一个商店索引并且可以被检索。但是,如果我创建另一个不同名称的表,例如itemsstore 以同样的方式,旧的在任何地方都不存在。
为什么?如何创建新的第二个表?
解决方法
看看给出的例子,它看起来像这样
- 删除
"store"
索引 - 检查
"store"
索引是否存在(不会因为它刚刚被删除) - 创建
"store"
索引 - 设置默认索引以用作
"store"
索引(将在索引第一个文档时创建,如果它不存在) - index
store
类型到"store"
索引 - 为
"store"
索引创建映射
总而言之,该示例看起来只与 "store"
索引交互。
创建两个索引的简单示例是
var client = new ElasticClient();
var createIndexResponse = await client.Indices.CreateAsync("store");
if (!createIndexResponse.IsValid)
{
// take some action e.g. logging,exception,etc.
// To keep the example simple,just throw an exception
throw new Exception(createIndexResponse.DebugInformation);
}
createIndexResponse = await client.Indices.CreateAsync("itemsstore");
if (!createIndexResponse.IsValid)
{
throw new Exception(createIndexResponse.DebugInformation);
}
walkthrough on building a Nuget search web application may be useful。不同的分支有针对不同版本客户端的演练。例如,7.x branch 用于 NEST 7.x,7.x-codecomplete 显示完整示例。它将演示许多 Elasticsearch 和搜索相关概念。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。