Search Documents (Azure Cognitive Search REST API) https://docs.microsoft.com/en-us/rest/api/searchservice/Search-Documents
J'étais un peu accro au fait que l'URL était différente lors d'une demande POST.
package hello;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import com.google.gson.JsonObject;
public class HelloAzurePostSearch {
public static void main(String[] args) throws Exception {
//Documentation officielle
// https://docs.microsoft.com/en-us/rest/api/searchservice/Search-Documents
// POST https://[service name].search.windows.net/indexes/[index name]/docs/search
// ?api-version=[api-version]
// Content-Type: application/json
// api-key: [admin or query key]
String serviceName = "{your service name}";
String indexName = "{your index name}";
String apiVersion = "2019-05-06";
String adminKey = "{your admin key}";
URIBuilder builder = new URIBuilder("https://" + serviceName + ".search.windows.net" //
+ "/indexes/" + indexName + "/docs" //
+ "/search" //← Notez que l'URL est différente entre GET et POST!
) //
.addParameter("api-version", apiVersion);
JsonObject requestBody = new JsonObject();
{
requestBody.addProperty("search", "*");
requestBody.addProperty("count", true);
}
CloseableHttpClient client = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(builder.build());
httpPost.addHeader("api-key", adminKey);
StringEntity requestEntity //
= new StringEntity(requestBody.toString(), ContentType.APPLICATION_JSON);
httpPost.setEntity(requestEntity);
CloseableHttpResponse response = client.execute(httpPost);
System.err.println(response.getStatusLine().getStatusCode());
// response body
System.err.println(EntityUtils.toString(response.getEntity()));
client.close();
}
}
Recommended Posts