Quickstart: Create an Azure Cognitive Search index in Java
Create a Java console application that creates, loads, and queries a search index using IntelliJ, Java 11 SDK, and the Azure Cognitive Search REST API. This article provides step-by-step instructions for creating the application. Alternatively, you can download and run the complete application.
If you don't have an Azure subscription, create a free account before you begin.
Prerequisites
We used the following software and services to build and test this quickstart:
Create an Azure Cognitive Search service or find an existing service under your current subscription. You can use a free service for this quickstart.
Get a key and URL
Calls to the service require a URL endpoint and an access key on every request. A search service is created with both, so if you added Azure Cognitive Search to your subscription, follow these steps to get the necessary information:
Sign in to the Azure portal, and in your search service Overview page, get the URL. An example endpoint might look like
https://mydemo.search.windows.net.In Settings > Keys, get an admin key for full rights on the service. There are two interchangeable admin keys, provided for business continuity in case you need to roll one over. You can use either the primary or secondary key on requests for adding, modifying, and deleting objects.
Every request sent to your service requires an API key. Having a valid key establishes trust, on a per request basis, between the application sending the request and the service that handles it.
Set up your environment
Begin by opening IntelliJ IDEA and setting up a new project.
Create the project
Open IntelliJ IDEA, and select Create New Project.
Select Maven.
In the Project SDK list, select the Java 11 SDK.
For GroupId and ArtifactId, enter
AzureSearchQuickstart.Accept the remaining defaults to open the project.
Specify Maven dependencies
Select File > Settings.
In the Settings window, select Build, Execution, Deployment > Build Tools > Maven > Importing.
Select the Import Maven projects automatically check box, and click OK to close the window. Maven plugins and other dependencies will now be automatically synchronized when you update the pom.xml file in the next step.
Open the pom.xml file and replace the contents with the following Maven configuration details. These include references to the Exec Maven Plugin and a JSON interface API
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>AzureSearchQuickstart</groupId> <artifactId>AzureSearchQuickstart</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <properties> <jackson.version>2.12.1</jackson.version> <auto-value.version>1.6.2</auto-value.version> <junit.version>5.4.2</junit.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <name>azuresearch-console</name> <url>http://maven.apache.org</url> <dependencies> <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>${jackson.version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>${jackson.version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.datatype</groupId> <artifactId>jackson-datatype-jdk8</artifactId> <version>${jackson.version}</version> </dependency> <dependency> <groupId>com.google.auto.value</groupId> <artifactId>auto-value-annotations</artifactId> <version>${auto-value.version}</version> </dependency> <dependency> <groupId>com.google.auto.value</groupId> <artifactId>auto-value</artifactId> <version>${auto-value.version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>com.azure</groupId> <artifactId>azure-search-documents</artifactId> <version>11.1.3</version> </dependency> </dependencies> <build> <plugins> <!--put generated source files to generated-sources--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> <configuration> <source>11</source> <target>11</target> </configuration> </plugin> <!-- For JUnit --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.1</version> </plugin> <!-- Add exec plugin to run demo program --> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>1.6.0</version> <executions> <execution> <goals> <goal>exec</goal> </goals> </execution> </executions> <configuration> <mainClass>com.microsoft.azure.search.samples.demo.App</mainClass> <cleanupDaemonThreads>false</cleanupDaemonThreads> </configuration> </plugin> </plugins> </build> </project>
Set up the project structure
Select File > Project Structure.
Select Modules, and expand the source tree to access the contents of the
src>mainfolder.In the
src>main>javafolder, add folders forcom,microsoft,azure,search,samples,demo. To do this, select thejavafolder, press Alt + Insert, and then enter the folder name.In the
src>main>resourcesfolder, addappandservicefolders.When you're done, the project tree should look like the following picture.
Click OK to close the window.
Add Azure Cognitive Search service information
In the Project window, expand the source tree to access the
src>main>resources>appfolder, and add aconfig.propertiesfile. To do this, select theappfolder, press Alt + Insert, select File, and then enter the file name.Copy the following settings into the new file and replace
<YOUR-SEARCH-SERVICE-NAME>,<YOUR-ADMIN-KEY>, and<YOUR-QUERY-KEY>with your service name and keys. If your service endpoint ishttps://mydemo.search.windows.net, the service name would be"mydemo".SearchServiceName=<YOUR-SEARCH-SERVICE-NAME> SearchServiceAdminKey=<YOUR-ADMIN-KEY> SearchServiceQueryKey=<YOUR-QUERY-KEY> IndexName=hotels-quickstart ApiVersion=2020-06-30
Add the main method
In the
src>main>java>appfolder, add anAppclass. To do this, select theappfolder, press Alt + Insert, select Java Class, and then enter the class name.Open the
Appclass and replace the content with the following code. This code contains themainmethod.The uncommented code reads the search service parameters and uses them to create an instance of the search service client. The search service client code will be added in the next section.
The commented code in this class will be uncommented in a later section of this quickstart.
package main.java.app; import main.java.service.SearchServiceClient; import java.io.IOException; import java.util.Properties; public class App { private static Properties loadPropertiesFromResource(String resourcePath) throws IOException { var inputStream = App.class.getResourceAsStream(resourcePath); var configProperties = new Properties(); configProperties.load(inputStream); return configProperties; } public static void main(String[] args) { try { var config = loadPropertiesFromResource("/app/config.properties"); var client = new SearchServiceClient( config.getProperty("SearchServiceName"), config.getProperty("SearchServiceAdminKey"), config.getProperty("SearchServiceQueryKey"), config.getProperty("ApiVersion"), config.getProperty("IndexName") ); //Uncomment the next 3 lines in the 1 - Create Index section of the quickstart // if(client.indexExists()){ client.deleteIndex();} // client.createIndex("/service/index.json"); // Thread.sleep(1000L); // wait a second to create the index //Uncomment the next 2 lines in the 2 - Load Documents section of the quickstart // client.uploadDocuments("/service/hotels.json"); // Thread.sleep(2000L); // wait 2 seconds for data to upload //Uncomment the following 5 search queries in the 3 - Search an index section of the quickstart // // Query 1 // client.logMessage("\n*QUERY 1****************************************************************"); // client.logMessage("Search for: Atlanta'"); // client.logMessage("Return: All fields'"); // client.searchPlus("Atlanta"); // // // Query 2 // client.logMessage("\n*QUERY 2****************************************************************"); // client.logMessage("Search for: Atlanta"); // client.logMessage("Return: HotelName, Tags, Address"); // SearchServiceClient.SearchOptions options2 = client.createSearchOptions(); // options2.select = "HotelName,Tags,Address"; // client.searchPlus("Atlanta", options2); // // //Query 3 // client.logMessage("\n*QUERY 3****************************************************************"); // client.logMessage("Search for: wifi & restaurant"); // client.logMessage("Return: HotelName, Description, Tags"); // SearchServiceClient.SearchOptions options3 = client.createSearchOptions(); // options3.select = "HotelName,Description,Tags"; // client.searchPlus("wifi,restaurant", options3); // // // Query 4 -filtered query // client.logMessage("\n*QUERY 4****************************************************************"); // client.logMessage("Search for: all"); // client.logMessage("Filter: Ratings greater than 4"); // client.logMessage("Return: HotelName, Rating"); // SearchServiceClient.SearchOptions options4 = client.createSearchOptions(); // options4.filter="Rating%20gt%204"; // options4.select = "HotelName,Rating"; // client.searchPlus("*",options4); // // // Query 5 - top 2 results, ordered by // client.logMessage("\n*QUERY 5****************************************************************"); // client.logMessage("Search for: boutique"); // client.logMessage("Get: Top 2 results"); // client.logMessage("Order by: Rating in descending order"); // client.logMessage("Return: HotelId, HotelName, Category, Rating"); // SearchServiceClient.SearchOptions options5 = client.createSearchOptions(); // options5.top=2; // options5.orderby = "Rating%20desc"; // options5.select = "HotelId,HotelName,Category,Rating"; // client.searchPlus("boutique", options5); } catch (Exception e) { System.err.println("Exception:" + e.getMessage()); e.printStackTrace(); } } }
Add the HTTP operations
In the
src>main>java>servicefolder, add anSearchServiceClientclass. To do this, select theservicefolder, press Alt + Insert, select Java Class, and then enter the class name.Open the
SearchServiceClientclass, and replace the contents with the following code. This code provides the HTTP operations required to use the Azure Cognitive Search REST API. Additional methods for creating an index, uploading documents, and querying the index will be added in a later section.package main.java.service; import javax.json.Json; import javax.net.ssl.HttpsURLConnection; import java.io.IOException; import java.io.StringReader; import java.net.HttpURLConnection; import java.net.URI; import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; import java.nio.charset.StandardCharsets; import java.util.Formatter; import java.util.function.Consumer; /* This class is responsible for implementing HTTP operations for creating the index, uploading documents and searching the data*/ public class SearchServiceClient { private final String _adminKey; private final String _queryKey; private final String _apiVersion; private final String _serviceName; private final String _indexName; private final static HttpClient client = HttpClient.newHttpClient(); public SearchServiceClient(String serviceName, String adminKey, String queryKey, String apiVersion, String indexName) { this._serviceName = serviceName; this._adminKey = adminKey; this._queryKey = queryKey; this._apiVersion = apiVersion; this._indexName = indexName; } private static HttpResponse<String> sendRequest(HttpRequest request) throws IOException, InterruptedException { logMessage(String.format("%s: %s", request.method(), request.uri())); return client.send(request, HttpResponse.BodyHandlers.ofString()); } private static URI buildURI(Consumer<Formatter> fmtFn) { Formatter strFormatter = new Formatter(); fmtFn.accept(strFormatter); String url = strFormatter.out().toString(); strFormatter.close(); return URI.create(url); } public static void logMessage(String message) { System.out.println(message); } public static boolean isSuccessResponse(HttpResponse<String> response) { try { int responseCode = response.statusCode(); logMessage("\n Response code = " + responseCode); if (responseCode == HttpURLConnection.HTTP_OK || responseCode == HttpURLConnection.HTTP_ACCEPTED || responseCode == HttpURLConnection.HTTP_NO_CONTENT || responseCode == HttpsURLConnection.HTTP_CREATED) { return true; } // We got an error var msg = response.body(); if (msg != null) { logMessage(String.format("\n MESSAGE: %s", msg)); } } catch (Exception e) { e.printStackTrace(); } return false; } public static HttpRequest httpRequest(URI uri, String key, String method, String contents) { contents = contents == null ? "" : contents; var builder = HttpRequest.newBuilder(); builder.uri(uri); builder.setHeader("content-type", "application/json"); builder.setHeader("api-key", key); switch (method) { case "GET": builder = builder.GET(); break; case "HEAD": builder = builder.GET(); break; case "DELETE": builder = builder.DELETE(); break; case "PUT": builder = builder.PUT(HttpRequest.BodyPublishers.ofString(contents)); break; case "POST": builder = builder.POST(HttpRequest.BodyPublishers.ofString(contents)); break; default: throw new IllegalArgumentException(String.format("Can't create request for method '%s'", method)); } return builder.build(); } }
Build the project
Verify that your project has the following structure.
Open the Maven tool window, and execute this maven goal:
verify exec:java
When processing completes, look for a BUILD SUCCESS message followed by a zero (0) exit code.
1 - Create index
The hotels index definition contains simple fields and one complex field. Examples of a simple field are "HotelName" or "Description". The "Address" field is a complex field because it has subfields, such as "Street Address" and "City". In this quickstart, the index definition is specified using JSON.
In the Project window, expand the source tree to access the
src>main>resources>servicefolder, and add anindex.jsonfile. To do this, select theappfolder, press Alt + Insert, select File, and then enter the file name.Open the
index.jsonfile and insert the following index definition.{ "name": "hotels-quickstart", "fields": [ { "name": "HotelId", "type": "Edm.String", "key": true, "filterable": true }, { "name": "HotelName", "type": "Edm.String", "searchable": true, "filterable": false, "sortable": true, "facetable": false }, { "name": "Description", "type": "Edm.String", "searchable": true, "filterable": false, "sortable": false, "facetable": false, "analyzer": "en.lucene" }, { "name": "Description_fr", "type": "Edm.String", "searchable": true, "filterable": false, "sortable": false, "facetable": false, "analyzer": "fr.lucene" }, { "name": "Category", "type": "Edm.String", "searchable": true, "filterable": true, "sortable": true, "facetable": true }, { "name": "Tags", "type": "Collection(Edm.String)", "searchable": true, "filterable": true, "sortable": false, "facetable": true }, { "name": "ParkingIncluded", "type": "Edm.Boolean", "filterable": true, "sortable": true, "facetable": true }, { "name": "LastRenovationDate", "type": "Edm.DateTimeOffset", "filterable": true, "sortable": true, "facetable": true }, { "name": "Rating", "type": "Edm.Double", "filterable": true, "sortable": true, "facetable": true }, { "name": "Address", "type": "Edm.ComplexType", "fields": [ { "name": "StreetAddress", "type": "Edm.String", "filterable": false, "sortable": false, "facetable": false, "searchable": true }, { "name": "City", "type": "Edm.String", "searchable": true, "filterable": true, "sortable": true, "facetable": true }, { "name": "StateProvince", "type": "Edm.String", "searchable": true, "filterable": true, "sortable": true, "facetable": true }, { "name": "PostalCode", "type": "Edm.String", "searchable": true, "filterable": true, "sortable": true, "facetable": true }, { "name": "Country", "type": "Edm.String", "searchable": true, "filterable": true, "sortable": true, "facetable": true } ] } ] }The index name will be "hotels-quickstart". Attributes on the index fields determine how the indexed data can be searched in an application. For example, the
IsSearchableattribute must be assigned to every field that should be included in a full text search. To learn more about attributes, see Create Index (REST).The
Descriptionfield in this index uses the optionalanalyzerproperty to override the default Lucene language analyzer. TheDescription_frfield is using the French Lucene analyzerfr.lucenebecause it stores French text. TheDescriptionis using the optional Microsoft language analyzer en.lucene. To learn more about analyzers, see Analyzers for text processing in Azure Cognitive Search.Add the following code to the
SearchServiceClientclass. These methods build Azure Cognitive Search REST service URLs that create and delete an index, and that determine if an index exists. The methods also make the HTTP request.public boolean indexExists() throws IOException, InterruptedException { logMessage("\n Checking if index exists..."); var uri = buildURI(strFormatter -> strFormatter.format( "https://%s.search.windows.net/indexes/%s/docs?api-version=%s&search=*", _serviceName,_indexName,_apiVersion)); var request = httpRequest(uri, _adminKey, "HEAD", ""); var response = sendRequest(request); return isSuccessResponse(response); } public boolean deleteIndex() throws IOException, InterruptedException { logMessage("\n Deleting index..."); var uri = buildURI(strFormatter -> strFormatter.format( "https://%s.search.windows.net/indexes/%s?api-version=%s", _serviceName,_indexName,_apiVersion)); var request = httpRequest(uri, _adminKey, "DELETE", "*"); var response = sendRequest(request); return isSuccessResponse(response); } public boolean createIndex(String indexDefinitionFile) throws IOException, InterruptedException { logMessage("\n Creating index..."); //Build the search service URL var uri = buildURI(strFormatter -> strFormatter.format( "https://%s.search.windows.net/indexes/%s?api-version=%s", _serviceName,_indexName,_apiVersion)); //Read in index definition file var inputStream = SearchServiceClient.class.getResourceAsStream(indexDefinitionFile); var indexDef = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8); //Send HTTP PUT request to create the index in the search service var request = httpRequest(uri, _adminKey, "PUT", indexDef); var response = sendRequest(request); return isSuccessResponse(response); }Uncomment the following code in the
Appclass. This code deletes the "hotels-quickstart" index, if it exists, and creates a new index based on the index definition in the "index.json" file.A one-second pause is inserted after the index creation request. This pause ensures that the index is created before you upload documents.
if (client.indexExists()) { client.deleteIndex();} client.createIndex("/service/index.json"); Thread.sleep(1000L); // wait a second to create the indexOpen the Maven tool window, and execute this maven goal:
verify exec:javaAs the code runs, look for a "Creating index" message followed by a 201 response code. This response code confirms that the index was created. The run should end with a BUILD SUCCESS message and a zero (0) exit code.
2 - Load documents
In the Project window, expand the source tree to access the
src>main>resources>servicefolder, and add anhotels.jsonfile. To do this, select theappfolder, press Alt + Insert, select File, and then enter the file name.Insert the following hotel documents into the file.
{ "value": [ { "@search.action": "upload", "HotelId": "1", "HotelName": "Secret Point Motel", "Description": "The hotel is ideally located on the main commercial artery of the city in the heart of New York. A few minutes away is Time's Square and the historic centre of the city, as well as other places of interest that make New York one of America's most attractive and cosmopolitan cities.", "Description_fr": "L'hôtel est idéalement situé sur la principale artère commerciale de la ville en plein cœur de New York. A quelques minutes se trouve la place du temps et le centre historique de la ville, ainsi que d'autres lieux d'intérêt qui font de New York l'une des villes les plus attractives et cosmopolites de l'Amérique.", "Category": "Boutique", "Tags": [ "pool", "air conditioning", "concierge" ], "ParkingIncluded": "false", "LastRenovationDate": "1970-01-18T00:00:00Z", "Rating": 3.60, "Address": { "StreetAddress": "677 5th Ave", "City": "New York", "StateProvince": "NY", "PostalCode": "10022", "Country": "USA" } }, { "@search.action": "upload", "HotelId": "2", "HotelName": "Twin Dome Motel", "Description": "The hotel is situated in a nineteenth century plaza, which has been expanded and renovated to the highest architectural standards to create a modern, functional and first-class hotel in which art and unique historical elements coexist with the most modern comforts.", "Description_fr": "L'hôtel est situé dans une place du XIXe siècle, qui a été agrandie et rénovée aux plus hautes normes architecturales pour créer un hôtel moderne, fonctionnel et de première classe dans lequel l'art et les éléments historiques uniques coexistent avec le confort le plus moderne.", "Category": "Boutique", "Tags": [ "pool", "free wifi", "concierge" ], "ParkingIncluded": "false", "LastRenovationDate": "1979-02-18T00:00:00Z", "Rating": 3.60, "Address": { "StreetAddress": "140 University Town Center Dr", "City": "Sarasota", "StateProvince": "FL", "PostalCode": "34243", "Country": "USA" } }, { "@search.action": "upload", "HotelId": "3", "HotelName": "Triple Landscape Hotel", "Description": "The Hotel stands out for its gastronomic excellence under the management of William Dough, who advises on and oversees all of the Hotel’s restaurant services.", "Description_fr": "L'hôtel est situé dans une place du XIXe siècle, qui a été agrandie et rénovée aux plus hautes normes architecturales pour créer un hôtel moderne, fonctionnel et de première classe dans lequel l'art et les éléments historiques uniques coexistent avec le confort le plus moderne.", "Category": "Resort and Spa", "Tags": [ "air conditioning", "bar", "continental breakfast" ], "ParkingIncluded": "true", "LastRenovationDate": "2015-09-20T00:00:00Z", "Rating": 4.80, "Address": { "StreetAddress": "3393 Peachtree Rd", "City": "Atlanta", "StateProvince": "GA", "PostalCode": "30326", "Country": "USA" } }, { "@search.action": "upload", "HotelId": "4", "HotelName": "Sublime Cliff Hotel", "Description": "Sublime Cliff Hotel is located in the heart of the historic center of Sublime in an extremely vibrant and lively area within short walking distance to the sites and landmarks of the city and is surrounded by the extraordinary beauty of churches, buildings, shops and monuments. Sublime Cliff is part of a lovingly restored 1800 palace.", "Description_fr": "Le sublime Cliff Hotel est situé au coeur du centre historique de sublime dans un quartier extrêmement animé et vivant, à courte distance de marche des sites et monuments de la ville et est entouré par l'extraordinaire beauté des églises, des bâtiments, des commerces et Monuments. Sublime Cliff fait partie d'un Palace 1800 restauré avec amour.", "Category": "Boutique", "Tags": [ "concierge", "view", "24-hour front desk service" ], "ParkingIncluded": "true", "LastRenovationDate": "1960-02-06T00:00:00Z", "Rating": 4.60, "Address": { "StreetAddress": "7400 San Pedro Ave", "City": "San Antonio", "StateProvince": "TX", "PostalCode": "78216", "Country": "USA" } } ] }Insert the following code into the
SearchServiceClientclass. This code builds the REST service URL to upload the hotel documents to the index, and then makes the HTTP POST request.public boolean uploadDocuments(String documentsFile) throws IOException, InterruptedException { logMessage("\n Uploading documents..."); //Build the search service URL var endpoint = buildURI(strFormatter -> strFormatter.format( "https://%s.search.windows.net/indexes/%s/docs/index?api-version=%s", _serviceName,_indexName,_apiVersion)); //Read in the data to index var inputStream = SearchServiceClient.class.getResourceAsStream(documentsFile); var documents = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8); //Send HTTP POST request to upload and index the data var request = httpRequest(endpoint, _adminKey, "POST", documents); var response = sendRequest(request); return isSuccessResponse(response); }Uncomment the following code in the
Appclass. This code uploads the documents in "hotels.json" to the index.client.uploadDocuments("/service/hotels.json"); Thread.sleep(2000L); // wait 2 seconds for data to uploadA two-second pause is inserted after the upload request to ensure that the document loading process completes before you query the index.
Open the Maven tool window, and execute this maven goal:
verify exec:javaBecause you created a "hotels-quickstart" index in the previous step, the code will now delete it and recreate it again before loading the hotel documents.
As the code runs, look for an "Uploading documents" message followed by a 200 response code. This response code confirms that the documents were uploaded to the index. The run should end with a BUILD SUCCESS message and a zero (0) exit code.
3 - Search an index
Now that you've loaded the hotels documents, you can create search queries to access the hotels data.
Add the following code to the
SearchServiceClientclass. This code builds Azure Cognitive Search REST service URLs to search the indexed data and prints the search results.The
SearchOptionsclass andcreateSearchOptionsmethod let you specify a subset of the available Azure Cognitive Search REST API query options. For more information on the REST API query options, see Search Documents (Azure Cognitive Search REST API).The
SearchPlusmethod creates the search query URL, makes the search request, and then prints the results to the console.public SearchOptions createSearchOptions() { return new SearchOptions();} //Defines available search parameters that can be set public static class SearchOptions { public String select = ""; public String filter = ""; public int top = 0; public String orderby= ""; } //Concatenates search parameters to append to the search request private String createOptionsString(SearchOptions options) { String optionsString = ""; if (options != null) { if (options.select != "") optionsString = optionsString + "&$select=" + options.select; if (options.filter != "") optionsString = optionsString + "&$filter=" + options.filter; if (options.top != 0) optionsString = optionsString + "&$top=" + options.top; if (options.orderby != "") optionsString = optionsString + "&$orderby=" +options.orderby; } return optionsString; } public void searchPlus(String queryString) { searchPlus( queryString, null); } public void searchPlus(String queryString, SearchOptions options) { try { String optionsString = createOptionsString(options); var uri = buildURI(strFormatter -> strFormatter.format( "https://%s.search.windows.net/indexes/%s/docs?api-version=%s&search=%s%s", _serviceName, _indexName, _apiVersion, queryString, optionsString)); var request = httpRequest(uri, _queryKey, "GET", null); var response = sendRequest(request); var jsonReader = Json.createReader(new StringReader(response.body())); var jsonArray = jsonReader.readObject().getJsonArray("value"); var resultsCount = jsonArray.size(); logMessage("Results:\nCount: " + resultsCount); for (int i = 0; i <= resultsCount - 1; i++) { logMessage(jsonArray.get(i).toString()); } jsonReader.close(); } catch (Exception e) { e.printStackTrace(); } }In the
Appclass, uncomment the following code. This code sets up five different queries, including the search text, query parameters, and data fields to return.// Query 1 client.logMessage("\n*QUERY 1****************************************************************"); client.logMessage("Search for: Atlanta"); client.logMessage("Return: All fields'"); client.searchPlus("Atlanta"); // Query 2 client.logMessage("\n*QUERY 2****************************************************************"); client.logMessage("Search for: Atlanta"); client.logMessage("Return: HotelName, Tags, Address"); SearchServiceClient.SearchOptions options2 = client.createSearchOptions(); options2.select = "HotelName,Tags,Address"; client.searchPlus("Atlanta", options2); //Query 3 client.logMessage("\n*QUERY 3****************************************************************"); client.logMessage("Search for: wifi & restaurant"); client.logMessage("Return: HotelName, Description, Tags"); SearchServiceClient.SearchOptions options3 = client.createSearchOptions(); options3.select = "HotelName,Description,Tags"; client.searchPlus("wifi,restaurant", options3); // Query 4 -filtered query client.logMessage("\n*QUERY 4****************************************************************"); client.logMessage("Search for: all"); client.logMessage("Filter: Ratings greater than 4"); client.logMessage("Return: HotelName, Rating"); SearchServiceClient.SearchOptions options4 = client.createSearchOptions(); options4.filter="Rating%20gt%204"; options4.select = "HotelName,Rating"; client.searchPlus("*",options4); // Query 5 - top 2 results, ordered by client.logMessage("\n*QUERY 5****************************************************************"); client.logMessage("Search for: boutique"); client.logMessage("Get: Top 2 results"); client.logMessage("Order by: Rating in descending order"); client.logMessage("Return: HotelId, HotelName, Category, Rating"); SearchServiceClient.SearchOptions options5 = client.createSearchOptions(); options5.top=2; options5.orderby = "Rating%20desc"; options5.select = "HotelId,HotelName,Category,Rating"; client.searchPlus("boutique", options5);There are two ways of matching terms in a query: full-text search, and filters. A full-text search query searches for one or more terms in
IsSearchablefields in your index. A filter is a boolean expression that is evaluated overIsFilterablefields in an index. You can use full-text search and filters together or separately.Open the Maven tool window, and execute this maven goal:
verify exec:javaLook for a summary of each query and its results. The run should complete with BUILD SUCCESS message and a zero (0) exit code.
Clean up resources
When you're working in your own subscription, at the end of a project, it's a good idea to remove the resources that you no longer need. Resources left running can cost you money. You can delete resources individually or delete the resource group to delete the entire set of resources.
You can find and manage resources in the portal, using the All resources or Resource groups link in the left-navigation pane.
If you are using a free service, remember that you are limited to three indexes, indexers, and data sources. You can delete individual items in the portal to stay under the limit.
Next steps
In this Java quickstart, you worked through a series of tasks to create an index, load it with documents, and run queries. If you are comfortable with the basic concepts, we recommend the following article that lists indexer operations in REST.