Skip to content

Commit

Permalink
Merge pull request Azure#22 from navalev/retrieve-document
Browse files Browse the repository at this point in the history
Retrieve document
  • Loading branch information
LizaShak committed Aug 18, 2019
2 parents a208d8c + 64c8542 commit 2c52406
Show file tree
Hide file tree
Showing 8 changed files with 375 additions and 16 deletions.
5 changes: 5 additions & 0 deletions search/data-plane/data/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@
<scope>system</scope>
<systemPath>${basedir}/lib/service-1.0-SNAPSHOT.jar</systemPath>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@

package com.azure.search.data.common;

import com.azure.core.exception.HttpResponseException;
import com.azure.core.exception.ResourceNotFoundException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
Expand All @@ -26,12 +30,23 @@ public static Map<String, Object> convertLinkedHashMapToMap(Object linkedMapObje
**/
@SuppressWarnings (value = "unchecked")
LinkedHashMap<String, Object> linkedMap = (LinkedHashMap<String, Object>) linkedMapObject;

Set<Map.Entry<String, Object>> entries = linkedMap.entrySet();

Map<String, Object> convertedMap = new HashMap<>();

for (Map.Entry<String, Object> entry : entries) {
convertedMap.put(entry.getKey(), entry.getValue());
Object value = entry.getValue();

if (value instanceof LinkedHashMap) {
value = convertLinkedHashMapToMap(entry.getValue());
}
if (value instanceof ArrayList) {
value = convertArray((ArrayList) value);

}

convertedMap.put(entry.getKey(), value);
}

return convertedMap;
Expand All @@ -47,4 +62,38 @@ public static Map<String, Object> dropUnnecessaryFields(Map<String, Object> map)

return map;
}


/**
* Convert Array Object elements
* @param array which elements will be converted
* @return ArrayList<Object>
*/
private static ArrayList<Object> convertArray(ArrayList array) {
ArrayList<Object> convertedArray = new ArrayList<>();
for (Object arrayValue : array) {
if (arrayValue instanceof LinkedHashMap) {
convertedArray.add(convertLinkedHashMapToMap(arrayValue));
} else {
convertedArray.add(arrayValue);
}
}
return convertedArray;
}

