Skip to content

Commit

Permalink
Update Search APIs Based on Review (#12791)
Browse files Browse the repository at this point in the history
* Remove vararg APIs from IndexDocumentsBatch

* Update README codesnippets
  • Loading branch information
alzimmermsft authored Jul 7, 2020
1 parent 86afb16 commit 927fa43
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 152 deletions.
4 changes: 2 additions & 2 deletions sdk/search/azure-search-documents/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -412,8 +412,8 @@ to be aware of.
<!-- embedme ./src/samples/java/com/azure/search/documents/ReadmeSamples.java#L217-L220 -->
```Java
IndexDocumentsBatch<Hotel> batch = new IndexDocumentsBatch<Hotel>();
batch.addUploadActions(new Hotel().setId("783").setName("Upload Inn"));
batch.addMergeActions(new Hotel().setId("12").setName("Renovated Ranch"));
batch.addUploadActions(Collections.singletonList(new Hotel().setId("783").setName("Upload Inn")));
batch.addMergeActions(Collections.singletonList(new Hotel().setId("12").setName("Renovated Ranch")));
searchClient.indexDocuments(batch);
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -704,9 +704,8 @@ private static Map<String, List<FacetResult>> getFacets(SearchDocumentsResult re

Map<String, List<FacetResult>> facets = new HashMap<>();

result.getFacets().forEach((key, values) -> {
facets.put(key, values.stream().map(FacetResultConverter::map).collect(Collectors.toList()));
});
result.getFacets().forEach((key, values) ->
facets.put(key, values.stream().map(FacetResultConverter::map).collect(Collectors.toList())));

return facets;
}
Expand Down Expand Up @@ -951,10 +950,11 @@ private static AutocompleteRequest createAutoCompleteRequest(String searchText,
}

private static <T> IndexDocumentsBatch<T> buildIndexBatch(Iterable<T> documents, IndexActionType actionType) {
List<IndexAction<T>> actions = new ArrayList<IndexAction<T>>();
List<IndexAction<T>> actions = new ArrayList<>();
documents.forEach(d -> actions.add(new IndexAction<T>()
.setActionType(actionType)
.setDocument(d)));
return new IndexDocumentsBatch<T>(actions);

return new IndexDocumentsBatch<T>().addActions(actions);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
import com.azure.search.documents.models.IndexBatchBase;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
* Contains a batch of document write actions to send to the index.
Expand All @@ -22,70 +20,48 @@ public class IndexDocumentsBatch<T> extends IndexBatchBase<T> {
* Constructor of {@link IndexDocumentsBatch}.
*/
public IndexDocumentsBatch() {
super(new ArrayList<IndexAction<T>>());
super(new ArrayList<>());
}

/**
* Constructor of {@link IndexDocumentsBatch}
* Adds document index actions to the batch.
*
* @param actions the actions value to set.
* @param actions Index actions.
* @return The updated IndexDocumentsBatch object.
*/
public IndexDocumentsBatch(List<IndexAction<T>> actions) {
super(actions);
}

/**
* Adds an Upload IndexAction to the IndexAction chain for a document.
*
* @param documents The documents to be uploaded.
* @return IndexBatch with the desired actions added.
*/
@SuppressWarnings("unchecked")
public IndexDocumentsBatch<T> addUploadActions(T... documents) {
addDocumentActions(Arrays.asList(documents), IndexActionType.UPLOAD);
public IndexDocumentsBatch<T> addActions(Iterable<IndexAction<T>> actions) {
actions.forEach(action -> this.getActions().add(action));
return this;
}

/**
* Adds Upload IndexActions to the IndexAction chain for a collection of documents.
* Adds upload document actions to the batch.
*
* @param documents The document collection to be uploaded.
* @return IndexBatch with the desired actions added.
* @param documents Documents to be uploaded.
* @return The updated IndexDocumentsBatch object.
*/
public IndexDocumentsBatch<T> addUploadActions(Iterable<T> documents) {
addDocumentActions(documents, IndexActionType.UPLOAD);
return this;
}

/**
* Adds a Delete IndexAction to the IndexAction chain for a document.
*
* @param documents The documents to be deleted.
* @return IndexBatch with the desired actions added.
*/
@SuppressWarnings("unchecked")
public IndexDocumentsBatch<T> addDeleteActions(T... documents) {
addDocumentActions(Arrays.asList(documents), IndexActionType.DELETE);
return this;
}

/**
* Adds Delete IndexActions to the IndexAction chain for a collection of documents.
* Adds document delete actions to the batch.
*
* @param documents The document collection to be deleted.
* @return IndexBatch with the desired actions added.
* @param documents Document to be deleted.
* @return The updated IndexDocumentsBatch object.
*/
public IndexDocumentsBatch<T> addDeleteActions(Iterable<T> documents) {
addDocumentActions(documents, IndexActionType.DELETE);
return this;
}

/**
* Adds Delete IndexActions to the IndexAction chain for a collection of documents.
* Adds document delete actions based on key IDs to the batch.
*
* @param keyName The name of the key field that uniquely identifies documents in the index.
* @param keyValues The keys of the documents to delete.
* @return IndexBatch with the desired actions added.
* @param keyName The key field name.
* @param keyValues Keys of the documents to delete.
* @return The updated IndexDocumentsBatch object.
*/
@SuppressWarnings({"unchecked", "rawtypes"})
public IndexDocumentsBatch<T> addDeleteActions(String keyName, Iterable<String> keyValues) {
Expand All @@ -101,67 +77,30 @@ public IndexDocumentsBatch<T> addDeleteActions(String keyName, Iterable<String>
}

/**
* Adds Delete IndexActions to the IndexAction chain for a collection of documents.
*
* @param keyName The name of the key field that uniquely identifies documents in the index.
* @param keyValues The keys of the documents to delete.
* @return IndexBatch with the desired actions added.
*/
public IndexDocumentsBatch<T> addDeleteActions(String keyName, String... keyValues) {
return this.addDeleteActions(keyName, Arrays.asList(keyValues));
}

/**
* Adds a Merge IndexAction to the IndexAction chain for a document.
*
* @param documents The documents to be merged.
* @return IndexBatch with the desired actions added.
*/
@SuppressWarnings("unchecked")
public IndexDocumentsBatch<T> addMergeActions(T... documents) {
addDocumentActions(Arrays.asList(documents), IndexActionType.MERGE);
return this;
}

/**
* Adds Merge IndexActions to the IndexAction chain for a collection of documents.
* Adds merge document actions to the batch..
*
* @param documents The document collection to be merged.
* @return IndexBatch with the desired actions added.
* @param documents Documents to be merged.
* @return The updated IndexDocumentsBatch object.
*/
public IndexDocumentsBatch<T> addMergeActions(Iterable<T> documents) {
addDocumentActions(documents, IndexActionType.MERGE);
return this;
}

/**
* Adds a Merge or Upload IndexAction to the IndexAction chain for a document.
*
* @param documents The documents to be merged or uploaded.
* @return IndexBatch with the desired actions added.
*/
@SuppressWarnings("unchecked")
public IndexDocumentsBatch<T> addMergeOrUploadActions(T... documents) {
addDocumentActions(Arrays.asList(documents), IndexActionType.MERGE_OR_UPLOAD);
return this;
}

/**
* Adds Merge or Upload IndexActions to the IndexAction chain for a collection of documents.
* Adds merge or upload document actions to the batch.
*
* @param documents The document collection to be merged or uploaded.
* @return IndexBatch with the desired actions added.
* @param documents Documents to be merged or uploaded.
* @return The updated IndexDocumentsBatch object.
*/
public IndexDocumentsBatch<T> addMergeOrUploadActions(Iterable<T> documents) {
addDocumentActions(documents, IndexActionType.MERGE_OR_UPLOAD);
return this;
}

private void addDocumentActions(Iterable<T> documents, IndexActionType actionType) {
documents.forEach(d -> {
this.getActions().add(new IndexAction<T>()
.setActionType(actionType)
.setDocument(d));
});
documents.forEach(d -> this.getActions().add(new IndexAction<T>()
.setActionType(actionType)
.setDocument(d)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.azure.search.documents.models.IndexDocumentsResult;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
Expand Down Expand Up @@ -66,8 +67,8 @@ private static void advancedIndexing() {
.buildClient();

IndexDocumentsBatch<Hotel> batch = new IndexDocumentsBatch<Hotel>()
.addMergeOrUploadActions(new Hotel().setHotelId("100"))
.addDeleteActions(new Hotel().setHotelId("200"));
.addMergeOrUploadActions(Collections.singletonList(new Hotel().setHotelId("100")))
.addDeleteActions(Collections.singletonList(new Hotel().setHotelId("200")));

// Send a single batch that performs many different actions
IndexDocumentsResult result = client.indexDocuments(batch);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,8 @@ public void retrieveDocuments() {

public void batchDocumentsOperations() {
IndexDocumentsBatch<Hotel> batch = new IndexDocumentsBatch<Hotel>();
batch.addUploadActions(new Hotel().setId("783").setName("Upload Inn"));
batch.addMergeActions(new Hotel().setId("12").setName("Renovated Ranch"));
batch.addUploadActions(Collections.singletonList(new Hotel().setId("783").setName("Upload Inn")));
batch.addMergeActions(Collections.singletonList(new Hotel().setId("12").setName("Renovated Ranch")));
searchClient.indexDocuments(batch);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,8 @@ public void indexDocuments() {
searchDocument2.put("hotelId", "2");
searchDocument2.put("hotelName", "test2");
IndexDocumentsBatch<SearchDocument> indexDocumentsBatch = new IndexDocumentsBatch<>();
indexDocumentsBatch.addUploadActions(searchDocument1);
indexDocumentsBatch.addDeleteActions(searchDocument2);
indexDocumentsBatch.addUploadActions(Collections.singletonList(searchDocument1));
indexDocumentsBatch.addDeleteActions(Collections.singletonList(searchDocument2));
IndexDocumentsResult result = searchClient.indexDocuments(indexDocumentsBatch);
for (IndexingResult indexingResult : result.getResults()) {
System.out.printf("Does document with key %s finish successfully? %b%n", indexingResult.getKey(),
Expand All @@ -242,8 +242,8 @@ public void indexDocumentsWithResponse() {
searchDocument2.put("hotelId", "2");
searchDocument2.put("hotelName", "test2");
IndexDocumentsBatch<SearchDocument> indexDocumentsBatch = new IndexDocumentsBatch<>();
indexDocumentsBatch.addUploadActions(searchDocument1);
indexDocumentsBatch.addDeleteActions(searchDocument2);
indexDocumentsBatch.addUploadActions(Collections.singletonList(searchDocument1));
indexDocumentsBatch.addDeleteActions(Collections.singletonList(searchDocument2));
Response<IndexDocumentsResult> resultResponse = searchClient.indexDocumentsWithResponse(indexDocumentsBatch,
null, new Context(key1, value1));
System.out.println("The status code of the response is " + resultResponse.getStatusCode());
Expand Down Expand Up @@ -561,8 +561,8 @@ public void indexDocumentsAsync() {
searchDocument2.put("hotelId", "2");
searchDocument2.put("hotelName", "test2");
IndexDocumentsBatch<SearchDocument> indexDocumentsBatch = new IndexDocumentsBatch<>();
indexDocumentsBatch.addUploadActions(searchDocument1);
indexDocumentsBatch.addDeleteActions(searchDocument2);
indexDocumentsBatch.addUploadActions(Collections.singletonList(searchDocument1));
indexDocumentsBatch.addDeleteActions(Collections.singletonList(searchDocument2));
searchAsyncClient.indexDocuments(indexDocumentsBatch)
.subscribe(result -> {
for (IndexingResult indexingResult : result.getResults()) {
Expand All @@ -585,8 +585,8 @@ public void indexDocumentsWithResponseAsync() {
searchDocument2.put("hotelId", "2");
searchDocument2.put("hotelName", "test2");
IndexDocumentsBatch<SearchDocument> indexDocumentsBatch = new IndexDocumentsBatch<>();
indexDocumentsBatch.addUploadActions(searchDocument1);
indexDocumentsBatch.addDeleteActions(searchDocument2);
indexDocumentsBatch.addUploadActions(Collections.singletonList(searchDocument1));
indexDocumentsBatch.addDeleteActions(Collections.singletonList(searchDocument2));
searchAsyncClient.indexDocumentsWithResponse(indexDocumentsBatch, null)
.subscribe(resultResponse -> {
System.out.println("The status code of the response is " + resultResponse.getStatusCode());
Expand Down
Loading

0 comments on commit 927fa43

Please sign in to comment.