Skip to content

Commit

Permalink
Merge pull request Azure#48 from navalev/feature/TestCanUseHitHighlig…
Browse files Browse the repository at this point in the history
…hting

UT for canUseHitHighlighting
  • Loading branch information
itye-msft authored Sep 3, 2019
2 parents 7e5f22f + 43b4b4c commit 3bf90be
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,49 @@ public void canSearchWithMinimumCoverage() {
.verifyComplete();
}

@Override
public void canUseHitHighlighting() {
//arrange
String description = "Description";
String category = "Category";

SearchParameters sp = new SearchParameters();
sp.filter("Rating eq 5");
sp.highlightPreTag("<b>");
sp.highlightPostTag("</b>");
sp.highlightFields(Arrays.asList(category, description));

//act
PagedFlux<SearchResult> results = client.search("luxury hotel", sp, new SearchRequestOptions());

//sanity
Assert.assertNotNull(results);

List<SearchResult> documents = results.log().collectList().block();

// sanity
Assert.assertEquals(1, documents.size());
Map<String, List<String>> highlights = documents.get(0).highlights();
Assert.assertNotNull(highlights);
Assert.assertEquals(2, highlights.keySet().size());
Assert.assertTrue(highlights.containsKey(description));
Assert.assertTrue(highlights.containsKey(category));

String categoryHighlight = highlights.get(category).get(0);

//asserts
Assert.assertEquals("<b>Luxury</b>", categoryHighlight);

// Typed as IEnumerable so we get the right overload of Assert.Equals below.
List<String> expectedDescriptionHighlights =
Arrays.asList(
"Best <b>hotel</b> in town if you like <b>luxury</b> <b>hotels</b>.",
"We highly recommend this <b>hotel</b>."
);

Assert.assertEquals(expectedDescriptionHighlights, highlights.get(description));
}

