Michelle
Michelle

Reputation: 73

Azure Search Merge Index Operation Returns Exception

I'm trying to run a batch operation to merge some changes to an existing Azure Search Index, but I keep running into this error:

{Microsoft.Rest.Azure.CloudException: The request is invalid. Details: parameters : Object reference not set to an instance of an object.

Here's a snippet of my code:

public static void UploadData<T>(List<T> data, ISearchIndexClient indexClient) where T : class
    {
        int totalFailedToIndex = 0;
        int totalPassedToIndex = 0;


        for (int i = 0; i < data.Count; i = i + 500)
        {
            var stBatch = data.Skip(i).Take(500).ToList();

            // Insert the data.
            var serviceTreeBatch = IndexBatch.Merge(stBatch);
            try
            {
                var index = indexClient.Documents.Index(serviceTreeBatch);
                totalPassedToIndex += index.Results.Count();

            }
            catch (IndexBatchException e)
            {
                totalFailedToIndex += e.IndexingResults.Where(f => !f.Succeeded).Count();
            }
            catch(Exception e)
            {
                continue;
            }
        }

    }

I've never seen this error before, and I can't seem to find anything online about it. Any help would definitely be appreciated!

Edit: Here's an example of the Type T that I'm passing in. The ProjectId is the key for these index items. It's also important to note that this version does not have all the index values (it's a merge so I'm only uploading the values that could possibly change along with the key). I'm wondering if the missing values is what's causing this to fail?

    public class IndexItemModel
{
    /// <summary>
    /// Unique ProjectId
    /// </summary>
    public string ProjectId { get; set; }
    public string RepositoryId { get; set; }

    public IEnumerable<string> Repository_Users { get; set; }

    public string Repository_UsersString { get; set; }
}

Upvotes: 3

Views: 1166

Answers (1)

Bruce Johnston
Bruce Johnston

Reputation: 8634

This issue is caused by two separate problems:

  1. The field names in the Index API request don't match the index definition (confirmed via troubleshooting). The root cause was a missing [SerializePropertyNamesAsCamelCase] attribute on the model class.
  2. At the time of this writing (July 2019), there is a bug in the Azure Search Index REST API that results in the unhelpful error message "Object reference not set to an instance of an object" being returned instead of a more helpful error message. This is a regression. The expected behavior is an error message that names the unrecognized field and specifically says that the field name isn't recognized.

If anybody else comes across this unhelpful error message, double-check that your field names match between the Index API and the index definition. Sorry for the inconvenience caused by this bug.

Upvotes: 3

Related Questions