Re-running az rest for search index creation is failing on 2nd run which it shouldn't?

md-infogr8 20 Reputation points
2024-09-03T15:50:14.7533333+00:00

Hi,

I'm creating a search index using az rest which is working as expected. However, when my script runs again the call fails with code: "ResourceNameAlreadyInUse" but the documentation states that:

"HTTPS is required for all service requests. If the index doesn't exist, it's created. If it already exists, it's updated to the new definition."

az rest -m post -u https://[service-name].search.windows.net/indexes?api-version=2024-07-01 --body "{'name':'search-index'}" --headers Content-Type=application/json api-key=[api-key]'

Am I missing an az rest switch for this or something else?

Thanks.

Azure AI Search
Azure AI Search
An Azure search service with built-in artificial intelligence capabilities that enrich information to help identify and explore relevant content at scale.
949 questions
0 comments No comments
{count} votes

Accepted answer
  1. Grmacjon-MSFT 17,886 Reputation points
    2024-09-03T21:29:51.65+00:00

    Hi @md-infogr8 you're the "ResourceNameAlreadyInUse" error because the POST method is being used. According to the Azure documentation, POST is typically used to create new resources, and it might not handle updates as expected.

    Before attempting to create the index again, verify if it already exists in your Azure Search service. You can use the following az rest command:

    az rest -m get https://[service-name].search.windows.net/indexes/search-index?api-version=2024-07-01 --headers api-key=[api-key]
    

    This command will return a response code of 200 if the index exists, and an error code if it doesn't.

    Next, to update an existing index, you should use the PUT method instead of POST. Here's how you can modify your command:

    
    az rest -m put -u https://[service-name].search.windows.net/indexes/search-index?api-version=2024-07-01 --body "{'name':'search-index'}" --headers Content-Type=application/json api-key=[api-key]
    
    

    This should update the index if it already exists, and create it if it doesn't.

    Hope that helps. Please let us know if you've more questions.

    -Grace

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.