I'm trying to create a new index in ElasticSearch using NEST, this new index has aliases, so I need an instance of IndexName in order to achieve this. This is the request I'm trying to implement:
PUT activeIndexName
{
"aliases": {
"indexName": {}
}
}
My code in NEST:
public bool CreateIndex(string indexName)
{
string date = DateTime.Now.Date.ToString("yyyy.MM.dd", CultureInfo.InvariantCulture);
string activeIndexName = string.Format("{0}-{1}-000001", indexName, date);
IDictionary<IndexName, IAlias> aliasDict = new Dictionary<IndexName, IAlias>();
IndexName indexNameDict = IndexName.From<IndexName>(indexName);//PROBLEM HERE
aliasDict.Add(indexNameDict, new Alias());
CreateIndexRequest createIndexRequest = new CreateIndexRequest(activeIndexName)
{
Aliases = new Aliases(aliasDict)
};
CreateIndexResponse response = _client.Indices.Create(createIndexRequest);
return response.IsValid;
}
And the error message I'm getting from the above code:
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().
It sounds to me as if the index needs to be there before it can be created? However it needs to be created with an Alias. Nest.IndexName doesn't have a constructor so I'm not sure how to have an instance of it so that the Dictionary<IndexName, IAlias> can be created.
IndexName
implements an implicit conversion from string
so you can simplify and fix your code by replacing
IndexName indexNameDict = IndexName.From<IndexName>(indexName);//PROBLEM HERE
aliasDict.Add(indexNameDict, new Alias());
with
aliasDict.Add(indexName, new Alias());