简体   繁体   English

NEST2:如何一次指定数据库索引名称

[英]NEST2: How to specify the db index name once

Our C# WebAPI application uses of an ElasticSearch database. 我们的C#WebAPI应用程序使用ElasticSearch数据库。 We're using NEST2 to access the db. 我们正在使用NEST2访问数据库。

All the NEST2 queries in the code specify the database index name, here an example: 代码中的所有NEST2查询都指定数据库索引名称,此处为示例:

public TestQuery[] GetAllDocuments()
{
    var readRecords = ec.Search<TestDocument>(s => s
        .Index("my-index-name")
        .Query(q => q.
            QueryString(qs => qs.Query("*")))).Documents;

    return readRecords.ToArray();
}

Sometimes we forget to specify the index name in the query... the problem doesn't manifest itself immediately as the API works as expected and everything is fine... only when we add another index with some similar documents in it we see the problem 有时我们忘记在查询中指定索引名称...问题不会立即显现,因为API可以按预期工作,并且一切都很好...仅当我们在其中添加另一个包含一些类似文档的索引时,我们才会看到问题

Is it possible to specify the index name once for all after NEST2 initialization? NEST2初始化后是否可以一次全部指定索引名称? Doing so I'll avoid the burden to remember to insert it on every single query 这样一来,我就避免了要记住在每个查询中插入它的负担

You are looking for .DefaultIndex method on ConnectionSettings . 您正在寻找ConnectionSettings上的.DefaultIndex方法。

var settings = new ConnectionSettings()
    .DefaultIndex("defaultindex");

Hope it helps. 希望能帮助到你。

You should take a look at aliases: Index Alias 您应该看看别名: 索引别名

So when you have a new index with similar documents you can add one alias after you create it and in the query in index name you specify the alias. 因此,当您拥有一个包含类似文档的新索引时,可以在创建索引后添加一个别名,并在索引名称的查询中指定别名。 In this way you can query 1 to multiple indexes using same name. 这样,您可以使用相同的名称查询1到多个索引。 Hope it helps! 希望能帮助到你! If you need more info, write a comment. 如果您需要更多信息,请写评论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM