Skip to content

Commit

Permalink
Add option to match for strings
Browse files Browse the repository at this point in the history
  • Loading branch information
Casmo committed Aug 13, 2024
1 parent 0221611 commit 6031a5f
Showing 1 changed file with 76 additions and 30 deletions.
106 changes: 76 additions & 30 deletions src/Engines/OpenSearchEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,50 +147,96 @@ protected function performSearch(Builder $builder, array $options = []): mixed
return \call_user_func($builder->callback, $this->client, $builder->query, $options);
}

$query = $builder->query;
$must = collect([
[
'query_string' => [
'query' => $query,
$options['query'] = [
'bool' => [
'must' => [
[
'query_string' => [
'query' => $builder->query
]
]
],
],
]);
$must = $must->merge(collect($builder->wheres)
->map(static fn ($value, $key): array => [
'term' => [
$key => $value,
'should' => [

],
])->values())->values();
'must_not' => [

if (property_exists($builder, 'whereIns')) {
$must = $must->merge(collect($builder->whereIns)->map(static fn ($values, $key): array => [
'terms' => [
$key => $values,
],
])->values())->values();
'filter' => [
]
]
];

collect($builder->wheres)
->each(function ($value, $key) use (&$options) {
if (is_string($value)) {
$options['query']['bool']['must'][] = [
'match' => [
$key => $value
]
];
}
else {
$options['query']['bool']['must'][] = [
'term' => [
$key => $value
]
];
}
});

if (property_exists($builder, 'whereIns')) {
collect($builder->whereIns)
->each(function ($values, $key) use (&$options) {
if (is_string($values[0])) {
$options['query']['bool']['must'][] = [
'match' => [
$key => [
'query' => implode(' ', $values),
'operator' => 'or',
'minimum_should_match' => 1
]
]
];
}
else {
$options['query']['bool']['must'][] = [
'terms' => [
$key => $values
]
];
}
});
}

$mustNot = collect();
if (property_exists($builder, 'whereNotIns')) {
$mustNot = $mustNot->merge(collect($builder->whereNotIns)->map(static fn ($values, $key): array => [
'terms' => [
$key => $values,
],
])->values())->values();
collect($builder->whereNotIns)
->each(function ($values, $key) use (&$options) {
if (is_string($values[0])) {
foreach ($values as $value) {
$options['query']['bool']['must_not'][] = [
'match' => [
$key => $value
]
];
}
}
else {
$options['query']['bool']['must_not'][] = [
'terms' => [
$key => $values
]
];
}
});
}

$options['query'] = [
'bool' => [
'must' => $must->all(),
'must_not' => $mustNot->all(),
],
];

$options['sort'] = collect($builder->orders)->map(static fn ($order): array => [
$order['column'] => [
'order' => $order['direction'],
],
])->all();

$result = $this->client->search([
'index' => $index,
'body' => $options,
Expand Down

0 comments on commit 6031a5f

Please sign in to comment.