From 12085cd79770ac4e0b715ef3df1b5e7431c1b23c Mon Sep 17 00:00:00 2001 From: Rabeea Abu-Saleh Date: Sun, 15 Sep 2019 12:13:53 +0300 Subject: [PATCH] Suggestion Example (#74) Added suggestion example --- sdk/search/azure-search-data/README.md | 1 + .../search/data/SearchSuggestionExample.java | 86 +++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 sdk/search/azure-search-data/src/samples/java/com/azure/search/data/SearchSuggestionExample.java diff --git a/sdk/search/azure-search-data/README.md b/sdk/search/azure-search-data/README.md index 1f7a3ba014e18..3d7aba0d76f91 100644 --- a/sdk/search/azure-search-data/README.md +++ b/sdk/search/azure-search-data/README.md @@ -41,6 +41,7 @@ The APIs documented in this section provide access to operations on search data, * [A simple search index example](/sdk/search/azure-search-data/src/samples/java/com/azure/search/data/SearchIndexClientExample.java). * [Handle a generic document search results](/sdk/search/azure-search-data/src/samples/java/com/azure/search/data/GenericDocumentSearchExample.java). * [Get a single generic document](/sdk/search/azure-search-data/src/samples/java/com/azure/search/data/GenericSingleDocumentGetExample.java). +* [Using suggestions](/sdk/search/azure-search-data/src/samples/java/com/azure/search/data/SearchSuggestionExample.java). ## How to provide feedback diff --git a/sdk/search/azure-search-data/src/samples/java/com/azure/search/data/SearchSuggestionExample.java b/sdk/search/azure-search-data/src/samples/java/com/azure/search/data/SearchSuggestionExample.java new file mode 100644 index 0000000000000..a548fd5505ad9 --- /dev/null +++ b/sdk/search/azure-search-data/src/samples/java/com/azure/search/data/SearchSuggestionExample.java @@ -0,0 +1,86 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.azure.search.data; + +import com.azure.core.http.rest.PagedIterable; +import com.azure.core.http.rest.PagedResponse; +import com.azure.search.data.common.SearchPipelinePolicy; +import com.azure.search.data.customization.SearchIndexClientBuilder; +import com.azure.search.data.generated.models.SuggestParameters; +import com.azure.search.data.generated.models.SuggestResult; + +import java.util.Iterator; +import java.util.List; + +public class SearchSuggestionExample { + + public static void main(String[] args) { + SearchIndexClient searchClient = getSearchClient(); + + SearchSuggestionHighlight(searchClient); + SearchSuggestionFuzzy(searchClient); + } + + private static void SearchSuggestionHighlight(SearchIndexClient searchClient) { + SuggestParameters suggestParams = new SuggestParameters() + .highlightPreTag("") + .highlightPostTag("") + .filter("Category eq 'Luxury'") + .top(1); + + PagedIterable suggestResult = + searchClient.suggest("hotel", "sg", suggestParams, null); + Iterator> iterator = suggestResult.iterableByPage().iterator(); + + List response = iterator.next().value(); + System.out.println("Received results with highlight:"); + response.forEach(r -> System.out.println(r.text())); + + /** Output: + * Received results with highlight: + * Best hotel in town if you like luxury hotels. They have an amazing infinity pool, a spa, and + * a really helpful concierge. The location is perfect -- right downtown, close to all the tourist + * attractions. We highly recommend this hotel. + **/ + } + + private static void SearchSuggestionFuzzy(SearchIndexClient searchClient) { + SuggestParameters suggestParams = new SuggestParameters() + .useFuzzyMatching(true); + + PagedIterable suggestResult = + searchClient.suggest("hitel", "sg", suggestParams, null); + Iterator> iterator = suggestResult.iterableByPage().iterator(); + + List response = iterator.next().value(); + System.out.println("Received results with fuzzy option:"); + response.forEach(r -> System.out.println(r.text())); + + /** Output: + * Received results with fuzzy option: + * Countryside Hotel + * Pretty good hotel + * Another good hotel + * Very popular hotel in town + * Cheapest hotel in town. Infact, a motel. + **/ + } + + private static SearchIndexClient getSearchClient() { + String searchServiceName = ""; + String apiKey = ""; + String dnsSuffix = "search.windows.net"; + String indexName = "hotels"; + String apiVersion = "2019-05-06"; + + return new SearchIndexClientBuilder() + .serviceName(searchServiceName) + .searchDnsSuffix(dnsSuffix) + .indexName(indexName) + .apiVersion(apiVersion) + .addPolicy(new SearchPipelinePolicy(apiKey)) + .buildClient(); + } + +}