如何使用弹性搜索创建另一个索引?

如何解决如何使用弹性搜索创建另一个索引?

我是使用 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 以同样的方式,旧的在任何地方都不存在。

为什么?如何创建新的第二个表?

解决方法

看看给出的例子,它看起来像这样

  1. 删除"store"索引
  2. 检查 "store" 索引是否存在(不会因为它刚刚被删除)
  3. 创建 "store" 索引
  4. 设置默认索引以用作 "store" 索引(将在索引第一个文档时创建,如果它不存在)
  5. index store 类型到 "store" 索引
  6. "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 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?