Skip to content

Commit 42038e5

Browse files
committed
upsert
1 parent 4857c68 commit 42038e5

3 files changed

Lines changed: 181 additions & 124 deletions

File tree

src/Eloquent/Docs/ModelDocs.php

Lines changed: 103 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace PDPhilip\Elasticsearch\Eloquent\Docs;
66

77
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
8-
use PDPhilip\Elasticsearch\Data\MetaDTO;
8+
use Illuminate\Pagination\Cursor;
99
use PDPhilip\Elasticsearch\Eloquent\Builder;
1010
use PDPhilip\Elasticsearch\Eloquent\ElasticCollection;
1111
use PDPhilip\Elasticsearch\Eloquent\Model;
@@ -15,7 +15,7 @@
1515
* Query Builder Methods ---------------------------------
1616
*
1717
* @method static $this query()
18-
* @method static Builder dslQuery()
18+
* @method static $this dslQuery()
1919
*-----------------------------------
2020
* @method static $this where($column, $operator = null, $value = null, $boolean = 'and', $options = [])
2121
* @method static $this whereNot($column, $operator = null, $value = null, $boolean = 'and', $options = [])
@@ -85,146 +85,140 @@
8585
* @method static $this whereTermExists($column, $boolean = 'and', $not = false)
8686
* @method static $this whereNotTermExists($column)
8787
* @method static $this orWhereTermExists($column)
88-
* @method static $this orWhereNotTermExists($column)
88+
* @method static $this orWhereNotTermsExists($column)
8989
*-----------------------------------
9090
* @method static $this whereRaw($dsl, $bindings = [], $boolean = 'and', $options = [])
9191
*===========================================
9292
* Full Text Search Methods
9393
*===========================================
94-
* @method static $this searchTerm($query, mixed $columns = null, $options = [])
95-
* @method static $this orSearchTerm($query, mixed $columns = null, $options = [])
96-
* @method static $this searchNotTerm($query, mixed $columns = null, $options = [])
97-
* @method static $this orSearchNotTerm($query, mixed $columns = null, $options = [])
98-
*-----------------------------------
99-
* @method static $this searchTermMost($query, mixed $columns = null, $options = [])
100-
* @method static $this orSearchTermMost($query, mixed $columns = null, $options = [])
101-
* @method static $this searchNotTermMost($query, mixed $columns = null, $options = [])
102-
* @method static $this orSearchNotTermMost($query, mixed $columns = null, $options = [])
103-
*-----------------------------------
104-
* @method static $this searchTermCross($query, mixed $columns = null, $options = [])
105-
* @method static $this orSearchTermCross($query, mixed $columns = null, $options = [])
106-
* @method static $this searchNotTermCross($query, mixed $columns = null, $options = [])
107-
* @method static $this orSearchNotTermCross($query, mixed $columns = null, $options = [])
108-
*-----------------------------------
109-
* @method static $this searchPhrase($phrase, mixed $columns = null, $options = [])
110-
* @method static $this orSearchPhrase($phrase, mixed $columns = null, $options = [])
111-
* @method static $this searchNotPhrase($phrase, mixed $columns = null, $options = [])
112-
* @method static $this orSearchNotPhrase($phrase, mixed $columns = null, $options = [])
113-
*-----------------------------------
114-
* @method static $this searchPhrasePrefix($phrase, mixed $columns = null, $options = [])
115-
* @method static $this orSearchPhrasePrefix($phrase, mixed $columns = null, $options = [])
116-
* @method static $this searchNotPhrasePrefix($phrase, mixed $columns = null, $options = [])
117-
* @method static $this orSearchNotPhrasePrefix($phrase, mixed $columns = null, $options = [])
118-
*-----------------------------------
119-
* @method static $this searchBoolPrefix($query, mixed $columns = null, $options = [])
120-
* @method static $this orSearchBoolPrefix($query, mixed $columns = null, $options = [])
121-
* @method static $this searchNotBoolPrefix($query, mixed $columns = null, $options = [])
122-
* @method static $this orSearchNotBoolPrefix($query, mixed $columns = null, $options = [])
123-
*-----------------------------------
124-
* @method static $this searchFuzzy($query, mixed $columns = null, $options = [])
125-
* @method static $this orSearchFuzzy($query, mixed $columns = null, $options = [])
126-
* @method static $this searchNotFuzzy($query, mixed $columns = null, $options = [])
127-
* @method static $this orSearchNotFuzzy($query, mixed $columns = null, $options = [])
128-
*-----------------------------------
129-
* @method static $this searchFuzzyPrefix($query, mixed $columns = null, $options = [])
130-
* @method static $this orSearchFuzzyPrefix($query, mixed $columns = null, $options = [])
131-
* @method static $this searchNotFuzzyPrefix($query, mixed $columns = null, $options = [])
132-
* @method static $this orSearchNotFuzzyPrefix($query, mixed $columns = null, $options = [])
133-
*-----------------------------------
134-
* @method static $this searchQueryString(mixed $query, mixed $columns = null, $options = [])
135-
* @method static $this orSearchQueryString(mixed $query, mixed $columns = null, $options = [])
136-
* @method static $this searchNotQueryString(mixed $query, mixed $columns = null, $options = [])
137-
* @method static $this orSearchNotQueryString(mixed $query, mixed $columns = null, $options = [])
94+
* @method static $this searchTerm($term, $fields = ['*'], $options = [], $boolean = 'and')
95+
* @method static $this orSearchTerm($term, $fields = ['*'], $options = [])
96+
* @method static $this searchNotTerm($term, $fields = ['*'], $options = [])
97+
* @method static $this orSearchNotTerm($term, $fields = ['*'], $options = [])
98+
*-----------------------------------
99+
* @method static $this searchTermMost($term, $fields = ['*'], $options = [], $boolean = 'and')
100+
* @method static $this orSearchTermMost($term, $fields = ['*'], $options = [])
101+
* @method static $this searchNotTermMost($term, $fields = ['*'], $options = [])
102+
* @method static $this orSearchNotTermMost($term, $fields = ['*'], $options = [])
103+
*-----------------------------------
104+
* @method static $this searchTermCross($term, $fields = ['*'], $options = [], $boolean = 'and')
105+
* @method static $this orSearchTermCross($term, $fields = ['*'], $options = [])
106+
* @method static $this searchNotTermCross($term, $fields = ['*'], $options = [])
107+
* @method static $this orSearchNotTermCross($term, $fields = ['*'], $options = [])
108+
*-----------------------------------
109+
* @method static $this searchPhrase($phrase, $fields = ['*'], $options = [], $boolean = 'and')
110+
* @method static $this orSearchPhrase($phrase, $fields = ['*'], $options = [])
111+
* @method static $this searchNotPhrase($phrase, $fields = ['*'], $options = [])
112+
* @method static $this orSearchNotPhrase($phrase, $fields = ['*'], $options = [])
113+
*-----------------------------------
114+
* @method static $this searchPhrasePrefix($phrase, $fields = ['*'], $options = [], $boolean = 'and')
115+
* @method static $this orSearchPhrasePrefix($phrase, $fields = ['*'], $options = [])
116+
* @method static $this searchNotPhrasePrefix($phrase, $fields = ['*'], $options = [])
117+
* @method static $this orSearchNotPhrasePrefix($phrase, $fields = ['*'], $options = [])
118+
*-----------------------------------
119+
* @method static $this searchBoolPrefix($phrase, $fields = ['*'], $options = [], $boolean = 'and')
120+
* @method static $this orSearchBoolPrefix($phrase, $fields = ['*'], $options = [])
121+
*-----------------------------------
122+
* @method static $this searchFuzzy($term, $fields = ['*'], $options = [])
123+
* @method static $this orSearchFuzzy($term, $fields = ['*'], $options = [])
124+
* @method static $this searchNotFuzzy($term, $fields = ['*'], $options = [])
125+
* @method static $this orSearchNotFuzzy($term, $fields = ['*'], $options = [])
126+
*-----------------------------------
127+
* @method static $this searchFuzzyPrefix($term, $fields = ['*'], $options = [])
128+
* @method static $this orSearchFuzzyPrefix($term, $fields = ['*'], $options = [])
129+
* @method static $this searchNotFuzzyPrefix($term, $fields = ['*'], $options = [])
130+
* @method static $this orSearchNotFuzzyPrefix($term, $fields = ['*'], $options = [])
131+
*-----------------------------------
132+
* @method static $this searchQueryString($query, $fields = null, $options = [])
133+
* @method static $this orSearchQueryString($query, $fields = null, $options = [])
134+
* @method static $this searchNotQueryString($query, $fields = null, $options = [])
135+
* @method static $this orSearchNotQueryString($query, $fields = null, $options = [])
138136
*===========================================
139-
* Specialty Methods
137+
* Speciality methods
140138
*===========================================
141-
* @method static $this whereScript(string $script, array $options = [], $boolean = 'and')
142-
* @method static $this orWhereScript(string $script, array $options = [])
139+
* @method static $this whereScript($script, $options = [], $boolean = 'and')
140+
* @method static $this orWhereScript($script, $options = [])
143141
*-----------------------------------
144-
* @method static $this highlight($columns = ['*'], $preTag = '<em>', $postTag = '</em>', array $options = [])
142+
* @method static $this highlight($fields = [], $preTag = '<em>', $postTag = '</em>', $globalOptions = [])
143+
* @method static $this withoutRefresh()
145144
*-----------------------------------
146-
* @method static $this whereChild(string $documentType, \Closure $callback, $options = [], $boolean = 'and')
147-
* @method static $this whereParent(string $documentType, \Closure $callback, $options = [], $boolean = 'and')
148-
* @method static $this whereParentId(string $parentType, $id, string $boolean = 'and')
145+
* @method static $this whereChild($documentType, $callback, $options = [], $boolean = 'and')
146+
* @method static $this whereParent($documentType, $callback, $options = [], $boolean = 'and')
147+
* @method static $this whereParentId($parentType, $id, $boolean = 'and')
149148
*-----------------------------------
150-
* @method static $this excludeFields(string|array $fields)
151-
* @method static $this withAnalyzer(string $analyzer)
152-
* @method static $this withMinScore(float $val)
149+
* @method static $this excludeFields($fields)
150+
* @method static $this withAnalyzer($analyzer)
151+
* @method static $this withMinScore($val)
153152
* @method static $this withSuffix($suffix)
154-
* @method static $this withTrackTotalHits(bool|int|null $val = true)
155-
* @method static $this withoutTrackTotalHits()
156153
*-----------------------------------
157-
* @method static $this routing(string $routing)
158-
* @method static $this parentId(string $id)
154+
* @method static $this routing($routing)
155+
* @method static $this parentId($id)
159156
*-----------------------------------
160157
* @method static $this proceedOnConflicts()
161-
* @method static $this onConflicts(string $option = 'proceed')
158+
* @method static $this onConflicts($option = 'proceed')
162159
*-----------------------------------
163-
* @method static Model withoutRefresh()
164-
* @method static Model withRefresh(bool|string $refresh)
165-
* @method static Model withOpType(string $value)
166-
* @method static Model createOnly()
160+
* @method static $this push($column, $value = null, $unique = false)
161+
* @method static $this pull($column, $value = null)
167162
*-----------------------------------
168-
* @method static int push(string $column, $value = null, $unique = false)
169-
* @method static int pull(string $column, $value = null)
170-
*-----------------------------------
171-
* @method static int incrementEach($columns, $extra = [])
172-
* @method static int decrementEach($columns, $extra = [])
163+
* @method static $this incrementEach($columns, $extra = [])
164+
* @method static $this decrementEach($columns, $extra = [])
173165
*===========================================
174-
* Filter and Order Methods
166+
* Filter and order methods
175167
*===========================================
176-
* @method static $this orderBy($column, $direction = 1, array $options = [])
177-
* @method static $this orderByDesc($column, array $options = [])
168+
* @method static $this orderBy($column, $direction = 'asc',$options = [])
169+
* @method static $this orderByDesc($column,$options = [])
178170
*-----------------------------------
179-
* @method static $this groupBy(...$groups)
180-
* @method static $this groupByRanges($column, $ranges = [])
181-
* @method static $this groupByDateRanges($column, $ranges = [], $options = [])
171+
* @method static $this groupBy($groups)
172+
* @method static $this groupByRanges($column, array $ranges)
173+
* @method static $this groupByDateRanges($column, array $ranges, array $options = [])
182174
*-----------------------------------
183-
* @method static $this orderByGeo(string $column, array $coordinates, $direction = 1, array $options = [])
184-
* @method static $this orderByGeoDesc(string $column, array $coordinates, array $options = [])
175+
* @method static $this orderByGeo($column, $pin, $direction = 'asc', $options = [])
176+
* @method static $this orderByGeoDesc($column, $pin, $options = [])
185177
*-----------------------------------
186-
* @method static $this orderByNested(string $column, $direction = 1, string $mode = 'avg')
187-
* @method static $this orderByNestedDesc(string $column, string $mode = 'avg')
178+
* @method static $this orderByNested($column, $direction = 'asc', $mode = null)
179+
* @method static $this orderByNestedDesc($column, $direction = 'asc', $mode = null)
188180
*-----------------------------------
189-
* @method static $this withSort(string $column, $key, $value)
181+
* @method static $this withSort($column, $key, $value)
182+
* @method static $this withRefresh($type)
190183
*===========================================
191184
* Executors
192185
*===========================================
193186
* @method static Model|null find($id)
194187
* @method static Model|null first($columns = ['*'])
195188
* @method static Model firstOrCreate($attributes, $values = [])
196-
* @method static Model createOrFail(array $attributes)
197189
*-----------------------------------
198190
* @method static array getModels($columns = ['*'])
199191
* @method static ElasticCollection get($columns = ['*'])
200-
* @method static MetaDTO|bool insert(array $values)
201-
* @method static array bulkInsert(array $values)
202-
* @method static int upsert(array $values, array|string $uniqueBy, ?array $update = null)
203-
* @method static Model create(array $attributes)
192+
* @method static ElasticCollection insert($values, $returnData = null)
193+
* @method static $this create(array $attributes)
194+
* @method static $this createOnly()
195+
* @method static $this createOrFail(array $attributes)
204196
*-----------------------------------
205-
* @method static array|string toDsl()
197+
* @method static array toDsl($columns = ['*'])
198+
* @method static array toSql($columns = ['*'])
206199
*-----------------------------------
207-
* @method static ElasticCollection rawSearch($dslBody, $options = [])
208-
* @method static array rawAggregation($dslBody, $options = [])
209-
* @method static array rawDsl($dsl)
200+
* @method static mixed rawDsl($bodyParams)
201+
* @method static ElasticCollection rawSearch($bodyParams)
202+
* @method static array rawAggregation($bodyParams)
210203
*-----------------------------------
211-
* @method static LengthAwarePaginator paginate($perPage = 15, $columns = ['*'], $pageName = 'page', $page = null, $total = null)
212-
* @method static SearchAfterPaginator cursorPaginate($perPage = null, $columns = ['*'], $cursorName = 'cursor', $cursor = null)
204+
* @method static LengthAwarePaginator paginate($perPage = 15, $columns = ['*'], $pageName = 'page', $page = null, $total = null)
205+
* @method static SearchAfterPaginator cursorPaginate($perPage = null, $columns = [], $cursorName = 'cursor', $cursor = null)
213206
*-----------------------------------
214-
* @method static bool chunk($count, callable $callback, $scrollTimeout = '30s')
207+
* @method static bool chunk($count, $callback)
215208
* @method static bool chunkById($count, $callback, $column = '_id', $alias = null)
216209
*-----------------------------------
217-
* @method static ElasticCollection distinct(mixed $columns = [], bool $includeCount = false)
218-
* @method static ElasticCollection bulkDistinct(array $columns = [], bool $includeCount = false)
210+
* @method static ElasticCollection distinct($columns = [], $includeCount = false)
211+
* @method static ElasticCollection bulkDistinct($columns = [], $includeCount = false)
219212
*===========================================
220-
* Aggregation Methods
213+
* Aggregators Methods
221214
*===========================================
222-
* @method static int|array sum($column, array $options = [])
223-
* @method static int|array min($column, array $options = [])
224-
* @method static int|array max($column, array $options = [])
225-
* @method static int|array avg($column, array $options = [])
226-
* @method static mixed agg(array $functions, string|array $columns, array $options = [])
227-
* @method static mixed aggregate($function, $columns = ['*'], $options = [])
215+
* @method static int|array sum($columns)
216+
* @method static int|array min($columns)
217+
* @method static int|array max($columns)
218+
* @method static int|array avg($columns)
219+
* @method static int|array average($columns)
220+
* @method static mixed agg(array $functions, $column)
221+
* @method static mixed aggregate(array $functions, $column)
228222
*-----------------------------------
229223
* @method static mixed boxplot($columns, $options = [])
230224
* @method static mixed cardinality($columns, $options = [])
@@ -237,21 +231,17 @@
237231
*-----------------------------------
238232
* @method static array getAggregationResults()
239233
* @method static array getRawAggregationResults()
240-
* @method static mixed getGroupByAfterKey($offset)
241234
*===========================================
242235
* Index Methods
243236
*===========================================
244237
* @method static void truncate()
245238
* @method static bool indexExists()
246-
* @method static void deleteIndexIfExists()
247-
* @method static void deleteIndex()
248-
* @method static bool createIndex(?\Closure $callback = null)
249-
* @method static array getIndexMappings(bool $raw = false)
250-
* @method static array getFieldMappings(bool $raw = false)
251-
* @method static array getFieldMapping(string|array $field = '*', bool $raw = false)
252-
* @method static array getIndexSettings()
253-
* @method static bool hasField(string $column)
254-
* @method static bool hasFields(array $columns)
239+
* @method static bool deleteIndexIfExists()
240+
* @method static bool deleteIndex()
241+
* @method static bool createIndex($callback)
242+
* @method static array getIndexMappings()
243+
* @method static array getFieldMapping(string|array $field = '*', $raw = false)
244+
* @method static array getIndexOptions()
255245
*
256246
* @property object $search_highlights
257247
* @property object $with_highlights