/**
* Map exceptions to be more informative
* @param throwable to convert
* @return Throwable
*/
public static Throwable exceptionMapper(Throwable throwable) {

if (throwable instanceof HttpResponseException && throwable.getMessage().equals("Status code 404, (empty body)")) {
return new ResourceNotFoundException("Document not found", ((HttpResponseException) throwable).response());
}

return throwable;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,14 @@ public PagedFlux<SearchResult> search(String searchText, SearchParameters search

@Override
public Mono<Map<String, Object>> getDocument(String key) {
return restClient.documents().getAsync(key).map(DocumentResponseConversions::convertLinkedHashMapToMap)
.map(DocumentResponseConversions::dropUnnecessaryFields);
return restClient
.documents()
.getAsync(key)
.map(DocumentResponseConversions::convertLinkedHashMapToMap)
.map(DocumentResponseConversions::dropUnnecessaryFields)
.onErrorMap(DocumentResponseConversions::exceptionMapper)
.doOnSuccess(s -> System.out.println("Document with key: " + key + " was retrieved succesfully"))
.doOnError(e -> System.out.println("An error occured in getDocument(key): " + e.getMessage()));
}

@Override
Expand All @@ -197,7 +203,10 @@ public Mono<Map<String, Object>> getDocument(
.documents()
.getAsync(key, selectedFields, searchRequestOptions)
.map(DocumentResponseConversions::convertLinkedHashMapToMap)
.map(DocumentResponseConversions::dropUnnecessaryFields);
.map(DocumentResponseConversions::dropUnnecessaryFields)
.onErrorMap(DocumentResponseConversions::exceptionMapper)
.doOnSuccess(s -> System.out.println("Document with key: " + key + "and selectedFields: " + selectedFields.toString() + " was retrieved succesfully"))
.doOnError(e -> System.out.println("An error occured in getDocument(key, selectedFields, searchRequestOptions): " + e.getMessage()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import com.azure.search.data.SearchIndexAsyncClient;
import com.azure.search.data.customization.SearchIndexClientBuilder;
import com.azure.search.data.common.SearchPipelinePolicy;
import com.azure.search.data.generated.models.DocumentIndexResult;
import com.azure.search.data.generated.models.IndexAction;
import com.azure.search.data.generated.models.IndexActionType;
import com.azure.search.data.generated.models.IndexBatch;
Expand Down Expand Up @@ -37,10 +36,10 @@ public class SearchIndexDocs {
* to be used in tests.
*
* @param searchServiceName The name of the Search service
* @param apiAdminKey The Admin key of the Search service
* @param indexName The name of the index
* @param dnsSuffix DNS suffix of the Search service
* @param apiVersion used API version
* @param apiAdminKey The Admin key of the Search service
* @param indexName The name of the index
* @param dnsSuffix DNS suffix of the Search service
* @param apiVersion used API version
*/
public SearchIndexDocs(
String searchServiceName, String apiAdminKey, String indexName,
Expand All @@ -54,6 +53,7 @@ public SearchIndexDocs(

/**
* Created new documents in the index. The new documents are retrieved from HotelsDataArray.json
*
* @throws IOException If the file in HOTELS_DATA_JSON is not existing or invalid.
*/
public void initialize() throws IOException {
Expand All @@ -77,15 +77,49 @@ private void addDocsData() throws IOException {
List<IndexAction> indexActions = createIndexActions(hotels);

System.out.println("Indexing " + indexActions.size() + " docs");
DocumentIndexResult documentIndexResult = searchIndexAsyncClient.index(new IndexBatch().actions(indexActions))
.block();
System.out.println("Indexing Results:");
searchIndexAsyncClient.index(
new IndexBatch()
.actions(indexActions))
.doOnSuccess(documentIndexResult ->
documentIndexResult
.results().forEach(
result ->
System.out.println("key:" + result.key() + (result.succeeded() ? " Succeeded" : " Error: " + result.errorMessage()))))
.doOnError(e -> System.out.println(e.getMessage()))
.block();


}

public void addSingleDocData(Map document) throws IOException {

if (searchIndexAsyncClient == null) {
searchIndexAsyncClient = new SearchIndexClientBuilder()
.serviceName(searchServiceName)
.searchDnsSuffix(dnsSuffix)
.indexName(indexName)
.apiVersion(apiVersion)
.addPolicy(new SearchPipelinePolicy(apiAdminKey))
.buildAsyncClient();
}

List<Map> documents = new ArrayList<>();
documents.add(document);
List<IndexAction> indexActions = createIndexActions(documents);

System.out.println("Indexing " + indexActions.size() + " docs");
System.out.println("Indexing Results:");
assert documentIndexResult != null;
documentIndexResult.results().forEach(result ->
System.out.println(
"key:" + result.key() + (result.succeeded() ? " Succeeded" : " Error: " + result.errorMessage()))
);
searchIndexAsyncClient.index(
new IndexBatch()
.actions(indexActions))
.doOnSuccess(documentIndexResult ->
documentIndexResult
.results().forEach(
result ->
System.out.println("key:" + result.key() + (result.succeeded() ? " Succeeded" : " Error: " + result.errorMessage()))))
.doOnError(e -> System.out.println(e.getMessage()))
.block();
}

private List<IndexAction> createIndexActions(List<Map> hotels) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package com.azure.search.data.test.customization;

import com.azure.core.exception.HttpResponseException;
import com.azure.core.exception.ResourceNotFoundException;
import com.azure.search.data.SearchIndexAsyncClient;
import io.netty.handler.codec.http.HttpResponseStatus;
import org.junit.Assert;
import org.junit.Test;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public class SearchIndexAsyncClientImplTest extends SearchIndexClientTestBase {

private SearchIndexAsyncClient asyncClient;

private static final String INDEX_NAME = "hotels";

@Override
protected void beforeTest() {
super.beforeTest();
asyncClient = builderSetup().indexName(INDEX_NAME).buildAsyncClient();
}

@Test
public void canGetDynamicDocument() {

Map<String, Object> addressDoc = new HashMap<String, Object>();
addressDoc.put("StreetAddress", "677 5th Ave");
addressDoc.put("City", "New York");
addressDoc.put("StateProvince", "NY");
addressDoc.put("Country", "USA");
addressDoc.put("PostalCode", "10022");

ArrayList<String> room1Tags = new ArrayList<String>();
room1Tags.add("vcr/dvd");

HashMap<String, Object> room1Doc = new HashMap<String, Object>();
room1Doc.put("Description", "Budget Room, 1 Queen Bed (Cityside)");
room1Doc.put("Description_fr", "Chambre Économique, 1 grand lit (côté ville)");
room1Doc.put("Type", "Budget Room");
room1Doc.put("BaseRate", 9.69);
room1Doc.put("BedOptions", "1 Queen Bed");
room1Doc.put("SleepsCount", 2);
room1Doc.put("SmokingAllowed", true);
room1Doc.put("Tags", room1Tags);

ArrayList<String> room2Tags = new ArrayList<String>();
room2Tags.add("vcr/dvd");
room2Tags.add("jacuzzi tub");

HashMap<String, Object> room2Doc = new HashMap<String, Object>();
room2Doc.put("Description", "Budget Room, 1 King Bed (Mountain View)");
room2Doc.put("Description_fr", "Chambre Économique, 1 très grand lit (Mountain View)");
room2Doc.put("Type", "Budget Room");
room2Doc.put("BaseRate", 8.09);
room2Doc.put("BedOptions", "1 King Bed");
room2Doc.put("SleepsCount", 2);
room2Doc.put("SmokingAllowed", true);
room2Doc.put("Tags", room2Tags);

ArrayList<HashMap<String, Object>> rooms = new ArrayList<HashMap<String, Object>>();
rooms.add(room1Doc);
rooms.add(room2Doc);


ArrayList<String> tags = new ArrayList<String>();
tags.add("pool");
tags.add("air conditioning");
tags.add("concierge");

HashMap<String, Object> expectedDoc = new HashMap<String, Object>();
expectedDoc.put("HotelId", "1");
expectedDoc.put("HotelName", "Secret Point Motel");
expectedDoc.put("Description", "The hotel is ideally located on the main commercial artery of the city in the heart of New York. A few minutes away is Time's Square and the historic centre of the city, as well as other places of interest that make New York one of America's most attractive and cosmopolitan cities.");
expectedDoc.put("Description_fr", "L'hôtel est idéalement situé sur la principale artère commerciale de la ville en plein cœur de New York. A quelques minutes se trouve la place du temps et le centre historique de la ville, ainsi que d'autres lieux d'intérêt qui font de New York l'une des villes les plus attractives et cosmopolites de l'Amérique.");
expectedDoc.put("Category", "Boutique");
expectedDoc.put("Tags", tags);
expectedDoc.put("ParkingIncluded", false);
expectedDoc.put("SmokingAllowed", true);
expectedDoc.put("LastRenovationDate", "1970-01-18T00:00:00Z");
expectedDoc.put("Rating", 3);
expectedDoc.put("Address", addressDoc);
expectedDoc.put("Rooms", rooms);
//TODO: Support GeoTypes

try {
super.indexDocument(asyncClient, expectedDoc);

} catch (Exception e) {
e.printStackTrace();
}

Mono<Map<String, Object>> futureDoc = asyncClient.getDocument("1");

StepVerifier
.create(futureDoc)
.assertNext(result -> {
result.remove("Location");
Assert.assertEquals(expectedDoc, result);
})
.verifyComplete();
}

@Test
public void getDocumentThrowsWhenDocumentNotFound() {
Mono<Map<String, Object>> futureDoc = asyncClient.getDocument("1000000001");
StepVerifier
.create(futureDoc)
.verifyErrorSatisfies(error -> assertEquals(ResourceNotFoundException.class, error.getClass()));
}

@Test
public void getDocumentThrowsWhenRequestIsMalformed() {

HashMap<String, Object> hotelDoc = new HashMap<String, Object>();
hotelDoc.put("HotelId", "2");
hotelDoc.put("Description", "Surprisingly expensive");

ArrayList<String> selectedFields = new ArrayList<String>();
selectedFields.add("HotelId");
selectedFields.add("ThisFieldDoesNotExist");

try {
super.indexDocument(asyncClient, hotelDoc);

} catch (Exception e) {
e.printStackTrace();
}

Mono futureDoc = asyncClient.getDocument("2", selectedFields, null);

StepVerifier
.create(futureDoc)
.verifyErrorSatisfies(error -> {
assertEquals(HttpResponseException.class, error.getClass());
assertEquals(HttpResponseStatus.BAD_REQUEST.code(), ((HttpResponseException) error).response().statusCode());
assertTrue(error.getMessage().contains("Invalid expression: Could not find a property named 'ThisFieldDoesNotExist' on type 'search.document'."));
//TODO: Create Enum for messages
});
}
}

0 comments on commit 2c52406

Please sign in to comment.