Skip to content

Commit

Permalink
Task 594 - Search with specific fields (Azure#28)
Browse files Browse the repository at this point in the history

* Task 594 - Search with specific fields
  • Loading branch information
eladiw authored and navalev committed Aug 18, 2019
1 parent e858145 commit a208d8c
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.azure.core.http.rest.PagedIterable;
import com.azure.core.http.rest.PagedResponse;
import com.azure.search.data.SearchIndexClient;
import com.azure.search.data.common.DocumentResponseConversions;
import com.azure.search.data.common.SearchPagedResponse;
import com.azure.search.data.generated.models.IndexAction;
import com.azure.search.data.generated.models.IndexActionType;
Expand All @@ -21,8 +22,10 @@
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
Expand All @@ -36,6 +39,7 @@ public class SearchIndexClientSearchTest extends SearchIndexClientTestBase {

private static final String INDEX_NAME = "hotels";
private static final String HOTELS_DATA_JSON = "HotelsDataArray.json";
private static final String SEARCH_SCORE_FIELD = "@search.score";

@Override
protected void beforeTest() {
Expand Down Expand Up @@ -81,15 +85,78 @@ public void testCanSyncSearchDynamicDocuments() {
Assert.assertNotNull(result.items());

result.items().forEach(item -> {
Assert.assertEquals(1, ((SearchResult) item).score(), 0);
Assert.assertNull(((SearchResult) item).highlights());
searchResults.add(((SearchResult) item).additionalProperties());
Assert.assertEquals(1, item.score(), 0);
Assert.assertNull(item.highlights());
searchResults.add(item.additionalProperties());
});
}
Assert.assertEquals(hotels.size(), searchResults.size());
assertTrue(compareResults(searchResults, hotels));
}


/**
* Verify that if searching and specifying fields, only those fields are returning
*/
@Test
public void canSearchWithSelectedFields() {
// Ask JUST for the following two fields
SearchParameters sp = new SearchParameters();
sp.searchFields(new LinkedList<>(Arrays.asList("HotelName", "Category")));
sp.select(new LinkedList<>(Arrays.asList("HotelName", "Rating", "Address/City", "Rooms/Type")));

PagedIterable<SearchResult> results = client.search("fancy luxury secret", sp, new SearchRequestOptions());

HashMap<String, Object> expectedHotel1 = new HashMap<>();
expectedHotel1.put("HotelName", "Fancy Stay");
expectedHotel1.put("Rating", 5);
expectedHotel1.put("Address", null);
expectedHotel1.put("Rooms", Arrays.asList());

// This is the expected document when querying the document later (notice that only two fields are expected)
HashMap<String, Object> expectedHotel2 = new HashMap<>();
expectedHotel2.put("HotelName", "Secret Point Motel");
expectedHotel2.put("Rating", 3);
HashMap<String, Object> address = new LinkedHashMap<>();
address.put("City", "New York");
expectedHotel2.put("Address", address);
HashMap<String, Object> rooms = new LinkedHashMap<>();
rooms.put("Type", "Budget Room");
HashMap<String, Object> rooms2 = new LinkedHashMap<>();
rooms2.put("Type", "Budget Room");
expectedHotel2.put("Rooms", Arrays.asList(rooms, rooms2));

Iterator<PagedResponse<SearchResult>> iterator = results.iterableByPage().iterator();
PagedResponse<SearchResult> result = iterator.next();
assert (result.items().size() == 2);

// From the result object, extract the two hotels, clean up (irrelevant fields) and change data structure
// as a preparation to check equality
Map<String, Object> hotel1 = extractAndTransformSingleResult(result.items().get(0));
Map<String, Object> hotel2 = extractAndTransformSingleResult(result.items().get(1));

Assert.assertEquals(expectedHotel1, hotel1);
Assert.assertEquals(expectedHotel2, hotel2);
}

private Map<String, Object> extractAndTransformSingleResult(SearchResult result) {
return dropUnnecessaryFields(
DocumentResponseConversions.convertLinkedHashMapToMap(
(result.additionalProperties())));
}

/**
* Drop fields that shouldn't be in the returned object
*
* @param map the map to drop items from
* @return the new map
*/
private static Map<String, Object> dropUnnecessaryFields(Map<String, Object> map) {
map.remove(SEARCH_SCORE_FIELD);

return map;
}

private boolean compareResults(List<Map<String, Object>> searchResults, List<Map<String, Object>> hotels) {
Iterator<Map<String, Object>> searchIterator = searchResults.iterator();
Iterator<Map<String, Object>> hotelsIterator = hotels.iterator();
Expand All @@ -102,9 +169,6 @@ private boolean compareResults(List<Map<String, Object>> searchResults, List<Map
// TODO (Nava) - remove once geo location issue is resolved
result.remove("Location");
hotel.remove("Location");
/*if (!hotel.entrySet().stream().allMatch(e -> checkEquals(e, result))) {
return false;
}*/
assertTrue(hotel.entrySet().stream().allMatch(e -> checkEquals(e, result)));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"networkCallRecords" : [ {
"Method" : "POST",
"Uri" : "https://azs-sdkd8d76965dd03.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" : "e30d0194-741c-4f21-8f1d-02e6630bc4da",
"StatusCode" : "200",
"Date" : "Thu, 15 Aug 2019 05:34:22 GMT",
"Strict-Transport-Security" : "max-age=15724800; includeSubDomains",
"Cache-Control" : "no-cache",
"elapsed-time" : "336",
"OData-Version" : "4.0",
"Expires" : "-1",
"Content-Length" : "795",
"Body" : "{\"@odata.context\":\"https://azs-sdkd8d76965dd03.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-sdkd8d76965dd03.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" : "2facd50b-d288-4ac0-927e-fa39bb0248f6",
"StatusCode" : "200",
"Date" : "Thu, 15 Aug 2019 05:35:09 GMT",
"Strict-Transport-Security" : "max-age=15724800; includeSubDomains",
"Cache-Control" : "no-cache",
"elapsed-time" : "146",
"OData-Version" : "4.0",
"Expires" : "-1",
"Content-Length" : "362",
"Body" : "{\"@odata.context\":\"https://azs-sdkd8d76965dd03.search.windows.net/indexes('hotels')/$metadata#docs(*)\",\"value\":[{\"@search.score\":1.1593354,\"HotelName\":\"Fancy Stay\",\"Rating\":5,\"Address\":null,\"Rooms\":[]},{\"@search.score\":0.17835927,\"HotelName\":\"Secret Point Motel\",\"Rating\":3,\"Address\":{\"City\":\"New York\"},\"Rooms\":[{\"Type\":\"Budget Room\"},{\"Type\":\"Budget Room\"}]}]}",
"Preference-Applied" : "odata.include-annotations=\"*\"",
"Content-Type" : "application/json; odata.metadata=minimal"
}
} ],
"variables" : [ ]
}

0 comments on commit a208d8c

Please sign in to comment.