1

How can I specify the index name using attribute using NEST for ElasticSearch?

Here is the POCO class I used:

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

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

1 Answer 1

1

You can't specify the index name(s) for a specific POCO type using attributes. You can however specify it on ConnectionSettings using .DefaultMappingFor<T>, where T is your POCO type.

When using BulkAll, you can specify an index name per bulk operation using 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; }
}
Sign up to request clarification or add additional context in comments.

6 Comments

I need to dynamically build an index name based on the message content. Using the default mapping doesn't allow that I think.
@pellea in that scenario, you likely will specify the index name in each request, as opposed to having a convention of POCO to index name
I didn't find a way to do that with the bulkAll method. For now I "split" my dataset by index beforehand and import items with the default index field. If I use the IndexMany/CreateMany directly I can specify the index per item but I didn't find a way with the bulkAll method.
@pellea With BulkAll, you can use BufferToBulk(..) to control the bulk operation for each item, including setting an index name
If I use the BufferToBulk I got this error Index name is null for the given type and no default index is set. Map an index name using ConnectionSettings.DefaultMappingFor<TDocument>() or set a default index using ConnectionSettings.DefaultIndex(). I don't get this error if I use the IndexMany directly without the BulkAll. Here is how I set the index : desc.Index("dev-testindex");
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.