Skip to content

Commit 158aa57

Browse files
authored
Merge pull request #2 from KarlErickson/bmitchell287-brendm-semantic-6519
restored text from JS version; fixed var statements; trimmed whitespace
2 parents 7ceff61 + e27df81 commit 158aa57

File tree

1 file changed

+85
-52
lines changed

1 file changed

+85
-52
lines changed

articles/search/includes/quickstarts/semantic-ranker-java.md

Lines changed: 85 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ The quickstart assumes the following is available on your computer:
3333
```xml
3434
<project xmlns="http://maven.apache.org/POM/4.0.0"
3535
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
36-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
36+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
3737
http://maven.apache.org/xsd/maven-4.0.0.xsd">
3838
<modelVersion>4.0.0</modelVersion>
3939

@@ -103,7 +103,7 @@ import java.util.Properties;
103103
104104
public class SearchConfig {
105105
private static final Properties properties = new Properties();
106-
106+
107107
static {
108108
try (InputStream input = SearchConfig.class.getClassLoader()
109109
.getResourceAsStream("application.properties")) {
@@ -113,17 +113,17 @@ public class SearchConfig {
113113
"Failed to load application.properties", e);
114114
}
115115
}
116-
117-
public static final String SEARCH_ENDPOINT =
116+
117+
public static final String SEARCH_ENDPOINT =
118118
properties.getProperty("azure.search.endpoint");
119-
public static final String INDEX_NAME =
119+
public static final String INDEX_NAME =
120120
properties.getProperty("azure.search.index.name");
121-
public static final String SEMANTIC_CONFIG_NAME =
121+
public static final String SEMANTIC_CONFIG_NAME =
122122
properties.getProperty("semantic.configuration.name");
123-
124-
public static final DefaultAzureCredential CREDENTIAL =
123+
124+
public static final DefaultAzureCredential CREDENTIAL =
125125
new DefaultAzureCredentialBuilder().build();
126-
126+
127127
static {
128128
System.out.println("Using Azure Search endpoint: " + SEARCH_ENDPOINT);
129129
System.out.println("Using index name: " + INDEX_NAME + "\n");
@@ -144,6 +144,8 @@ In this section, you get settings for the existing `hotels-sample-index` index o
144144
import com.azure.search.documents.indexes.models.SearchField;
145145
import com.azure.search.documents.indexes.models.SearchIndex;
146146
import com.azure.search.documents.indexes.models.SemanticConfiguration;
147+
import com.azure.search.documents.indexes.models.SemanticField;
148+
import com.azure.search.documents.indexes.models.SemanticSearch;
147149
148150
public class GetIndexSettings {
149151
public static void main(String[] args) {
@@ -164,17 +166,17 @@ In this section, you get settings for the existing `hotels-sample-index` index o
164166
field.getName(), field.getType(), field.isSearchable());
165167
}
166168
167-
var semanticSearch = index.getSemanticSearch();
168-
if (semanticSearch != null &&
169+
SemanticSearch semanticSearch = index.getSemanticSearch();
170+
if (semanticSearch != null &&
169171
semanticSearch.getConfigurations() != null) {
170-
System.out.println("Semantic search configurations: " +
172+
System.out.println("Semantic search configurations: " +
171173
semanticSearch.getConfigurations().size());
172-
for (SemanticConfiguration config :
174+
for (SemanticConfiguration config :
173175
semanticSearch.getConfigurations()) {
174176
System.out.println("Configuration name: " + config.getName());
175-
var titleField = config.getPrioritizedFields().getTitleField();
177+
SemanticField titleField = config.getPrioritizedFields().getTitleField();
176178
if (titleField != null) {
177-
System.out.println("Title field: " +
179+
System.out.println("Title field: " +
178180
titleField.getFieldName());
179181
}
180182
}
@@ -221,7 +223,7 @@ In this section, you get settings for the existing `hotels-sample-index` index o
221223
.credential(SearchConfig.CREDENTIAL)
222224
.buildClient();
223225
224-
SearchIndex existingIndex =
226+
SearchIndex existingIndex =
225227
indexClient.getIndex(SearchConfig.INDEX_NAME);
226228
227229
// Create prioritized fields for semantic configuration
@@ -234,13 +236,14 @@ In this section, you get settings for the existing `hotels-sample-index` index o
234236
SearchConfig.SEMANTIC_CONFIG_NAME, prioritizedFields);
235237
236238
// Add the semantic configuration to the index
237-
var semanticSearch = existingIndex.getSemanticSearch();
239+
SemanticSearch semanticSearch = existingIndex.getSemanticSearch();
238240
if (semanticSearch == null) {
239241
semanticSearch = new SemanticSearch();
240242
existingIndex.setSemanticSearch(semanticSearch);
241243
}
242244
243-
var configurations = semanticSearch.getConfigurations();
245+
List<SemanticConfiguration> configurations =
246+
semanticSearch.getConfigurations();
244247
if (configurations == null) {
245248
configurations = new ArrayList<>();
246249
semanticSearch.setConfigurations(configurations);
@@ -257,36 +260,38 @@ In this section, you get settings for the existing `hotels-sample-index` index o
257260
258261
indexClient.createOrUpdateIndex(existingIndex);
259262
260-
SearchIndex updatedIndex =
263+
SearchIndex updatedIndex =
261264
indexClient.getIndex(SearchConfig.INDEX_NAME);
262265
263266
System.out.println("Semantic configurations:");
264267
System.out.println("-".repeat(40));
265268
266-
var updatedSemanticSearch = updatedIndex.getSemanticSearch();
267-
if (updatedSemanticSearch != null &&
269+
SemanticSearch updatedSemanticSearch =
270+
updatedIndex.getSemanticSearch();
271+
if (updatedSemanticSearch != null &&
268272
updatedSemanticSearch.getConfigurations() != null) {
269-
for (SemanticConfiguration config :
273+
for (SemanticConfiguration config :
270274
updatedSemanticSearch.getConfigurations()) {
271275
System.out.println("Configuration name: " + config.getName());
272276
273-
var fields = config.getPrioritizedFields();
277+
SemanticPrioritizedFields fields =
278+
config.getPrioritizedFields();
274279
if (fields.getTitleField() != null) {
275-
System.out.println("Title field: " +
280+
System.out.println("Title field: " +
276281
fields.getTitleField().getFieldName());
277282
}
278283
if (fields.getKeywordsFields() != null) {
279-
var keywords = fields.getKeywordsFields().stream()
284+
List<String> keywords = fields.getKeywordsFields().stream()
280285
.map(SemanticField::getFieldName)
281286
.toList();
282-
System.out.println("Keywords fields: " +
287+
System.out.println("Keywords fields: " +
283288
String.join(", ", keywords));
284289
}
285290
if (fields.getContentFields() != null) {
286-
var content = fields.getContentFields().stream()
291+
List<String> content = fields.getContentFields().stream()
287292
.map(SemanticField::getFieldName)
288293
.toList();
289-
System.out.println("Content fields: " +
294+
System.out.println("Content fields: " +
290295
String.join(", ", content));
291296
}
292297
System.out.println("-".repeat(40));
@@ -299,7 +304,7 @@ In this section, you get settings for the existing `hotels-sample-index` index o
299304
300305
System.exit(0);
301306
} catch (Exception e) {
302-
System.err.println("Error updating semantic configuration: " +
307+
System.err.println("Error updating semantic configuration: " +
303308
e.getMessage());
304309
}
305310
}
@@ -357,8 +362,8 @@ Once the `hotels-sample-index` index has a semantic configuration, you can run q
357362
System.out.printf(" Re-ranker Score: %.2f%n", rerankerScore);
358363
System.out.printf(" HotelId: %s%n", document.get("HotelId"));
359364
System.out.printf(" HotelName: %s%n", document.get("HotelName"));
360-
System.out.printf(" Description: %s%n%n",
361-
document.get("Description") != null ?
365+
System.out.printf(" Description: %s%n%n",
366+
document.get("Description") != null ?
362367
document.get("Description") : "N/A");
363368
}
364369
@@ -387,12 +392,15 @@ Optionally, you can add captions to extract portions of the text and apply hit h
387392
import com.azure.search.documents.SearchClientBuilder;
388393
import com.azure.search.documents.SearchDocument;
389394
import com.azure.search.documents.models.QueryCaption;
395+
import com.azure.search.documents.models.QueryCaptionResult;
390396
import com.azure.search.documents.models.QueryCaptionType;
391397
import com.azure.search.documents.models.QueryType;
392398
import com.azure.search.documents.models.SearchOptions;
393399
import com.azure.search.documents.models.SearchResult;
394400
import com.azure.search.documents.models.SemanticSearchOptions;
395401
import com.azure.search.documents.util.SearchPagedIterable;
402+
403+
import java.util.List;
396404
397405
public class SemanticQueryWithCaptions {
398406
public static void main(String[] args) {
@@ -402,7 +410,7 @@ Optionally, you can add captions to extract portions of the text and apply hit h
402410
.credential(SearchConfig.CREDENTIAL)
403411
.buildClient();
404412
405-
System.out.println("Using semantic configuration: " +
413+
System.out.println("Using semantic configuration: " +
406414
SearchConfig.SEMANTIC_CONFIG_NAME);
407415
System.out.println("Search query: walking distance to live music");
408416
@@ -427,22 +435,23 @@ Optionally, you can add captions to extract portions of the text and apply hit h
427435
System.out.printf("Search result #%d:%n", rowNumber++);
428436
System.out.printf(" Re-ranker Score: %.2f%n", rerankerScore);
429437
System.out.printf(" HotelName: %s%n", document.get("HotelName"));
430-
System.out.printf(" Description: %s%n%n",
431-
document.get("Description") != null ?
438+
System.out.printf(" Description: %s%n%n",
439+
document.get("Description") != null ?
432440
document.get("Description") : "N/A");
433441
434442
// Handle captions
435-
var captions = result.getSemanticSearch().getQueryCaptions();
443+
List<QueryCaptionResult> captions =
444+
result.getSemanticSearch().getQueryCaptions();
436445
if (captions != null && !captions.isEmpty()) {
437-
var caption = captions.get(0);
446+
QueryCaptionResult caption = captions.get(0);
438447
439-
if (caption.getHighlights() != null &&
448+
if (caption.getHighlights() != null &&
440449
!caption.getHighlights().trim().isEmpty()) {
441-
System.out.printf(" Caption with highlights: %s%n",
450+
System.out.printf(" Caption with highlights: %s%n",
442451
caption.getHighlights());
443-
} else if (caption.getText() != null &&
452+
} else if (caption.getText() != null &&
444453
!caption.getText().trim().isEmpty()) {
445-
System.out.printf(" Caption text: %s%n",
454+
System.out.printf(" Caption text: %s%n",
446455
caption.getText());
447456
} else {
448457
System.out.println(
@@ -465,13 +474,24 @@ Optionally, you can add captions to extract portions of the text and apply hit h
465474
mvn compile exec:java -Dexec.mainClass="com.azure.search.quickstart.SemanticQueryWithCaptions"
466475
```
467476
468-
1. Output should include caption elements alongside search fields. Captions extract the most relevant passages from results, helpful for extracting interesting sentences from larger text chunks.
477+
1. Output should include a new caption element alongside search field. Captions are the most relevant passages in a result. If your index includes larger chunks of text, a caption is helpful for extracting the most interesting sentences.
478+
479+
```console
480+
Search result #1:
481+
Re-ranker Score: 2.613231658935547
482+
HotelName: Uptown Chic Hotel
483+
Description: Chic hotel near the city. High-rise hotel in downtown, within walking distance to theaters, art galleries, restaurants and shops. Visit Seattle Art Museum by day, and then head over to Benaroya Hall to catch the evening's concert performance.
484+
485+
Caption with highlights: Chic hotel near the city. High-rise hotel in downtown, within walking distance to<em> theaters, </em>art galleries, restaurants and shops. Visit<em> Seattle Art Museum </em>by day, and then head over to<em> Benaroya Hall </em>to catch the evening's concert performance.
486+
```
469487
470488
### Return semantic answers
471489
472490
In this final query, return semantic answers.
473491
474-
Semantic ranker can produce an answer to a query string that has the characteristics of a question. The generated answer is extracted verbatim from your content. If potential answers fail to meet a confidence threshold, the model doesn't return an answer.
492+
Semantic ranker can produce an answer to a query string that has the characteristics of a question. The generated answer is extracted verbatim from your content so it won't include composed content like what you might expect from a chat completion model.
493+
494+
To produce a semantic answer, the question and answer must be closely aligned, and the model must find content that clearly answers the question. If potential answers fail to meet a confidence threshold, the model doesn't return an answer. For demonstration purposes, the question in this example is designed to get a response so that you can see the syntax.
475495
476496
1. Create a file in `src/main/java/com/azure/search/quickstart` called `SemanticAnswer.java`.
477497
@@ -484,13 +504,16 @@ Semantic ranker can produce an answer to a query string that has the characteris
484504
import com.azure.search.documents.models.QueryAnswerResult;
485505
import com.azure.search.documents.models.QueryAnswerType;
486506
import com.azure.search.documents.models.QueryCaption;
507+
import com.azure.search.documents.models.QueryCaptionResult;
487508
import com.azure.search.documents.models.QueryCaptionType;
488509
import com.azure.search.documents.models.QueryType;
489510
import com.azure.search.documents.models.SearchOptions;
490511
import com.azure.search.documents.models.SearchResult;
491512
import com.azure.search.documents.models.SemanticSearchOptions;
492513
import com.azure.search.documents.util.SearchPagedIterable;
493514
515+
import java.util.List;
516+
494517
public class SemanticAnswer {
495518
public static void main(String[] args) {
496519
var searchClient = new SearchClientBuilder()
@@ -514,17 +537,18 @@ Semantic ranker can produce an answer to a query string that has the characteris
514537
System.out.println("Answers:\n");
515538
516539
// Extract semantic answers
517-
var semanticAnswers = results.getSemanticResults().getQueryAnswers();
540+
List<QueryAnswerResult> semanticAnswers =
541+
results.getSemanticResults().getQueryAnswers();
518542
int answerNumber = 1;
519543
520544
if (semanticAnswers != null) {
521545
for (QueryAnswerResult answer : semanticAnswers) {
522-
System.out.printf("Semantic answer result #%d:%n",
546+
System.out.printf("Semantic answer result #%d:%n",
523547
answerNumber++);
524548
525-
if (answer.getHighlights() != null &&
549+
if (answer.getHighlights() != null &&
526550
!answer.getHighlights().trim().isEmpty()) {
527-
System.out.printf("Semantic Answer: %s%n",
551+
System.out.printf("Semantic Answer: %s%n",
528552
answer.getHighlights());
529553
} else {
530554
System.out.printf("Semantic Answer: %s%n", answer.getText());
@@ -545,14 +569,15 @@ Semantic ranker can produce an answer to a query string that has the characteris
545569
System.out.printf("Search result #%d:%n", rowNumber++);
546570
System.out.printf("Re-ranker Score: %.2f%n", rerankerScore);
547571
System.out.printf("Hotel: %s%n", document.get("HotelName"));
548-
System.out.printf("Description: %s%n",
549-
document.get("Description") != null ?
572+
System.out.printf("Description: %s%n",
573+
document.get("Description") != null ?
550574
document.get("Description") : "N/A");
551575
552-
var captions = result.getSemanticSearch().getQueryCaptions();
576+
List<QueryCaptionResult> captions =
577+
result.getSemanticSearch().getQueryCaptions();
553578
if (captions != null && !captions.isEmpty()) {
554-
var caption = captions.get(0);
555-
if (caption.getHighlights() != null &&
579+
QueryCaptionResult caption = captions.get(0);
580+
if (caption.getHighlights() != null &&
556581
!caption.getHighlights().trim().isEmpty()) {
557582
System.out.printf("Caption: %s%n%n",
558583
caption.getHighlights());
@@ -575,4 +600,12 @@ Semantic ranker can produce an answer to a query string that has the characteris
575600
mvn compile exec:java -Dexec.mainClass="com.azure.search.quickstart.SemanticAnswer"
576601
```
577602
578-
1. Output should show semantic answers extracted verbatim from your content. Recall that answers are *verbatim content* pulled from your index. To get *composed answers* as generated by a chat completion model, consider using a [RAG pattern](../../retrieval-augmented-generation-overview.md) or [agentic retrieval](../../search-agentic-retrieval-concept.md).
603+
1. Output should look similar to the following example, where the best answer to question is pulled from one of the results.
604+
605+
Recall that answers are *verbatim content* pulled from your index and might be missing phrases that a user would expect to see. To get *composed answers* as generated by a chat completion model, considering using a [RAG pattern](../../retrieval-augmented-generation-overview.md) or [agentic retrieval](../../search-agentic-retrieval-concept.md).
606+
607+
```console
608+
Semantic answer result #1:
609+
Semantic Answer: Nature is Home on the beach. Explore the shore by day, and then come home to our shared living space to relax around a stone fireplace, sip something warm, and explore the<em> library </em>by night. Save up to 30 percent. Valid Now through the end of the year. Restrictions and blackouts may apply.
610+
Semantic Answer Score: 0.9829999804496765
611+
```

0 commit comments

Comments
 (0)