Azure-sdk-for-net: Azure Search: does it support deleting documents?

Created on 20 Mar 2015  路  3Comments  路  Source: Azure/azure-sdk-for-net

I've searched but can't find any tests demonstrating that the .NET Azure SDK for Azure Search supports deleting documents in the search service index.

Using Microsoft Azure Search Library 0.9.7-preview .

Intellisense in VS doesn't show any operation/method other than Create.

Was expecting IndexBatch.Delete to be able to state:

await indexClient.Documents.IndexAsync(IndexBatch.Delete(myDocs.Select(doc => IndexAction.Delete(doc))));

Most helpful comment

The answer was valid for the SDK version mentioned in the original issue. Then we changed the SDK to make things easier. :-)

Here is how you would delete a batch with version 1.0 or later:

var batch = IndexBatch.Delete("id", new[] { "123", "456", "789" });
indexClient.Documents.Index(batch);

Or, if you want to mix different operations in the same batch, you can do this:

var batch =
    IndexBatch.New(new[] {
        IndexAction.Delete("id", "123"),
        IndexAction.Delete("id", "456"),
        IndexAction.Delete("id", "789"),
        /* Put IndexAction.Upload, Merge, or MergeOrUpload here */
    });

indexClient.Documents.Index(batch);

All 3 comments

Yes. You can use IndexActionType.Delete for this:

var batch =
    IndexBatch.Create(
        new IndexAction(IndexActionType.Delete, new Document() { { "id", "123" } }),
        new IndexAction(IndexActionType.Delete, new Document() { { "id", "456" } }),
        new IndexAction(IndexActionType.Delete, new Document() { { "id", "789" } }));

indexClient.Documents.Index(batch);

Urm...... not with the latest version of the .sdk you can't!!! Might need to update the code snippet above to something like
IndexBatch.Delete(
new Document() { { "id", "123" } },
new Document() { { "id", "456" } },
new Document() { { "id", "789" } });

The answer was valid for the SDK version mentioned in the original issue. Then we changed the SDK to make things easier. :-)

Here is how you would delete a batch with version 1.0 or later:

var batch = IndexBatch.Delete("id", new[] { "123", "456", "789" });
indexClient.Documents.Index(batch);

Or, if you want to mix different operations in the same batch, you can do this:

var batch =
    IndexBatch.New(new[] {
        IndexAction.Delete("id", "123"),
        IndexAction.Delete("id", "456"),
        IndexAction.Delete("id", "789"),
        /* Put IndexAction.Upload, Merge, or MergeOrUpload here */
    });

indexClient.Documents.Index(batch);
Was this page helpful?
0 / 5 - 0 ratings