private void assertResponse(SearchPagedResponse response, List<Map<String, Object>> actualResults) {
Assert.assertNull(response.count());
Assert.assertNull(response.coverage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,50 @@ public void canSearchWithMinimumCoverage() {
Assert.assertEquals(100.0, ((SearchPagedResponse) resultsIterator.next()).coverage(), 0);
}

@Override
public void canUseHitHighlighting() {
//arrange
String description = "Description";
String category = "Category";

SearchParameters sp = new SearchParameters();
sp.filter("Rating eq 5");
sp.highlightPreTag("<b>");
sp.highlightPostTag("</b>");
sp.highlightFields(Arrays.asList(category, description));

//act
PagedIterable<SearchResult> results = client.search("luxury hotel", sp, new SearchRequestOptions());

//sanity
Assert.assertNotNull(results);
Iterator<PagedResponse<SearchResult>> iterator = results.iterableByPage().iterator();
PagedResponse<SearchResult> result = iterator.next();
List<SearchResult> documents = result.items();

// sanity
Assert.assertEquals(1, documents.size());
Map<String, List<String>> highlights = documents.get(0).highlights();
Assert.assertNotNull(highlights);
Assert.assertEquals(2, highlights.keySet().size());
Assert.assertTrue(highlights.containsKey(description));
Assert.assertTrue(highlights.containsKey(category));

String categoryHighlight = highlights.get(category).get(0);

//asserts
Assert.assertEquals("<b>Luxury</b>", categoryHighlight);

// Typed as IEnumerable so we get the right overload of Assert.Equals below.
List<String> expectedDescriptionHighlights =
Arrays.asList(
"Best <b>hotel</b> in town if you like <b>luxury</b> <b>hotels</b>.",
"We highly recommend this <b>hotel</b>."
);

Assert.assertEquals(expectedDescriptionHighlights, highlights.get(description));
}

private List<Map<String, Object>> getSearchResults(PagedIterable<SearchResult> results) {
Iterator<PagedResponse<SearchResult>> iterator = results.iterableByPage().iterator();
List<Map<String, Object>> searchResults = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,9 @@ public void searchThrowsWhenSpecialCharInRegexIsUnescaped() {
@Test
public abstract void searchWithScoringProfileBoostsScore();

@Test
public abstract void canUseHitHighlighting();

abstract void search(String searchText, SearchParameters searchParameters, SearchRequestOptions searchRequestOptions);

abstract void indexDocuments(List<IndexAction> indexActions);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"networkCallRecords" : [ {
"Method" : "POST",
"Uri" : "https://azs-sdk17747141b950.search.windows.net/indexes('hotels')/docs/search.index?api-version=2019-05-06",
"Headers" : {
"Content-Type" : "application/json; charset=utf-8"
},
"Response" : {
"Pragma" : "no-cache",
"retry-after" : "0",
"request-id" : "fb686a2f-6b0b-4e9f-a7e2-f0bdc24a8dc8",
"StatusCode" : "200",
"Date" : "Mon, 02 Sep 2019 08:48:23 GMT",
"Strict-Transport-Security" : "max-age=15724800; includeSubDomains",
"Cache-Control" : "no-cache",
"elapsed-time" : "143",
"OData-Version" : "4.0",
"Expires" : "-1",
"Content-Length" : "795",
"Body" : "{\"@odata.context\":\"https://azs-sdk17747141b950.search.windows.net/indexes('hotels')/$metadata#Collection(Microsoft.Azure.Search.V2019_05_06.IndexResult)\",\"value\":[{\"key\":\"1\",\"status\":true,\"errorMessage\":null,\"statusCode\":201},{\"key\":\"2\",\"status\":true,\"errorMessage\":null,\"statusCode\":201},{\"key\":\"3\",\"status\":true,\"errorMessage\":null,\"statusCode\":201},{\"key\":\"4\",\"status\":true,\"errorMessage\":null,\"statusCode\":201},{\"key\":\"5\",\"status\":true,\"errorMessage\":null,\"statusCode\":201},{\"key\":\"6\",\"status\":true,\"errorMessage\":null,\"statusCode\":201},{\"key\":\"7\",\"status\":true,\"errorMessage\":null,\"statusCode\":201},{\"key\":\"8\",\"status\":true,\"errorMessage\":null,\"statusCode\":201},{\"key\":\"9\",\"status\":true,\"errorMessage\":null,\"statusCode\":201},{\"key\":\"10\",\"status\":true,\"errorMessage\":null,\"statusCode\":201}]}",
"Preference-Applied" : "odata.include-annotations=\"*\"",
"Content-Type" : "application/json; odata.metadata=minimal"
}
}, {
"Method" : "POST",
"Uri" : "https://azs-sdk17747141b950.search.windows.net/indexes('hotels')/docs/search.post.search?api-version=2019-05-06",
"Headers" : {
"Content-Type" : "application/json; charset=utf-8"
},
"Response" : {
"Pragma" : "no-cache",
"retry-after" : "0",
"request-id" : "79e464f6-b536-4bdb-9cbf-8e80ffe42102",
"StatusCode" : "200",
"Date" : "Mon, 02 Sep 2019 08:48:26 GMT",
"Strict-Transport-Security" : "max-age=15724800; includeSubDomains",
"Cache-Control" : "no-cache",
"elapsed-time" : "54",
"OData-Version" : "4.0",
"Expires" : "-1",
"Content-Length" : "1260",
"Body" : "{\"@odata.context\":\"https://azs-sdk17747141b950.search.windows.net/indexes('hotels')/$metadata#docs(*)\",\"value\":[{\"@search.score\":0.60304105,\"@search.highlights\":{\"Description\":[\"Best <b>hotel</b> in town if you like <b>luxury</b> <b>hotels</b>.\",\"We highly recommend this <b>hotel</b>.\"],\"Category\":[\"<b>Luxury</b>\"]},\"HotelId\":\"1\",\"HotelName\":\"Fancy Stay\",\"Description\":\"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.\",\"Description_fr\":\"Meilleur h\\u00f4tel en ville si vous aimez les h\\u00f4tels de luxe. Ils ont une magnifique piscine \\u00e0 d\\u00e9bordement, un spa et un concierge tr\\u00e8s utile. L'emplacement est parfait \\u2013 en plein centre, \\u00e0 proximit\\u00e9 de toutes les attractions touristiques. Nous recommandons fortement cet h\\u00f4tel.\",\"Category\":\"Luxury\",\"Tags\":[\"pool\",\"view\",\"wifi\",\"concierge\"],\"ParkingIncluded\":false,\"SmokingAllowed\":false,\"LastRenovationDate\":\"2010-06-27T00:00:00Z\",\"Rating\":5,\"Location\":{\"type\":\"Point\",\"coordinates\":[-122.131577,47.678581],\"crs\":{\"type\":\"name\",\"properties\":{\"name\":\"EPSG:4326\"}}},\"Address\":null,\"Rooms\":[]}]}",
"Preference-Applied" : "odata.include-annotations=\"*\"",
"Content-Type" : "application/json; odata.metadata=minimal"
}
} ],
"variables" : [ ]
}

0 comments on commit 3bf90be

Please sign in to comment.