如何正确地将别名指向索引



我正在创建一个带有时间戳的弹性搜索索引,并将别名指向它。

类似:indexname=index-201512302002515529,alias=index

因此,如果我需要重新索引,我会创建一个新索引(带有另一个时间戳),在索引完成时将别名指向它,并删除旧索引。

这个过程是有效的,但在使用别名搜索索引时,我总是得到ZERO命中率。

我在连接设置的SetDefaultIndex()上使用别名。

创建索引:

    var timeStamp = DateTime.Now.ToString("yyyyMMddHHmmssffff");
    var indexName = string.Concat(containerName, "-", timeStamp);
    CreateIndex(indexName);
    Console.WriteLine($"Creating index {indexName}...");
    //do the indexing
    Console.WriteLine("Indexing from Database...");
    IndexAllFromDb();
    //delete aliases
    if (cli.AliasExists(containerName).Exists)
    {
        Console.WriteLine("Deleting Aliases...");
        foreach (var index in cli.GetIndicesPointingToAlias(containerName))
        {
            cli.Alias(a => a
                .Remove(remove => remove
                    .Index(index)
                    .Alias(containerName)
                )
            );
        }
    }
    Console.WriteLine("Creating Aliases...");
    //create the alias
    cli.Alias(a => a
        .Add(add => add
            .Index(indexName)
            .Alias(containerName)
        )
    );

CreateIndex(indexName)创建所有映射和分析设置。IndexAllFromDB()读取SQL数据库并进行索引。

我正在使用此代码连接:

    var settings = new ConnectionSettings(new Uri(connectionString));
    settings.SetDefaultIndex(SiteSettings.Settings.SearchContainerName);
    var cli = new ElasticClient(settings);

如果我直接用别名创建索引,它会起作用,所以我可能对别名做了错误的处理。。。。

谢谢。

ConnectionSettings上使用别名作为默认索引是可以的;除了你在评论中提到的错误之外,我可以从你目前的方法中看到两件潜在的事情:

  • 您一次删除一个别名,然后将别名添加到新索引中。这些操作可以在一个操作中以原子方式执行
  • 索引中碎片的默认刷新间隔是1秒,因此您可能在刷新之前进行搜索。您可能需要考虑在所有索引完成后进行刷新,以确保新索引的文档出现在搜索结果中

我总结了这个例子,它可能会帮助你开始

void Main()
{
    var containerName = "index";
    var settings = new ConnectionSettings(new Uri("http://localhost:9200"))
        .SetDefaultIndex(containerName)
        .ExposeRawResponse(true)
        .SetConnectionStatusHandler(response =>
        {
            // log out the requests
            if (response.Request != null)
            {
                Console.WriteLine("{0} {1} n{2}n", response.RequestMethod.ToUpperInvariant(), response.RequestUrl,
                    Encoding.UTF8.GetString(response.Request));
            }
            else
            {
                Console.WriteLine("{0} {1}n", response.RequestMethod.ToUpperInvariant(), response.RequestUrl);
            }
            if (response.ResponseRaw != null)
            {
                Console.WriteLine("Status: {0}n{1}nn{2}n", response.HttpStatusCode, Encoding.UTF8.GetString(response.ResponseRaw), new String('-', 30));
            }
            else
            {
                Console.WriteLine("Status: {0}nn{1}n", response.HttpStatusCode, new String('-', 30));
            }
        });
    var client = new ElasticClient(settings);
    var timeStamp = DateTime.Now.ToString("yyyyMMddHHmmssffff");
    var indexName = string.Concat(containerName, "-", timeStamp);
    var indexResponse = client.CreateIndex(indexName, c => c
        .AddMapping<Item>(m => m
            .MapFromAttributes()
        )
    );
    //do the indexing
    Console.WriteLine("Indexing from Database...");
    IndexAllFromDb(client, indexName);
    // add alias to the new index
    var aliasDescriptor = new AliasDescriptor()
        .Add(add => add.Alias(containerName).Index(indexName));
    // get any existing indices that have the alias
    Console.WriteLine($"Get existing indices with alias '{containerName}'");
    var aliasesResponses = client.GetAliases(a => a.Alias(containerName));
    foreach (var index in aliasesResponses.Indices)
    {
        if (index.Value.Any(a => a.Name == containerName))
        {
            Console.WriteLine($"Removing alias from index '{index.Key}'");
            aliasDescriptor.Remove(remove => remove.Alias(containerName).Index(index.Key));
        }
    }
    // atomic add and delete
    var aliasResponse = client.Alias(aliasDescriptor);
    var countResponse = client.Search<Item>(s => s.SearchType(SearchType.Count));
    Console.WriteLine($"{countResponse.Total} items");
}
private static void IndexAllFromDb(IElasticClient client, string indexName)
{
    var items = Enumerable.Range(1, 1000).Select(i => Item.Create(i));
    // refresh shards manually as opposed to relying on the refresh interval
    // https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-index_.html#index-refresh
    var indexManyResponse = client.Bulk(b => b.IndexMany(items, (bi, i) => bi.Index(indexName).Document(i)).Refresh());
}
public class Item
{
    public int Id { get; set; }
    public string Name { get; set; }
    public static Item Create(int id)
    {
        return new Item { Id = id, Name = "Item " + id };
    }
}

第二次运行此操作(以确保存在应用了别名的现有索引)会产生以下结果

POST http://localhost:9200/index-201601011621518238 
{
  "settings": {
    "index": {}
  },
  "mappings": {
    "item": {
      "properties": {
        "id": {
          "type": "integer"
        },
        "name": {
          "type": "string"
        }
      }
    }
  }
}
Status: 200
{"acknowledged":true}
------------------------------
Indexing from Database...
POST http://localhost:9200/_bulk?refresh=true 
{ "index" :  {"_index":"index-201601011621518238","_type":"item","_id":"1"} }
{"id":1,"name":"Item 1"}
// ... cut out for brevity
{ "index" :  {"_index":"index-201601011621518238","_type":"item","_id":"1000"} }
{"id":1000,"name":"Item 1000"}

Status: 200
{"took":124,"errors":false,"items":[{"index":{"_index":"index-201601011621518238","_type":"item","_id":"1","_version":1,"status":201}},
// ... cut out for brevity
{"index":{"_index":"index-201601011621518238","_type":"item","_id":"1000","_version":1,"status":201}}]}
------------------------------
Get existing indices with alias 'index'
GET http://localhost:9200/_aliases/index
Status: 200
{"index-201601011616271553":{"aliases":{"index":{}}},"index-201601011621518238":{"aliases":{}}}
------------------------------
Removing alias from index 'index-201601011616271553'
POST http://localhost:9200/_aliases 
{
  "actions": [
    {
      "add": {
        "index": "index-201601011621518238",
        "alias": "index"
      }
    },
    {
      "remove": {
        "index": "index-201601011616271553",
        "alias": "index"
      }
    }
  ]
}
Status: 200
{"acknowledged":true}
------------------------------
POST http://localhost:9200/index/item/_search?search_type=count 
{}
Status: 200
{"took":1,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":1000,"max_score":0.0,"hits":[]}}
------------------------------
1000 items

最新更新