IndexDocumentsBatch.java

  1. // Copyright (c) Microsoft Corporation. All rights reserved.
  2. // Licensed under the MIT License.

  3. package com.azure.search.documents.indexes.models;

  4. import com.azure.core.annotation.Fluent;
  5. import com.azure.search.documents.SearchDocument;
  6. import com.azure.search.documents.models.IndexAction;
  7. import com.azure.search.documents.models.IndexActionType;
  8. import com.azure.search.documents.models.IndexBatchBase;

  9. import java.util.ArrayList;

  10. /**
  11.  * Contains a batch of document write actions to send to the index.
  12.  */
  13. @Fluent
  14. public class IndexDocumentsBatch<T> extends IndexBatchBase<T> {
  15.     /**
  16.      * Constructor of {@link IndexDocumentsBatch}.
  17.      */
  18.     public IndexDocumentsBatch() {
  19.         super(new ArrayList<>());
  20.     }

  21.     /**
  22.      * Adds document index actions to the batch.
  23.      *
  24.      * @param actions Index actions.
  25.      * @return The updated IndexDocumentsBatch object.
  26.      */
  27.     public IndexDocumentsBatch<T> addActions(Iterable<IndexAction<T>> actions) {
  28.         actions.forEach(action -> this.getActions().add(action));
  29.         return this;
  30.     }

  31.     /**
  32.      * Adds upload document actions to the batch.
  33.      *
  34.      * @param documents Documents to be uploaded.
  35.      * @return The updated IndexDocumentsBatch object.
  36.      */
  37.     public IndexDocumentsBatch<T> addUploadActions(Iterable<T> documents) {
  38.         addDocumentActions(documents, IndexActionType.UPLOAD);
  39.         return this;
  40.     }

  41.     /**
  42.      * Adds document delete actions to the batch.
  43.      *
  44.      * @param documents Document to be deleted.
  45.      * @return The updated IndexDocumentsBatch object.
  46.      */
  47.     public IndexDocumentsBatch<T> addDeleteActions(Iterable<T> documents) {
  48.         addDocumentActions(documents, IndexActionType.DELETE);
  49.         return this;
  50.     }

  51.     /**
  52.      * Adds document delete actions based on key IDs to the batch.
  53.      *
  54.      * @param keyName The key field name.
  55.      * @param keyValues Keys of the documents to delete.
  56.      * @return The updated IndexDocumentsBatch object.
  57.      */
  58.     @SuppressWarnings({"unchecked", "rawtypes"})
  59.     public IndexDocumentsBatch<T> addDeleteActions(String keyName, Iterable<String> keyValues) {
  60.         for (String val : keyValues) {
  61.             SearchDocument doc = new SearchDocument();
  62.             doc.put(keyName, val);
  63.             IndexAction indexAction = new IndexAction()
  64.                 .setActionType(IndexActionType.DELETE)
  65.                 .setDocument(doc);
  66.             this.getActions().add(indexAction);
  67.         }
  68.         return this;
  69.     }

  70.     /**
  71.      * Adds merge document actions to the batch.
  72.      *
  73.      * @param documents Documents to be merged.
  74.      * @return The updated IndexDocumentsBatch object.
  75.      */
  76.     public IndexDocumentsBatch<T> addMergeActions(Iterable<T> documents) {
  77.         addDocumentActions(documents, IndexActionType.MERGE);
  78.         return this;
  79.     }

  80.     /**
  81.      * Adds merge or upload document actions to the batch.
  82.      *
  83.      * @param documents Documents to be merged or uploaded.
  84.      * @return The updated IndexDocumentsBatch object.
  85.      */
  86.     public IndexDocumentsBatch<T> addMergeOrUploadActions(Iterable<T> documents) {
  87.         addDocumentActions(documents, IndexActionType.MERGE_OR_UPLOAD);
  88.         return this;
  89.     }

  90.     private void addDocumentActions(Iterable<T> documents, IndexActionType actionType) {
  91.         documents.forEach(d -> this.getActions().add(new IndexAction<T>()
  92.             .setActionType(actionType)
  93.             .setDocument(d)));
  94.     }
  95. }