| | | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | | 2 | | // Licensed under the MIT License. See License.txt in the project root for |
| | | 3 | | // license information. |
| | | 4 | | |
| | | 5 | | namespace Microsoft.Azure.Search.Models |
| | | 6 | | { |
| | | 7 | | using System.Collections.Generic; |
| | | 8 | | using System.Linq; |
| | | 9 | | using Common; |
| | | 10 | | |
| | | 11 | | /// <summary> |
| | | 12 | | /// Provides factory methods for creating a batch of document write operations to send to the search index. |
| | | 13 | | /// </summary> |
| | | 14 | | public static class IndexBatch |
| | | 15 | | { |
| | | 16 | | /// <summary> |
| | | 17 | | /// Creates a new IndexBatch for deleting a batch of documents. |
| | | 18 | | /// </summary> |
| | | 19 | | /// <param name="keyName">The name of the key field that uniquely identifies documents in the index.</param> |
| | | 20 | | /// <param name="keyValues">The keys of the documents to delete.</param> |
| | | 21 | | /// <returns>A new IndexBatch.</returns> |
| | | 22 | | public static IndexBatch<Document> Delete(string keyName, IEnumerable<string> keyValues) |
| | | 23 | | { |
| | 2 | 24 | | Throw.IfArgumentNull(keyName, "keyName"); |
| | 2 | 25 | | Throw.IfArgumentNull(keyValues, "keyValues"); |
| | | 26 | | |
| | 6 | 27 | | return New(keyValues.Select(v => IndexAction.Delete(keyName, v))); |
| | | 28 | | } |
| | | 29 | | |
| | | 30 | | /// <summary> |
| | | 31 | | /// Creates a new IndexBatch for deleting a batch of documents. |
| | | 32 | | /// </summary> |
| | | 33 | | /// <typeparam name="T"> |
| | | 34 | | /// The CLR type that maps to the index schema. Instances of this type can be stored as documents in the index. |
| | | 35 | | /// </typeparam> |
| | | 36 | | /// <param name="documents">The documents to delete; Fields other than the key are ignored.</param> |
| | | 37 | | /// <returns>A new IndexBatch.</returns> |
| | | 38 | | public static IndexBatch<T> Delete<T>(IEnumerable<T> documents) |
| | | 39 | | { |
| | 4 | 40 | | Throw.IfArgumentNull(documents, "documents"); |
| | | 41 | | |
| | 8 | 42 | | return New(documents.Select(d => IndexAction.Delete(d))); |
| | | 43 | | } |
| | | 44 | | |
| | | 45 | | /// <summary> |
| | | 46 | | /// Creates a new IndexBatch for merging documents into existing documents in the index. |
| | | 47 | | /// </summary> |
| | | 48 | | /// <typeparam name="T"> |
| | | 49 | | /// The CLR type that maps to the index schema. Instances of this type can be stored as documents in the index. |
| | | 50 | | /// </typeparam> |
| | | 51 | | /// <param name="documents">The documents to merge; Set only the properties that you want to change.</param> |
| | | 52 | | /// <returns>A new IndexBatch.</returns> |
| | | 53 | | /// <remarks> |
| | | 54 | | /// <para>If type T contains non-nullable value-typed properties, these properties may not merge correctly. If y |
| | | 55 | | /// do not set such a property, it will automatically take its default value (for example, 0 for int or false |
| | | 56 | | /// for bool), which will override the value of the property currently stored in the index, even if this was |
| | | 57 | | /// not your intent. For this reason, it is strongly recommended that you always declare value-typed |
| | | 58 | | /// properties to be nullable in type T.</para> |
| | | 59 | | /// <para>The above does not apply if you are using <c cref="Document">Document</c> as type T.</para> |
| | | 60 | | /// </remarks> |
| | | 61 | | public static IndexBatch<T> Merge<T>(IEnumerable<T> documents) |
| | | 62 | | { |
| | 12 | 63 | | Throw.IfArgumentNull(documents, "documents"); |
| | | 64 | | |
| | 24 | 65 | | return New(documents.Select(d => IndexAction.Merge(d))); |
| | | 66 | | } |
| | | 67 | | |
| | | 68 | | /// <summary> |
| | | 69 | | /// Creates a new IndexBatch for uploading documents to the index, or merging them into existing documents |
| | | 70 | | /// for those that already exist in the index. |
| | | 71 | | /// </summary> |
| | | 72 | | /// <typeparam name="T"> |
| | | 73 | | /// The CLR type that maps to the index schema. Instances of this type can be stored as documents in the index. |
| | | 74 | | /// </typeparam> |
| | | 75 | | /// <param name="documents">The documents to merge or upload.</param> |
| | | 76 | | /// <returns>A new IndexBatch.</returns> |
| | | 77 | | /// <remarks> |
| | | 78 | | /// <para>If type T contains non-nullable value-typed properties, these properties may not merge correctly. If y |
| | | 79 | | /// do not set such a property, it will automatically take its default value (for example, 0 for int or false |
| | | 80 | | /// for bool), which will override the value of the property currently stored in the index, even if this was |
| | | 81 | | /// not your intent. For this reason, it is strongly recommended that you always declare value-typed |
| | | 82 | | /// properties to be nullable in type T.</para> |
| | | 83 | | /// <para>The above does not apply if you are using <c cref="Document">Document</c> as type T.</para> |
| | | 84 | | /// </remarks> |
| | | 85 | | public static IndexBatch<T> MergeOrUpload<T>(IEnumerable<T> documents) |
| | | 86 | | { |
| | 8 | 87 | | Throw.IfArgumentNull(documents, "documents"); |
| | | 88 | | |
| | 16 | 89 | | return New(documents.Select(d => IndexAction.MergeOrUpload(d))); |
| | | 90 | | } |
| | | 91 | | |
| | | 92 | | /// <summary> |
| | | 93 | | /// Creates a new instance of the IndexBatch class. |
| | | 94 | | /// </summary> |
| | | 95 | | /// <typeparam name="T"> |
| | | 96 | | /// The CLR type that maps to the index schema. Instances of this type can be stored as documents in the index. |
| | | 97 | | /// </typeparam> |
| | | 98 | | /// <param name="actions">The index actions to include in the batch.</param> |
| | | 99 | | /// <returns>A new IndexBatch.</returns> |
| | | 100 | | /// <remarks> |
| | | 101 | | /// You can use this method as a convenience if you don't want to explicitly specify your model class as a |
| | | 102 | | /// type parameter. |
| | | 103 | | /// </remarks> |
| | | 104 | | public static IndexBatch<T> New<T>(IEnumerable<IndexAction<T>> actions) |
| | | 105 | | { |
| | 494 | 106 | | return new IndexBatch<T>(actions); |
| | | 107 | | } |
| | | 108 | | |
| | | 109 | | /// <summary> |
| | | 110 | | /// Creates a new IndexBatch for uploading documents to the index. |
| | | 111 | | /// </summary> |
| | | 112 | | /// <typeparam name="T"> |
| | | 113 | | /// The CLR type that maps to the index schema. Instances of this type can be stored as documents in the index. |
| | | 114 | | /// </typeparam> |
| | | 115 | | /// <param name="documents">The documents to upload.</param> |
| | | 116 | | /// <returns>A new IndexBatch.</returns> |
| | | 117 | | public static IndexBatch<T> Upload<T>(IEnumerable<T> documents) |
| | | 118 | | { |
| | 438 | 119 | | Throw.IfArgumentNull(documents, "documents"); |
| | | 120 | | |
| | 27984 | 121 | | return New(documents.Select(d => IndexAction.Upload(d))); |
| | | 122 | | } |
| | | 123 | | } |
| | | 124 | | } |