src/Query/Builder.php

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -810,16 +810,9 @@ private function buildUpsertKey(array $doc, array $uniqueBy): string
810810
*/
811811
private function findExistingIds(array $uniqueBy, array $lookupValues): array
812812
{
813-
// Build a fresh query against the same index
814-
$query = $this->newQuery();
815-
816-
if (count($uniqueBy) === 1) {
817-
// Single field: simple whereIn
818-
$field = $uniqueBy[0];
819-
$fieldValues = array_unique(array_map(fn ($key) => explode('|', $key)[0], $lookupValues));
820-
$results = $query->whereIn($field, $fieldValues)->get();
821-
} else {
822-
// Multi-field: use bool should with exact match per combination
813+
try {
814+
$query = $this->newQuery($this->from);
815+
823816
foreach (array_unique($lookupValues) as $key) {
824817
$parts = explode('|', $key);
825818
$query->orWhere(function ($q) use ($uniqueBy, $parts) {
@@ -829,9 +822,11 @@ private function findExistingIds(array $uniqueBy, array $lookupValues): array
829822
});
830823
}
831824
$results = $query->get();
825+
} catch (\Throwable) {
826+
// Index doesn't exist yet or field has no keyword mapping
827+
return [];
832828
}
833829

834-
// Map lookup_key => _id
835830
$map = [];
836831
foreach ($results as $result) {
837832
$key = $this->buildUpsertKey((array) $result, $uniqueBy);

0 commit comments

Comments
 (0)