Skip to content

BulkAll requires DefaultIndex or DefaultMappingFor with index #3951

@russcam

Description

@russcam

From: https://discuss.elastic.co/t/nest-client-bulkall-requires-defaultindexfor-or-defaultindex-to-work/189766

The BulkAll method of ElasticClient requires

  • DefaultMappingFor<T>() with Index

or

  • DefaultIndex()

or

  • .Index() on the BulkAll call

to work.

You'll get an ArgumentException otherwise :

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()

Repro

static void Main(string[] args)
{
	var connectionSettings = new ConnectionSettings(new Uri("http://localhost:9200"))
	.DisableDirectStreaming()
	.PrettyJson()
	.OnRequestCompleted(callDetails =>
	{
		if (callDetails.RequestBodyInBytes != null)
		{
			Console.WriteLine(
				$"{callDetails.HttpMethod} {callDetails.Uri} \n" +
				$"{Encoding.UTF8.GetString(callDetails.RequestBodyInBytes)}");
		}
		else
		{
			Console.WriteLine($"{callDetails.HttpMethod} {callDetails.Uri}");
		}

		Console.WriteLine();

		if (callDetails.ResponseBodyInBytes != null)
		{
			Console.WriteLine($"Status: {callDetails.HttpStatusCode}\n" +
					 $"{Encoding.UTF8.GetString(callDetails.ResponseBodyInBytes)}\n" +
					 $"{new string('-', 30)}\n");
		}
		else
		{
			Console.WriteLine($"Status: {callDetails.HttpStatusCode}\n" +
					 $"{new string('-', 30)}\n");
		}
	});
	//uncomment one of the following lines to crash
	//connectionSettings.DefaultMappingFor<Document>(d => d.IndexName("myindex"));
	//connectionSettings.DefaultIndex("mydefaultindex");

	ElasticClient client = new ElasticClient(connectionSettings);

	BulkAllObservable<Document> bulk = client.BulkAll(GetDocuments(), b => b
		.BufferToBulk((descriptor, list) =>
		{
			foreach (Document doc in list)
			{
				descriptor.Index<Document>(bi => bi
					.Index($"myindex-{doc.Date:yyyy.mm.dd}")
					.Document(doc)
				);
			}
		})
		.Size(2)
	);

	bulk.Subscribe(new BulkAllObserver(
		onNext: r => Console.WriteLine(string.Join(";", r.Items.Select(i => i.Id))),
		onError: e => Console.Error.WriteLine(e),
		onCompleted: () => Console.WriteLine("Done")
	));
	bulk.Wait(TimeSpan.FromSeconds(30), null);
}

private static IEnumerable<Document> GetDocuments()
{
	for (int i = 0; i < 10; i++)
	{
		yield return new Document { Date = DateTime.UtcNow.AddDays(i) };
	}
}

private class Document
{
	public DateTime Date { get; set; }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions