azure-functionsazure-cognitive-search

How do I run my Azure Search indexers from an Azure function?


I started out with some Java code that will run my indexers within an Azure AI Search resource. I then expose an API resource with Spring. When going to the URL http://localhost/run/indexers, the indexers will run. All of this works so far. The issue is when I try to call this URL from an azure function.

My Java app method that runs the indexers is very simple and looks like this:

@RequestMapping("/indexers")
public void runIndexers() {
    searchIndexerClient.runIndexer("my-indexer");
}
// debug log statements

I've created an Event Grid Topic that will trigger my Function App and Function, which will connect to that URL that runs my indexers. My java Azure Function looks like this:

@FunctionName("RunIndexers")
public void runIndexers(
    @EventGridTrigger(
        name = "event"
    )
    String content,
    final ExecutionContext context) {
    String apiUrl= "http://localhost/run/indexers";

    try {
        URL url = new URL(apiUrl);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
    }
    catch (IOException e) {
        e.printStackTrace();
    }
}

When testing in Postman, the Event Grid Trigger will work and the Function will show as successful, however, my indexers do not run. I, instead, get the 503 Service Temporarily Unavailable error. I thought this meant that it would not work if I was in a free tier, but on the Basic tier, it still shows the same message.

I think the issue lies in the Azure Function code not reaching the api url? I put in debug statements in my app code, which I do not see printed.


Solution

  • I've just done a workaround (which is actually a more direct route). Ideally, for my uses I'd like to reach the URL to run the indexers, but I just ran the indexers directly from the Azure function.

    public class Function {
        @FunctionName("RunIndexers")
        public void runIndexers(@EventGridTrigger(name="event")String content,
        final ExecutionContext context) 
        {
             String endpoint = System.getenv("azure_endpoint");
             String key = System.getenv("azure_key");
    
             SearchIndexerClient client = new SearchIndexerClientBuilder()
                 .endpoint(endpoint)
                 .credential(new AzureKeyCredential(key))
                 .buildClient();
    
    
             Response<Void> response = client.runIndexerWithResponse("my-indexer", new Context(Context.NONE, null);
             context.getLogger().info("status code: " + response.getStatusCode();
        }
             
    }