NEST - IndexMany doesn't index my objects

I've used NEST for elasticsearch for a while now and up until now I've used the regular ElasticSearchClient.Index(...) function, but now I want to index many items in a bulk operation.

I found the IndexMany(...) function, but I must do something wrong because nothing is added to the elastic search database as it does with the regular Index(...) function?

Does anyone have any idea?

Thanks in advance!

Upvotes: 2

Views: 1459

Answers (2)

I found the problem. I had to specifiy the index name in the call to IndexMany

 var res = ElasticClient.CreateIndex("pages", i => i.Mappings(m => m.Map<ESPageViewModel>(mm => mm.AutoMap())));

                var page = new ESPageViewModel
                {
                    Id = dbPage.Id,
                    PageId = dbPage.PageId,
                    Name = dbPage.Name,
                    Options = pageTags,
                    CustomerCategoryId = saveTagOptions.CustomerCategoryId,
                    Link = dbPage.Link,
                    Price = dbPage.Price
                };

                var pages = new List<ESPageViewModel>() { page };

                var res2 = ElasticClient.IndexManyAsync<ESPageViewModel>(pages, "pages");

This works as expected. Guess I could specify a default index name in the configuration to avoid specifying the index for the IndexMany call.

Upvotes: 1

danvasiloiu
danvasiloiu

Reputation: 751

If you are using C# you should create a list of objects that you want to insert then call the IndexMany function.

Example :

List<Business> businessList = new List<Business>();

#region Fill the business list
............................... 
#endregion

if (businessList.Count == 1000) // the size of the bulk.
{
     EsClient.IndexMany<Business>(businessList, IndexName);

     businessList.Clear();
}

And in the end check again

if (businessList.Count > 0)
{
    EsClient.IndexMany<Business>(businessList, IndexName);
 }

Upvotes: 0

Related Questions