@@ -71,6 +71,7 @@ class Builder extends BaseEloquentBuilder
71
71
'bucketaggregation ' ,
72
72
'openpit ' ,
73
73
'bulkinsert ' ,
74
+ 'createonly ' ,
74
75
];
75
76
76
77
/**
@@ -95,7 +96,7 @@ public function setModel($model): static
95
96
public function newModelInstance ($ attributes = [])
96
97
{
97
98
$ model = $ this ->model ->newInstance ($ attributes )->setConnection (
98
- $ this ->query ->getConnection () ->getName ()
99
+ $ this ->query ->connection ->getName ()
99
100
);
100
101
101
102
// Merge in our options.
@@ -216,7 +217,7 @@ public function hydrate(array $items)
216
217
$ instance = $ this ->newModelInstance ();
217
218
218
219
return $ instance ->newCollection (array_map (function ($ item ) use ($ instance ) {
219
- return $ instance ->newFromBuilder ($ item , $ this ->getConnection () ->getName ());
220
+ return $ instance ->newFromBuilder ($ item , $ this ->query -> connection ->getName ());
220
221
}, $ items ));
221
222
}
222
223
@@ -394,6 +395,17 @@ public function withoutRefresh()
394
395
return $ this ->model ;
395
396
}
396
397
398
+ /**
399
+ * Explicitly control the Elasticsearch refresh behavior for write ops.
400
+ * Accepts: true, false, or 'wait_for'.
401
+ */
402
+ public function withRefresh (bool |string $ refresh ): static
403
+ {
404
+ $ this ->query ->options ()->add ('refresh ' , $ refresh );
405
+
406
+ return $ this ;
407
+ }
408
+
397
409
/**
398
410
* @throws DynamicIndexException
399
411
*/
@@ -644,6 +656,27 @@ public function rawDsl($dsl): array
644
656
return $ this ->query ->raw ($ dsl )->asArray ();
645
657
}
646
658
659
+ /**
660
+ * Force insert operations to use op_type=create for dedupe semantics.
661
+ * When set, attempts to create an existing _id will fail with a 409 from Elasticsearch.
662
+ */
663
+ public function createOnly (): static
664
+ {
665
+ // mark insert op type on the underlying query options
666
+ $ this ->query ->options ()->add ('insert_op_type ' , 'create ' );
667
+
668
+ return $ this ;
669
+ }
670
+
671
+ /**
672
+ * Convenience method to perform a create-only insert and surface 409s as exceptions.
673
+ * Accepts single document attributes or an array of documents.
674
+ */
675
+ public function createOrFail (array $ attributes )
676
+ {
677
+ return $ this ->createOnly ()->create ($ attributes );
678
+ }
679
+
647
680
// ----------------------------------------------------------------------
648
681
// Protected
649
682
// ----------------------------------------------------------------------
0 commit comments