简体   繁体   中英

Azure Search Service Deleting an Index

I'm very new to the Azure Search Service and I'm following along with the guides that I found on the web. In one of these guides they have a method such as this:

static void Main(string[] args)
{
    IConfigurationBuilder builder = new ConfigurationBuilder().AddJsonFile("appsettings.json");
    IConfigurationRoot configuration = builder.Build();

    SearchServiceClient serviceClient = CreateSearchServiceClient(configuration);
    var test = serviceClient.Indexes.GetClient("testindex");

    Console.WriteLine("{0}", "Deleting index...\n");
    DeleteHotelsIndexIfExists(serviceClient);

    Console.WriteLine("{0}", "Creating index...\n");
    CreateHotelsIndex(serviceClient);

    ISearchIndexClient indexClient = serviceClient.Indexes.GetClient("hotels");

    Console.WriteLine("{0}", "Uploading documents...\n");
    UploadDocuments(indexClient);

    ISearchIndexClient indexClientForQueries = CreateSearchIndexClient(configuration);

    RunQueries(indexClientForQueries);

    Console.WriteLine("{0}", "Complete.  Press any key to end application...\n");
    Console.ReadKey();
}

So the idea above is to delete an index if it exists, then create a new index. Then generate and upload documents and then finally run a search. Now this all makes sense to me how it works, but one thing that troubles me is the deleting the index part. Essentially what would appear to me is that they are suggesting to always delete the index and then recreate it. Which makes me concerned.

If this is a live index, then by me deleting it even for a split of a second, wouldn't that mean that services calling search on this index will fail? The other thing that I'm concerned about is that in most cases my data will be 90% the same, I'll have some updates and newer records, maybe few deleted. But if my database has a million records, it just seems foolish to delete it all and then create it all over again.

Is there a better approach. Is there a way for me to just update the documents in the index as opposed to deleting it?

So I guess this is a 2-part question. 1. If I delete the index, will it stop the searching capability while the new one is being built? 2. Is there a better approach than just deleting the index. If there is a way to update, how do you handle scenarios where document structure has changed, for example a new field is added.

That sample is meant to demonstrate how to call Azure Search via the .NET SDK. It should not be taken as an example of best practices.

To answer your specific questions:

  1. Deleting the index will immediately cause all requests targeting that index to fail. You should not delete a live index in production. Also, there is not yet a backup/restore capability for indexes, so even if an index is not live, you shouldn't delete it unless you can restore the data from elsewhere.
  2. If you need to update documents without updating the index schema, you can achieve this with the "merge" action of the Index API. There is a tutorial that covers this here . If you need to add a new field, you can do this without deleting and re-creating the index -- just use one of the Indexes.CreateOrUpdate methods of the .NET SDK. By default this operation does not cause any index downtime. However, if you need to delete, rename, or change the type of an existing field, or enable new behaviors on a field (eg -- make it facetable, sortable, or part of a suggester), you'll need to create a new index. For this reason, it is recommended that your application implement an extra layer of indirection over index names so that you can swap and re-build indexes when needed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM