Skip to content

Commit

Permalink
Search facet tests (Azure#42)
Browse files Browse the repository at this point in the history
* test canSearchWithRangeFacets, add RangeFacetResult class to deserialize facet results

* test canSearchWithValueFacets, add ValueFacetResult todeserialized value facet results

* javadoc

* cleanup
  • Loading branch information
navalev authored Aug 25, 2019
1 parent ce69fbf commit 1a4f15c
Show file tree
Hide file tree
Showing 10 changed files with 425 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.search.data.customization;

import com.azure.search.data.generated.models.FacetResult;

public class RangeFacetResult {
private Long count;
private Object from;
private Object to;

/**
* Constructor
* @param facetResult facet result object
*/
public RangeFacetResult(FacetResult facetResult) {
count = facetResult.count();
from = facetResult.additionalProperties().get("from");
to = facetResult.additionalProperties().get("to");
}

/**
* Get cont
* @return count
*/
public long count() {
return count;
}

/**
* Get from
* @return from
*/
public Object from() {
return from;
}

/**
* Get to
* @return to
*/
public Object to() {
return to;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.search.data.customization;

import com.azure.search.data.generated.models.FacetResult;

public class ValueFacetResult {
private Long count;
private Object value;

/**
* Constructor
* @param facetResult facet result object
*/
public ValueFacetResult(FacetResult facetResult) {
this.count = facetResult.count();
this.value = facetResult.additionalProperties().get("value");
}

/**
* Constructor
* @param count count
* @param value value
*/
public ValueFacetResult(Long count, Object value) {
this.count = count;
this.value = value;
}

/**
* Get count
* @return count
*/
public Long count() {
return count;
}

/**
* Get value
* @return value
*/
public Object value() {
return value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import com.azure.core.http.rest.PagedFlux;
import com.azure.core.http.rest.PagedResponse;
import com.azure.search.data.common.SearchPagedResponse;
import com.azure.search.data.customization.RangeFacetResult;
import com.azure.search.data.customization.ValueFacetResult;
import com.azure.search.data.generated.models.FacetResult;
import com.azure.search.data.generated.models.IndexAction;
import com.azure.search.data.generated.models.IndexBatch;
import com.azure.search.data.generated.models.QueryType;
Expand Down Expand Up @@ -178,6 +181,75 @@ public void canFilter() {
Assert.assertTrue(Arrays.asList("1", "5").containsAll(hotelIds));
}

@Override
public void canSearchWithRangeFacets() {
PagedFlux<SearchResult> results = client.search("*", getSearchParametersForRangeFacets(), new SearchRequestOptions());
Assert.assertNotNull(results);

StepVerifier.create(results.byPage())
.assertNext(res -> {
assertContainKeys(res.items());
Map<String, List<FacetResult>> facets = ((SearchPagedResponse) res).facets();
Assert.assertNotNull(facets);
List<RangeFacetResult> baseRateFacets = getRangeFacetsForField(facets, "Rooms/BaseRate", 4);
List<RangeFacetResult> lastRenovationDateFacets = getRangeFacetsForField(facets, "LastRenovationDate", 2);
assertRangeFacets(baseRateFacets, lastRenovationDateFacets);
}).verifyComplete();
}

@Override
public void canSearchWithValueFacets() {
PagedFlux<SearchResult> results = client.search("*", getSearchParametersForValueFacets(), new SearchRequestOptions());
Assert.assertNotNull(results);

StepVerifier.create(results.byPage())
.assertNext(res -> {
assertContainKeys(res.items());
Map<String, List<FacetResult>> facets = ((SearchPagedResponse) res).facets();
Assert.assertNotNull(facets);

assertValueFacetsEqual(getValueFacetsForField(facets, "Rating", 2),
new ArrayList<>(Arrays.asList(new ValueFacetResult(1L, 5),
new ValueFacetResult(4L, 4))));

assertValueFacetsEqual(getValueFacetsForField(facets, "SmokingAllowed", 2),
new ArrayList<>(Arrays.asList(new ValueFacetResult(4L, false),
new ValueFacetResult(2L, true))));

assertValueFacetsEqual(getValueFacetsForField(facets, "Category", 3),
new ArrayList<>(Arrays.asList(new ValueFacetResult(5L, "Budget"),
new ValueFacetResult(1L, "Boutique"),
new ValueFacetResult(1L, "Luxury"))));

assertValueFacetsEqual(getValueFacetsForField(facets, "LastRenovationDate", 6),
new ArrayList<>(Arrays.asList(new ValueFacetResult(1L, "1970-01-01T00:00:00Z"),
new ValueFacetResult(1L, "1982-01-01T00:00:00Z"),
new ValueFacetResult(2L, "1995-01-01T00:00:00Z"),
new ValueFacetResult(1L, "1999-01-01T00:00:00Z"),
new ValueFacetResult(1L, "2010-01-01T00:00:00Z"),
new ValueFacetResult(1L, "2012-01-01T00:00:00Z"))));

assertValueFacetsEqual(getValueFacetsForField(facets, "Rooms/BaseRate", 4),
new ArrayList<>(Arrays.asList(new ValueFacetResult(1L, 2.44),
new ValueFacetResult(1L, 7.69),
new ValueFacetResult(1L, 8.09),
new ValueFacetResult(1L, 9.69))));

assertValueFacetsEqual(getValueFacetsForField(facets, "Tags", 10),
new ArrayList<>(Arrays.asList(new ValueFacetResult(1L, "24-hour front desk service"),
new ValueFacetResult(1L, "air conditioning"),
new ValueFacetResult(4L, "budget"),
new ValueFacetResult(1L, "coffee in lobby"),
new ValueFacetResult(2L, "concierge"),
new ValueFacetResult(1L, "motel"),
new ValueFacetResult(2L, "pool"),
new ValueFacetResult(1L, "restaurant"),
new ValueFacetResult(1L, "view"),
new ValueFacetResult(4L, "wifi"))));

}).verifyComplete();
}

@Override
public void canSearchWithLuceneSyntax() {
HashMap<String, Object> expectedResult = new HashMap<>();
Expand Down Expand Up @@ -208,13 +280,13 @@ public void canSearchDynamicDocuments() {
}

@Override
public void testCanSearchWithSearchModeAll() {
public void canSearchWithSearchModeAll() {
Flux<SearchResult> response = client.search("Cheapest hotel", new SearchParameters().queryType(SIMPLE).searchMode(ALL), new SearchRequestOptions()).log();
StepVerifier.create(response).assertNext(res -> Assert.assertEquals("2", getSearchResultId(res, "HotelId"))).verifyComplete();
}

@Override
public void testDefaultSearchModeIsAny() {
public void defaultSearchModeIsAny() {
Flux<SearchResult> response = client.search("Cheapest hotel", new SearchParameters(), new SearchRequestOptions()).log();

StepVerifier.create(response)
Expand All @@ -229,7 +301,7 @@ public void testDefaultSearchModeIsAny() {
}

@Override
public void testCanGetResultCountInSearch() {
public void canGetResultCountInSearch() {
Flux<PagedResponse<SearchResult>> results = client.search("*", new SearchParameters().includeTotalResultCount(true), new SearchRequestOptions()).byPage();
StepVerifier.create(results)
.assertNext(res -> Assert.assertEquals(hotels.size(), ((SearchPagedResponse) res).count().intValue()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
import com.azure.core.http.rest.PagedResponse;
import com.azure.search.data.common.DocumentResponseConversions;
import com.azure.search.data.common.SearchPagedResponse;
import com.azure.search.data.customization.RangeFacetResult;
import com.azure.search.data.customization.ValueFacetResult;
import com.azure.search.data.generated.models.FacetResult;
import com.azure.search.data.generated.models.IndexAction;
import com.azure.search.data.generated.models.IndexBatch;
import com.azure.search.data.generated.models.QueryType;
Expand Down Expand Up @@ -180,6 +183,75 @@ public void canFilter() {
Assert.assertTrue(Arrays.asList("1", "5").containsAll(hotelIds));
}

@Override
public void canSearchWithRangeFacets() {
PagedIterable<SearchResult> results = client.search("*", getSearchParametersForRangeFacets(), new SearchRequestOptions());
Assert.assertNotNull(results);

Iterator<PagedResponse<SearchResult>> iterator = results.iterableByPage().iterator();
while (iterator.hasNext()) {
SearchPagedResponse result = (SearchPagedResponse) iterator.next();
assertContainKeys(result.items());
Assert.assertNotNull(result.facets());
List<RangeFacetResult> baseRateFacets = getRangeFacetsForField(result.facets(), "Rooms/BaseRate", 4);
List<RangeFacetResult> lastRenovationDateFacets = getRangeFacetsForField(result.facets(), "LastRenovationDate", 2);
assertRangeFacets(baseRateFacets, lastRenovationDateFacets);
}
}

@Override
public void canSearchWithValueFacets() {
PagedIterable<SearchResult> results = client.search("*", getSearchParametersForValueFacets(), new SearchRequestOptions());
Assert.assertNotNull(results);

Iterator<PagedResponse<SearchResult>> iterator = results.iterableByPage().iterator();
while (iterator.hasNext()) {
SearchPagedResponse result = (SearchPagedResponse) iterator.next();
assertContainKeys(result.items());
Map<String, List<FacetResult>> facets = result.facets();
Assert.assertNotNull(facets);

assertValueFacetsEqual(getValueFacetsForField(facets, "Rating", 2),
new ArrayList<>(Arrays.asList(new ValueFacetResult(1L, 5),
new ValueFacetResult(4L, 4))));

assertValueFacetsEqual(getValueFacetsForField(facets, "SmokingAllowed", 2),
new ArrayList<>(Arrays.asList(new ValueFacetResult(4L, false),
new ValueFacetResult(2L, true))));

assertValueFacetsEqual(getValueFacetsForField(facets, "Category", 3),
new ArrayList<>(Arrays.asList(new ValueFacetResult(5L, "Budget"),
new ValueFacetResult(1L, "Boutique"),
new ValueFacetResult(1L, "Luxury"))));

assertValueFacetsEqual(getValueFacetsForField(facets, "LastRenovationDate", 6),
new ArrayList<>(Arrays.asList(new ValueFacetResult(1L, "1970-01-01T00:00:00Z"),
new ValueFacetResult(1L, "1982-01-01T00:00:00Z"),
new ValueFacetResult(2L, "1995-01-01T00:00:00Z"),
new ValueFacetResult(1L, "1999-01-01T00:00:00Z"),
new ValueFacetResult(1L, "2010-01-01T00:00:00Z"),
new ValueFacetResult(1L, "2012-01-01T00:00:00Z"))));

assertValueFacetsEqual(getValueFacetsForField(facets, "Rooms/BaseRate", 4),
new ArrayList<>(Arrays.asList(new ValueFacetResult(1L, 2.44),
new ValueFacetResult(1L, 7.69),
new ValueFacetResult(1L, 8.09),
new ValueFacetResult(1L, 9.69))));

assertValueFacetsEqual(getValueFacetsForField(facets, "Tags", 10),
new ArrayList<>(Arrays.asList(new ValueFacetResult(1L, "24-hour front desk service"),
new ValueFacetResult(1L, "air conditioning"),
new ValueFacetResult(4L, "budget"),
new ValueFacetResult(1L, "coffee in lobby"),
new ValueFacetResult(2L, "concierge"),
new ValueFacetResult(1L, "motel"),
new ValueFacetResult(2L, "pool"),
new ValueFacetResult(1L, "restaurant"),
new ValueFacetResult(1L, "view"),
new ValueFacetResult(4L, "wifi"))));
}
}

@Override
public void canSearchWithLuceneSyntax() {
Map<String, Object> expectedResult = new HashMap<>();
Expand All @@ -197,7 +269,6 @@ public void canSearchWithLuceneSyntax() {

@Override
public void canFilterNonNullableType() throws Exception {

List<Map<String, Object>> expectedDocsList = prepareDataForNonNullableTest();
SearchParameters searchParameters = new SearchParameters()
.filter("IntValue eq 0 or (Bucket/BucketName eq 'B' and Bucket/Count lt 10)");
Expand All @@ -212,7 +283,7 @@ public void canFilterNonNullableType() throws Exception {
}

@Override
public void testCanSearchWithSearchModeAll() {
public void canSearchWithSearchModeAll() {
List<Map<String, Object>> response = getSearchResults(client
.search("Cheapest hotel", new SearchParameters().queryType(SIMPLE).searchMode(ALL),
new SearchRequestOptions()));
Expand All @@ -222,21 +293,18 @@ public void testCanSearchWithSearchModeAll() {
}

@Override
public void testDefaultSearchModeIsAny() {
List<Map<String, Object>> response = getSearchResults(
client.search("Cheapest hotel", new SearchParameters(), new SearchRequestOptions()));
public void defaultSearchModeIsAny() {
List<Map<String, Object>> response = getSearchResults(client.search("Cheapest hotel", new SearchParameters(), new SearchRequestOptions()));
Assert.assertEquals(7, response.size());
Assert.assertEquals(
Arrays.asList("2", "10", "3", "4", "5", "1", "9"),
response.stream().map(res -> res.get("HotelId").toString()).collect(Collectors.toList()));

}


@Override
public void testCanGetResultCountInSearch() {
PagedIterable<SearchResult> results = client
.search("*", new SearchParameters().includeTotalResultCount(true), new SearchRequestOptions());
public void canGetResultCountInSearch() {
PagedIterable<SearchResult> results = client.search("*", new SearchParameters().includeTotalResultCount(true), new SearchRequestOptions());
Assert.assertNotNull(results);
Iterator<PagedResponse<SearchResult>> resultsIterator = results.iterableByPage().iterator();

Expand Down
Loading

0 comments on commit 1a4f15c

Please sign in to comment.