Skip to content

Commit 87e958b

Browse files
committed
java quickstart run and then explain
1 parent 9950860 commit 87e958b

9 files changed

+112
-85
lines changed

articles/search/includes/quickstarts/full-text-csharp.md

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -108,21 +108,21 @@ AzureKeyCredential credential = new AzureKeyCredential("<Your search service adm
108108
//AzureKeyCredential credential = new AzureKeyCredential("Your search service admin key");
109109
110110
// Create a SearchIndexClient to send create/delete index commands
111-
SearchIndexClient adminClient = new SearchIndexClient(serviceEndpoint, credential);
111+
SearchIndexClient searchIndexClient = new SearchIndexClient(serviceEndpoint, credential);
112112
113113
// Create a SearchClient to load and query documents
114114
string indexName = "hotels-quickstart";
115-
SearchClient srchclient = new SearchClient(serviceEndpoint, indexName, credential);
115+
SearchClient searchClient = new SearchClient(serviceEndpoint, indexName, credential);
116116
117117
// Delete index if it exists
118118
Console.WriteLine("{0}", "Deleting index...\n");
119-
DeleteIndexIfExists(indexName, adminClient);
119+
DeleteIndexIfExists(indexName, searchIndexClient);
120120
121121
// Create index
122122
Console.WriteLine("{0}", "Creating index...\n");
123-
CreateIndex(indexName, adminClient);
123+
CreateIndex(indexName, searchIndexClient);
124124
125-
SearchClient ingesterClient = adminClient.GetSearchClient(indexName);
125+
SearchClient ingesterClient = searchIndexClient.GetSearchClient(indexName);
126126
127127
// Load documents
128128
Console.WriteLine("{0}", "Uploading documents...\n");
@@ -134,23 +134,23 @@ AzureKeyCredential credential = new AzureKeyCredential("<Your search service adm
134134
135135
// Call the RunQueries method to invoke a series of queries
136136
Console.WriteLine("Starting queries...\n");
137-
RunQueries(srchclient);
137+
RunQueries(searchClient);
138138
139139
// End the program
140140
Console.WriteLine("{0}", "Complete. Press any key to end this program...\n");
141141
Console.ReadKey();
142142
}
143143
144144
// Delete the hotels-quickstart index to reuse its name
145-
private static void DeleteIndexIfExists(string indexName, SearchIndexClient adminClient)
145+
private static void DeleteIndexIfExists(string indexName, SearchIndexClient searchIndexClient)
146146
{
147-
adminClient.GetIndexNames();
147+
searchIndexClient.GetIndexNames();
148148
{
149-
adminClient.DeleteIndex(indexName);
149+
searchIndexClient.DeleteIndex(indexName);
150150
}
151151
}
152152
// Create hotels-quickstart index
153-
private static void CreateIndex(string indexName, SearchIndexClient adminClient)
153+
private static void CreateIndex(string indexName, SearchIndexClient searchIndexClient)
154154
{
155155
FieldBuilder fieldBuilder = new FieldBuilder();
156156
var searchFields = fieldBuilder.Build(typeof(Hotel));
@@ -160,7 +160,7 @@ AzureKeyCredential credential = new AzureKeyCredential("<Your search service adm
160160
var suggester = new SearchSuggester("sg", new[] { "HotelName", "Category", "Address/City", "Address/StateProvince" });
161161
definition.Suggesters.Add(suggester);
162162
163-
adminClient.CreateOrUpdateIndex(definition);
163+
searchIndexClient.CreateOrUpdateIndex(definition);
164164
}
165165
166166
// Upload documents in a single Upload request.
@@ -266,7 +266,7 @@ AzureKeyCredential credential = new AzureKeyCredential("<Your search service adm
266266
}
267267
268268
// Run queries, use WriteDocuments to print output
269-
private static void RunQueries(SearchClient srchclient)
269+
private static void RunQueries(SearchClient searchClient)
270270
{
271271
SearchOptions options;
272272
SearchResults<Hotel> response;
@@ -285,7 +285,7 @@ AzureKeyCredential credential = new AzureKeyCredential("<Your search service adm
285285
options.Select.Add("HotelName");
286286
options.Select.Add("Rating");
287287
288-
response = srchclient.Search<Hotel>("*", options);
288+
response = searchClient.Search<Hotel>("*", options);
289289
WriteDocuments(response);
290290
291291
// Query 2
@@ -301,7 +301,7 @@ AzureKeyCredential credential = new AzureKeyCredential("<Your search service adm
301301
options.Select.Add("HotelName");
302302
options.Select.Add("Rating");
303303
304-
response = srchclient.Search<Hotel>("hotels", options);
304+
response = searchClient.Search<Hotel>("hotels", options);
305305
WriteDocuments(response);
306306
307307
// Query 3
@@ -316,7 +316,7 @@ AzureKeyCredential credential = new AzureKeyCredential("<Your search service adm
316316
options.Select.Add("HotelName");
317317
options.Select.Add("Tags");
318318
319-
response = srchclient.Search<Hotel>("pool", options);
319+
response = searchClient.Search<Hotel>("pool", options);
320320
WriteDocuments(response);
321321
322322
// Query 4 - Use Facets to return a faceted navigation structure for a given query
@@ -334,22 +334,22 @@ AzureKeyCredential credential = new AzureKeyCredential("<Your search service adm
334334
options.Select.Add("HotelName");
335335
options.Select.Add("Category");
336336
337-
response = srchclient.Search<Hotel>("*", options);
337+
response = searchClient.Search<Hotel>("*", options);
338338
WriteDocuments(response);
339339
340340
// Query 5
341341
Console.WriteLine("Query #5: Look up a specific document...\n");
342342
343343
Response<Hotel> lookupResponse;
344-
lookupResponse = srchclient.GetDocument<Hotel>("3");
344+
lookupResponse = searchClient.GetDocument<Hotel>("3");
345345
346346
Console.WriteLine(lookupResponse.Value.HotelId);
347347
348348
349349
// Query 6
350350
Console.WriteLine("Query #6: Call Autocomplete on HotelName...\n");
351351
352-
var autoresponse = srchclient.Autocomplete("sa", "sg");
352+
var autoresponse = searchClient.Autocomplete("sa", "sg");
353353
WriteDocuments(autoresponse);
354354
355355
}
@@ -651,11 +651,11 @@ static void Main(string[] args)
651651
//AzureKeyCredential credential = new AzureKeyCredential("Your search service admin key");
652652
653653
// Create a SearchIndexClient to send create/delete index commands
654-
SearchIndexClient adminClient = new SearchIndexClient(serviceEndpoint, credential);
654+
SearchIndexClient searchIndexClient = new SearchIndexClient(serviceEndpoint, credential);
655655
656656
// Create a SearchClient to load and query documents
657657
string indexName = "hotels-quickstart";
658-
SearchClient srchclient = new SearchClient(serviceEndpoint, indexName, credential);
658+
SearchClient searchClient = new SearchClient(serviceEndpoint, indexName, credential);
659659
660660
// REDACTED FOR BREVITY . . .
661661
}
@@ -685,7 +685,7 @@ In *Program.cs*, you create a [SearchIndex](/dotnet/api/azure.search.documents.i
685685
686686
```csharp
687687
// Create hotels-quickstart index
688-
private static void CreateIndex(string indexName, SearchIndexClient adminClient)
688+
private static void CreateIndex(string indexName, SearchIndexClient searchIndexClient)
689689
{
690690
FieldBuilder fieldBuilder = new FieldBuilder();
691691
var searchFields = fieldBuilder.Build(typeof(Hotel));
@@ -695,7 +695,7 @@ private static void CreateIndex(string indexName, SearchIndexClient adminClient)
695695
var suggester = new SearchSuggester("sg", new[] { "HotelName", "Category", "Address/City", "Address/StateProvince" });
696696
definition.Suggesters.Add(suggester);
697697
698-
adminClient.CreateOrUpdateIndex(definition);
698+
searchIndexClient.CreateOrUpdateIndex(definition);
699699
}
700700
```
701701
@@ -741,10 +741,10 @@ private static void UploadDocuments(SearchClient searchClient)
741741
742742
Once you initialize the [IndexDocumentsBatch](/dotnet/api/azure.search.documents.models.indexdocumentsbatch-1) object, you can send it to the index by calling [IndexDocuments](/dotnet/api/azure.search.documents.searchclient.indexdocuments) on your [SearchClient](/dotnet/api/azure.search.documents.searchclient) object.
743743
744-
You load documents using SearchClient in `Main()`, but the operation also requires admin rights on the service, which is typically associated with SearchIndexClient. One way to set up this operation is to get SearchClient through `SearchIndexClient` (`adminClient` in this example).
744+
You load documents using SearchClient in `Main()`, but the operation also requires admin rights on the service, which is typically associated with SearchIndexClient. One way to set up this operation is to get SearchClient through `SearchIndexClient` (`searchIndexClient` in this example).
745745
746746
```csharp
747-
SearchClient ingesterClient = adminClient.GetSearchClient(indexName);
747+
SearchClient ingesterClient = searchIndexClient.GetSearchClient(indexName);
748748
749749
// Load documents
750750
Console.WriteLine("{0}", "Uploading documents...\n");
@@ -800,7 +800,7 @@ The `RunQueries` method executes queries and returns results. Results are Hotel
800800
801801
```csharp
802802
// Run queries, use WriteDocuments to print output
803-
private static void RunQueries(SearchClient srchclient)
803+
private static void RunQueries(SearchClient searchClient)
804804
{
805805
SearchOptions options;
806806
SearchResults<Hotel> response;
@@ -819,7 +819,7 @@ private static void RunQueries(SearchClient srchclient)
819819
options.Select.Add("HotelName");
820820
options.Select.Add("Address/City");
821821
822-
response = srchclient.Search<Hotel>("*", options);
822+
response = searchClient.Search<Hotel>("*", options);
823823
WriteDocuments(response);
824824
// REDACTED FOR BREVITY
825825
}
@@ -843,7 +843,7 @@ options.Select.Add("HotelId");
843843
options.Select.Add("HotelName");
844844
options.Select.Add("Rating");
845845
846-
response = srchclient.Search<Hotel>("hotels", options);
846+
response = searchClient.Search<Hotel>("hotels", options);
847847
WriteDocuments(response);
848848
```
849849
@@ -863,7 +863,7 @@ options.Select.Add("HotelId");
863863
options.Select.Add("HotelName");
864864
options.Select.Add("Tags");
865865
866-
response = srchclient.Search<Hotel>("pool", options);
866+
response = searchClient.Search<Hotel>("pool", options);
867867
WriteDocuments(response);
868868
```
869869
@@ -886,7 +886,7 @@ options.Select.Add("HotelId");
886886
options.Select.Add("HotelName");
887887
options.Select.Add("Category");
888888
889-
response = srchclient.Search<Hotel>("*", options);
889+
response = searchClient.Search<Hotel>("*", options);
890890
WriteDocuments(response);
891891
```
892892
@@ -899,7 +899,7 @@ In the fifth query, return a specific document. A document lookup is a typical r
899899
Console.WriteLine("Query #5: Look up a specific document...\n");
900900
901901
Response<Hotel> lookupResponse;
902-
lookupResponse = srchclient.GetDocument<Hotel>("3");
902+
lookupResponse = searchClient.GetDocument<Hotel>("3");
903903
904904
Console.WriteLine(lookupResponse.Value.HotelId);
905905
```
@@ -912,7 +912,7 @@ The last query shows the syntax for autocomplete, simulating a partial user inpu
912912
// Query 6
913913
Console.WriteLine("Query #6: Call Autocomplete on HotelName that starts with 'sa'...\n");
914914
915-
var autoresponse = srchclient.Autocomplete("sa", "sg");
915+
var autoresponse = searchClient.Autocomplete("sa", "sg");
916916
WriteDocuments(autoresponse);
917917
```
918918

articles/search/includes/quickstarts/full-text-java.md

Lines changed: 80 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -27,81 +27,110 @@ For the recommended keyless authentication with Microsoft Entra ID, you need to:
2727

2828
[!INCLUDE [resource authentication](../resource-authentication.md)]
2929

30-
## Set up your environment
30+
## Set up
3131

32-
Use the following tools to create this quickstart.
32+
The sample in this quickstart works with the Java Runtime. Install a Java Development Kit such as [Azul Zulu OpenJDK](https://www.azul.com/downloads/?package=jdk). The [Microsoft Build of OpenJDK](https://www.microsoft.com/openjdk) or your preferred JDK should also work.
3333

34-
+ [Visual Studio Code with the Java extension](https://code.visualstudio.com/docs/java/extensions)
34+
1. Install [Apache Maven](https://maven.apache.org/install.html). Then run `mvn -v` to confirm successful installation.
35+
1. Create a new `pom.xml` file in the root of your project, and copy the following code into it:
3536

36-
+ [Java 11 SDK](/java/azure/jdk/)
37-
38-
## Create the project
39-
40-
1. Start Visual Studio Code.
41-
42-
1. Open the [Command Palette](https://code.visualstudio.com/docs/getstarted/userinterface#_command-palette) by using **Ctrl+Shift+P**. Search for **Create Java Project**.
37+
```xml
38+
<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">
39+
<modelVersion>4.0.0</modelVersion>
40+
<groupId>azure.search.sample</groupId>
41+
<artifactId>azuresearchquickstart</artifactId>
42+
<version>1.0.0-SNAPSHOT</version>
43+
<build>
44+
<sourceDirectory>src</sourceDirectory>
45+
<plugins>
46+
<plugin>
47+
<artifactId>maven-compiler-plugin</artifactId>
48+
<version>3.7.0</version>
49+
<configuration>
50+
<source>1.8</source>
51+
<target>1.8</target>
52+
</configuration>
53+
</plugin>
54+
</plugins>
55+
</build>
56+
<dependencies>
57+
<dependency>
58+
<groupId>junit</groupId>
59+
<artifactId>junit</artifactId>
60+
<version>4.11</version>
61+
<scope>test</scope>
62+
</dependency>
63+
<dependency>
64+
<groupId>com.azure</groupId>
65+
<artifactId>azure-search-documents</artifactId>
66+
<version>11.7.3</version>
67+
</dependency>
68+
<dependency>
69+
<groupId>com.azure</groupId>
70+
<artifactId>azure-core</artifactId>
71+
<version>1.53.0</version>
72+
</dependency>
73+
<dependency>
74+
<groupId>com.azure</groupId>
75+
<artifactId>azure-identity</artifactId>
76+
<version>1.15.1</version>
77+
</dependency>
78+
</dependencies>
79+
</project>
80+
```
4381

44-
:::image type="content" source="../../media/search-get-started-java/java-quickstart-create-project.png" alt-text="Screenshot of a Java project." border="true":::
82+
1. Install the dependencies including the Azure AI Search client library ([Azure.Search.Documents](/java/api/overview/azure/search)) for Java and [Azure Identity client library for Java](https://mvnrepository.com/artifact/com.azure/azure-identity) with:
4583

46-
1. Select **Maven**.
84+
```console
85+
mvn clean dependency:copy-dependencies
86+
```
4787

48-
:::image type="content" source="../../media/search-get-started-java/java-quickstart-select-maven.png" alt-text="Screenshot of a maven project." border="true":::
88+
1. For the **recommended** keyless authentication with Microsoft Entra ID, sign in to Azure with the following command:
4989

50-
1. Select **maven-archetype-quickstart**.
90+
```console
91+
az login
92+
```
5193

52-
:::image type="content" source="../../media/search-get-started-java/java-quickstart-select-maven-project-type.png" alt-text="Screenshot of a maven quickstart project." border="true":::
94+
## Create, load, and query a search index
5395

54-
1. Select the latest version, currently **1.4**.
96+
In the prior [set up](#set-up) section, you created a new console application and installed the Azure AI Search client library.
5597

56-
:::image type="content" source="../../media/search-get-started-java/java-quickstart-group-id.png" alt-text="Screenshot of the group ID location." border="true":::
98+
In this section, you add code to create a search index, load it with documents, and run queries. You run the program to see the results in the console. For a detailed explanation of the code, see the [explaining the code](#explaining-the-code) section.
5799

58-
1. Enter **azure.search.sample** as the group ID.
100+
The sample code in this quickstart uses Microsoft Entra ID for authentication. If you prefer to use an API key, you can replace the `DefaultAzureCredential` object with a `AzureKeyCredential` object.
59101

60-
:::image type="content" source="../../media/search-get-started-java/java-quickstart-group-id.png" alt-text="Screenshot of the group ID for search." border="true":::
102+
#### [Microsoft Entra ID](#tab/keyless)
61103

62-
1. Enter **azuresearchquickstart** as the artifact ID.
104+
```java
105+
String searchServiceEndpoint = "https://<Put your search service NAME here>.search.windows.net/";
106+
DefaultAzureCredential credential = new DefaultAzureCredentialBuilder().build();
107+
```
63108

64-
:::image type="content" source="../../media/search-get-started-java/java-quickstart-artifact-id.png" alt-text="Screenshot of an artifact ID." border="true":::
109+
#### [API key](#tab/api-key)
65110

66-
1. Select the folder to create the project in.
111+
```java
112+
String searchServiceEndpoint = "https://<Put your search service NAME here>.search.windows.net/";
113+
AzureKeyCredential credential = new AzureKeyCredential("<Your search service admin key>");
114+
```
115+
---
67116

68-
1. Finish project creation in the [integrated terminal](https://code.visualstudio.com/docs/terminal/basics). Press enter to accept the default for "1.0-SNAPSHOT" and then type "y" to confirm the properties for your project.
117+
1. Create a new file named *App.java* in the same project root directory.
118+
1. Copy and paste the following code into *App.java*:
69119

70-
:::image type="content" source="../../media/search-get-started-java/java-quickstart-finish-setup-terminal.png" alt-text="Screenshot of the finished project definition." border="true":::
120+
```java
121+
122+
```
71123

72-
1. Open the folder you created the project in.
73124

74-
## Specify Maven dependencies
75125

76-
1. Open the *pom.xml* file and add the following dependencies. This includes the [Azure.Search.Documents](/java/api/overview/azure/search) library.
126+
1. Run your new console application:
77127

78-
```xml
79-
<dependencies>
80-
<dependency>
81-
<groupId>com.azure</groupId>
82-
<artifactId>azure-search-documents</artifactId>
83-
<version>11.7.3</version>
84-
</dependency>
85-
<dependency>
86-
<groupId>com.azure</groupId>
87-
<artifactId>azure-core</artifactId>
88-
<version>1.53.0</version>
89-
</dependency>
90-
<dependency>
91-
<groupId>junit</groupId>
92-
<artifactId>junit</artifactId>
93-
<version>4.11</version>
94-
<scope>test</scope>
95-
</dependency>
96-
</dependencies>
128+
```console
129+
javac Address.java App.java Hotel.java -cp ".;target\dependency\*"
130+
java -cp ".;target\dependency\*" App
97131
```
98132
99-
1. Change the compiler Java version to 11.
100133
101-
```xml
102-
<maven.compiler.source>1.11</maven.compiler.source>
103-
<maven.compiler.target>1.11</maven.compiler.target>
104-
```
105134
106135
## Create a search client
107136

0 commit comments

Comments
 (0)