简体   繁体   English

如何使用NEST为ElasticSearch lib使用属性指定索引名称?

[英]How to specify the index name using attribute using NEST for ElasticSearch lib?

How can I specify the index name using attribute using NEST for ElasticSearch? 如何使用NEST for ElasticSearch使用属性指定索引名称?

Here is the POCO class I used: 这是我使用的POCO类:

[Serializable]
[ElasticsearchType(IdProperty = "msgid")]
public class Message
{
    [PropertyName("msgid")]
    public string Id { get; set; }

    [PropertyName("date")]
    public string Date { get; set; }
}

You can't specify the index name(s) for a specific POCO type using attributes. 您不能使用属性为特定的POCO类型指定索引名称。 You can however specify it on ConnectionSettings using .DefaultMappingFor<T> , where T is your POCO type. 但是,您可以使用.DefaultMappingFor<T>ConnectionSettings上指定它,其中T是您的POCO类型。

When using BulkAll , you can specify an index name per bulk operation using BufferToBulk(...) 使用BulkAll ,可以使用BufferToBulk(...)为每个批量操作指定索引名称BufferToBulk(...)

private static void Main()
{
    var defaultIndex = "my-index";
    var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));

    var settings = new ConnectionSettings(pool)
        .DefaultIndex(defaultIndex);

    var client = new ElasticClient(settings);

    var bulkAll = client.BulkAll(MyDocuments(), b => b
        .Index(null)
        .Type(null)
        .BufferToBulk((bu, docs) =>
        {
            foreach (var doc in docs)
            {
                bu.Index<MyDocument>(bi => bi
                    .Index(doc.Id % 2 == 0 ? "even-index" : "odd-index")
                    .Document(doc)
               );
            }
        })
        .RefreshOnCompleted()
        .Size(100)
    );

    bulkAll.Wait(TimeSpan.FromMinutes(20), _ => {});
}

private static IEnumerable<MyDocument> MyDocuments()
{
    for (int i = 0; i < 1000; i++)
        yield return new MyDocument(i);
}


public class MyDocument 
{
    public MyDocument(int id)
    {
        Id = id;
        Message = $"message {id}";
    }

    public int Id { get; set; }

    public string Message { get; set; }
}

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

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