【问题标题】:How I create another index using the elastic search?如何使用弹性搜索创建另一个索引?
【发布时间】:2021-04-11 18:50:39
【问题描述】:

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

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

【问题讨论】:

    标签: c# asp.net elasticsearch nest elasticsearch-5


    【解决方案1】:

    看看给出的例子,看起来会这样

    1. 删除"store"索引
    2. 检查"store" 索引是否存在(不存在,因为它刚刚被删除)
    3. 创建"store" 索引
    4. 将默认索引设置为"store" 索引(将在索引第一个文档时创建,如果它不存在)
    5. 索引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 和搜索相关的概念。

    【讨论】:

    • 我会试试这个,然后告诉你,Russ。谢谢大佬。
    猜你喜欢
    • 2014-01-06
    • 2019-09-28
    • 2022-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-25
    • 1970-01-01
    相关资源
    最近更新 更多