@@ -30,7 +30,7 @@ import scala.util.{Failure, Success}
3030
3131/** Update Management API
3232 */
33- trait UpdateApi extends ElasticClientHelpers { _ : RefreshApi with SerializationApi =>
33+ trait UpdateApi extends ElasticClientHelpers { _ : SettingsApi with SerializationApi =>
3434
3535 // ========================================================================
3636 // PUBLIC METHODS
@@ -45,10 +45,18 @@ trait UpdateApi extends ElasticClientHelpers { _: RefreshApi with SerializationA
4545 * - the source of the entity to update in JSON format
4646 * @param upsert
4747 * - true to upsert the entity if it does not exist, false otherwise
48+ * @param wait
49+ * - whether to wait for a refresh to happen after updating (default is false)
4850 * @return
4951 * true if the entity was updated successfully, false otherwise
5052 */
51- def update (index : String , id : String , source : String , upsert : Boolean ): ElasticResult [Boolean ] = {
53+ def update (
54+ index : String ,
55+ id : String ,
56+ source : String ,
57+ upsert : Boolean ,
58+ wait : Boolean = false
59+ ): ElasticResult [Boolean ] = {
5260 validateIndexName(index) match {
5361 case Some (error) =>
5462 return ElasticResult .failure(
@@ -77,10 +85,12 @@ trait UpdateApi extends ElasticClientHelpers { _: RefreshApi with SerializationA
7785
7886 logger.debug(s " Updating document with id ' $id' in index ' $index' " )
7987
80- executeUpdate(index, id, source, upsert) match {
81- case ElasticSuccess (true ) =>
88+ // if wait for next refresh is enabled, we should make sure that the refresh is enabled (different to -1)
89+ val waitEnabled = wait && isRefreshEnabled(index).getOrElse(false )
90+ executeUpdate(index, id, source, upsert, waitEnabled) match {
91+ case success @ ElasticSuccess (true ) =>
8292 logger.info(s " ✅ Successfully updated document with id ' $id' in index ' $index' " )
83- this .refresh(index)
93+ success
8494 case ElasticSuccess (false ) =>
8595 val error = s " Document with id ' $id' in index ' $index' not updated "
8696 logger.warn(s " ❌ $error" )
@@ -110,6 +120,8 @@ trait UpdateApi extends ElasticClientHelpers { _: RefreshApi with SerializationA
110120 * - the type of the entity (default is the entity class name in lowercase)
111121 * @param upsert
112122 * - true to upsert the entity if it does not exist, false otherwise
123+ * @param wait
124+ * - whether to wait for a refresh to happen after updating (default is false)
113125 * @return
114126 * true if the entity was updated successfully, false otherwise
115127 */
@@ -118,7 +130,8 @@ trait UpdateApi extends ElasticClientHelpers { _: RefreshApi with SerializationA
118130 id : String ,
119131 index : Option [String ] = None ,
120132 maybeType : Option [String ] = None ,
121- upsert : Boolean = true
133+ upsert : Boolean = true ,
134+ wait : Boolean = false
122135 )(implicit u : ClassTag [U ], formats : Formats ): ElasticResult [Boolean ] = {
123136 val indexType = maybeType.getOrElse(u.runtimeClass.getSimpleName.toLowerCase)
124137 val indexName = index.getOrElse(indexType)
@@ -128,7 +141,7 @@ trait UpdateApi extends ElasticClientHelpers { _: RefreshApi with SerializationA
128141 serialization.write[U ](entity)
129142 }
130143 .flatMap { source =>
131- this .update(indexName, id, source, upsert)
144+ this .update(indexName, id, source, upsert, wait )
132145 }
133146 }
134147
@@ -141,10 +154,18 @@ trait UpdateApi extends ElasticClientHelpers { _: RefreshApi with SerializationA
141154 * - the source of the entity to update in JSON format
142155 * @param upsert
143156 * - true to upsert the entity if it does not exist, false otherwise
157+ * @param wait
158+ * - whether to wait for a refresh to happen after updating (default is false)
144159 * @return
145160 * a Future that completes with true if the entity was updated successfully, false otherwise
146161 */
147- def updateAsync (index : String , id : String , source : String , upsert : Boolean )(implicit
162+ def updateAsync (
163+ index : String ,
164+ id : String ,
165+ source : String ,
166+ upsert : Boolean ,
167+ wait : Boolean = false
168+ )(implicit
148169 ec : ExecutionContext
149170 ): Future [ElasticResult [Boolean ]] = {
150171 validateIndexName(index) match {
@@ -179,13 +200,15 @@ trait UpdateApi extends ElasticClientHelpers { _: RefreshApi with SerializationA
179200
180201 logger.debug(s " Updating document with id ' $id' in index ' $index' asynchronously " )
181202
203+ // if wait for next refresh is enabled, we should make sure that the refresh is enabled (different to -1)
204+ val waitEnabled = wait && isRefreshEnabled(index).getOrElse(false )
182205 val promise : Promise [ElasticResult [Boolean ]] = Promise ()
183- executeUpdateAsync(index, id, source, upsert) onComplete {
206+ executeUpdateAsync(index, id, source, upsert, waitEnabled ) onComplete {
184207 case Success (s) =>
185208 s match {
186- case _ @ ElasticSuccess (true ) =>
209+ case success @ ElasticSuccess (true ) =>
187210 logger.info(s " ✅ Successfully updated document with id ' $id' in index ' $index' " )
188- promise.success(this .refresh(index) )
211+ promise.success(success )
189212 case success @ ElasticSuccess (_) =>
190213 logger.warn(s " ❌ Document with id ' $id' in index ' $index' not updated " )
191214 promise.success(success)
@@ -217,6 +240,8 @@ trait UpdateApi extends ElasticClientHelpers { _: RefreshApi with SerializationA
217240 * - the type of the entity (default is the entity class name in lowercase)
218241 * @param upsert
219242 * - true to upsert the entity if it does not exist, false otherwise
243+ * @param wait
244+ * - whether to wait for a refresh to happen after updating (default is false)
220245 * @return
221246 * a Future that completes with true if the entity was updated successfully, false otherwise
222247 */
@@ -225,7 +250,8 @@ trait UpdateApi extends ElasticClientHelpers { _: RefreshApi with SerializationA
225250 id : String ,
226251 index : Option [String ] = None ,
227252 maybeType : Option [String ] = None ,
228- upsert : Boolean = true
253+ upsert : Boolean = true ,
254+ wait : Boolean = false
229255 )(implicit
230256 u : ClassTag [U ],
231257 ec : ExecutionContext ,
@@ -240,7 +266,7 @@ trait UpdateApi extends ElasticClientHelpers { _: RefreshApi with SerializationA
240266 case failure @ ElasticFailure (_) =>
241267 logger.error(s " ❌ Failed to serialize entity for update in index ' $indexName' " )
242268 Future .successful(failure)
243- case ElasticSuccess (source) => this .updateAsync(indexName, id, source, upsert)
269+ case ElasticSuccess (source) => this .updateAsync(indexName, id, source, upsert, wait )
244270 }
245271 }
246272
@@ -252,13 +278,15 @@ trait UpdateApi extends ElasticClientHelpers { _: RefreshApi with SerializationA
252278 index : String ,
253279 id : String ,
254280 source : String ,
255- upsert : Boolean
281+ upsert : Boolean ,
282+ wait : Boolean
256283 ): ElasticResult [Boolean ]
257284
258285 private [client] def executeUpdateAsync (
259286 index : String ,
260287 id : String ,
261288 source : String ,
262- upsert : Boolean
289+ upsert : Boolean ,
290+ wait : Boolean
263291 )(implicit ec : ExecutionContext ): Future [ElasticResult [Boolean ]]
264292}
0 commit comments