Skip to content

Commit

Permalink
Merge pull request #3 from Hilsonxhero/dev
Browse files Browse the repository at this point in the history
chore : update core
  • Loading branch information
Hilsonxhero authored Mar 30, 2023
2 parents 91f477e + d175d6b commit d8d347c
Show file tree
Hide file tree
Showing 14 changed files with 195 additions and 49 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"require": {
"php": "8.0.*||8.1.*||8.2.*",
"illuminate/support": "^9.0||^10.0",
"laravel/scout": "^9.0",
"laravel/scout": "^9.0||^10.0",
"webmozart/assert": "^1.10",
"elasticsearch/elasticsearch": "^8.6"
},
Expand Down
32 changes: 26 additions & 6 deletions docs/advanced-queries.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Advanced queries

ElasticVision expands your possibilities using query builders to write more complex queries.
Explorer expands your possibilities using query builders to write more complex queries.
First there are the three methods to set the context of the query: must, should and filter.

From the Elasticsearch [documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html):
Expand All @@ -11,12 +11,12 @@ From the Elasticsearch [documentation](https://www.elastic.co/guide/en/elasticse

**filter**: The query must appear in matching documents. However unlike must the score of the query will be ignored.

Together they take in ElasticVision a _more-matches-is-better_ approach, the more a document matches with the given queries, the higher its score.
Together they take in Explorer a _more-matches-is-better_ approach, the more a document matches with the given queries, the higher its score.
The filter context is best used for structured data, such as dates or flags.

In ElasticVision, you can build a compound query as complex as you like. Elasticsearch provides a broad set of queries,
In Explorer, you can build a compound query as complex as you like. Elasticsearch provides a broad set of queries,
these are the query types implementing `SyntaxInterface`. There is for example the `MultiMatch` for fuzzy search and Term for a very precise search.
It is too much to list every type of query here. At the time of writing, ElasticVision does not yet have every Elasticsearch query type that is out there.
It is too much to list every type of query here. At the time of writing, Explorer does not yet have every Elasticsearch query type that is out there.
It is however very easy to write a class for a missing query type, and if you do write one a Pull Request is more than welcome!

## Fuzziness
Expand All @@ -28,7 +28,7 @@ By default, it is set to 'auto' but the [Elasticsearch docs](https://www.elastic
## Retrieving selected fields

By default ElasticVision will retrieve all fields for the documents that get returned.
By default Explorer will retrieve all fields for the documents that get returned.
You can change this by using the `field()` function on the search query builder.
It is important to know that this does necessarily have a performance improvement, the whole document is still being processed by Elasticsearch.

Expand All @@ -49,7 +49,11 @@ $results = Post::search('Self-steering')
Elastic has a multitude of options one can add to its queries.

In Explorer one can easily add these with Query Properties, you can create a class , implement the `QueryProperty` interface
and pass it to the query. An example of this is source field filtering which we included in the SourceFilter class.
and pass it to the query.

### Source filter

An example of this is source field filtering which we included in the SourceFilter class.

```php
use App\Models\Post;
Expand All @@ -60,3 +64,19 @@ $results = Post::search('Self-steering')
->property(SourceFilter::empty()->include('*.description')->exclude('*_secret'))
->get();
```

### Track Total Hits

To add the `track_total_hits` query property you can use the `TrackTotalHits` query parameter. See the example below to
add `"track_total_hits": true` to the query. Other alternatives are `TrackTotalHits::none()` for `"track_total_hits": false`
and `TrackTotalHits::count((int)$c)` for `"track_total_hits": $c`.

```php
use App\Models\Post;
use Hilsonxhero\ElasticVision\Domain\Query\QueryProperties\TrackTotalHits;

$results = Post::search('Self-steering')
->field('id')
->property(TrackTotalHits::all())
->get();
```
21 changes: 18 additions & 3 deletions docs/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,30 @@ Use Laravel Scout import command to create and update indices.
php artisan scout:import <model>
```

For example, if your model is "App\Models\Post.php" then command would be like this:
For example, if your model is "App\Models\Post" then command would be like this:

```php
php artisan scout:import "App\Models\Post.php"
```
php artisan scout:import "App\Models\Post"
```

If you want to recreate an index, first make sure it's deleted and then create it.
Follow up with a scout import to refill the index as well.

## Update Aliased Indexes

If you are using Aliased Indexes, you should use this command instead of `scout:import`

```
php artisan elastic:update <index?>
```

You can specify an index or choose to omit it and the command will update all your indexes.
For example, if your model is "App\Model\Post" and the index is "posts":

```
php artisan elastic:update posts
```

## Delete

```
Expand Down
2 changes: 1 addition & 1 deletion docs/index-aliases.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ If you wish to keep the old indices set `prune_old_aliases` to false in `config/

A model is only using index aliases if it implements the Aliased interface or enabled in the configuration (see [mapping](mapping.md)).

After that, any time you use the `scout:import` command a new index will be created and when the insertion of models is done the alias will be pointed to the new index.
After that, any time you use the `elastic:update` command a new index will be created and when the insertion of models is done the alias will be pointed to the new index.

```php
<?php
Expand Down
36 changes: 36 additions & 0 deletions src/Domain/Query/QueryProperties/TrackTotalHits.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace Hilsonxhero\ElasticVision\Domain\Query\QueryProperties;

class TrackTotalHits implements QueryProperty
{
/** @param int|bool $trackTotalHits */
private function __construct(
private mixed $trackTotalHits,
) {
}

public static function all(): self
{
return new self(true);
}

public static function none(): self
{
return new self(false);
}

public static function count(int $count): self
{
return new self($count);
}

public function build(): array
{
return [
'track_total_hits' => $this->trackTotalHits,
];
}
}
35 changes: 35 additions & 0 deletions src/Domain/Syntax/RegExp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace Hilsonxhero\ElasticVision\Domain\Syntax;

class RegExp implements SyntaxInterface
{
private string $field;

private ?string $value;

private string $flags;

private bool $insensitive;

public function __construct(string $field, string $value = null, string $flags = 'ALL', bool $insensitive = false)
{
$this->field = $field;
$this->value = $value;
$this->flags = $flags;
$this->insensitive = $insensitive;
}

public function build(): array
{
return ['regexp' => [
$this->field => [
'value' => $this->value,
'flags' => $this->flags,
'case_insensitive' => $this->insensitive,
],
]];
}
}
8 changes: 3 additions & 5 deletions src/Infrastructure/Elastic/ElasticDocumentAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@

final class ElasticDocumentAdapter implements DocumentAdapterInterface
{
private Client $client;

public function __construct(ElasticClientFactory $clientFactory)
{
$this->client = $clientFactory->client();
public function __construct(
private Client $client,
) {
}

public function bulk(BulkOperationInterface $command)
Expand Down
3 changes: 2 additions & 1 deletion src/Infrastructure/Elastic/ElasticIndexAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ public function createNewWriteIndex(IndexConfigurationInterface $indexConfigurat
$this->client->indices()->updateAliases([
'body' => [
'actions' => [
['add' => ['index' => $aliasConfig->getAliasName() . '*', 'alias' => $aliasConfig->getHistoryAliasName()]],
['remove' => ['index' => '*', 'alias' => $aliasConfig->getWriteAliasName()]],
['add' => ['index' => $indexName, 'alias' => $aliasConfig->getWriteAliasName()]],
],
Expand Down Expand Up @@ -131,7 +132,7 @@ private function makeAliasActive(IndexAliasConfigurationInterface $aliasConfigur
$this->client->indices()->updateAliases([
'body' => [
'actions' => [
['add' => ['index' => $aliasConfiguration->getAliasName() . '*', 'alias' => $aliasConfiguration->getAliasName() . '-history']],
['add' => ['index' => $aliasConfiguration->getAliasName() . '*', 'alias' => $aliasConfiguration->getHistoryAliasName()]],
['remove' => ['index' => '*', 'alias' => $aliasConfiguration->getAliasName()]],
['add' => ['index' => $index, 'alias' => $aliasConfiguration->getAliasName()]],
],
Expand Down
12 changes: 4 additions & 8 deletions src/Infrastructure/Elastic/Finder.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,10 @@

class Finder
{
private Client $client;

private SearchCommandInterface $builder;

public function __construct(Client $client, SearchCommandInterface $builder)
{
$this->client = $client;
$this->builder = $builder;
public function __construct(
private Client $client,
private SearchCommandInterface $builder,
) {
}

public function find(): Results
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class ElasticIndexConfigurationRepository implements IndexConfigurationRepositor

private bool $pruneOldAliases;

public function __construct(array $indexConfigurations, $pruneOldAliases = true)
public function __construct(array $indexConfigurations, bool $pruneOldAliases = true)
{
$this->indexConfigurations = $indexConfigurations;
$this->pruneOldAliases = $pruneOldAliases;
Expand Down
8 changes: 8 additions & 0 deletions src/Infrastructure/Scout/ElasticEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,4 +217,12 @@ public function deleteIndex($name): void
$configuration = $this->indexConfigurationRepository->findForIndex($name);
$this->indexAdapter->delete($configuration);
}

public function deleteAllIndexes()
{
$configs = $this->indexConfigurationRepository->getConfigurations();
foreach ($configs as $config) {
$this->indexAdapter->delete($config);
}
}
}
33 changes: 24 additions & 9 deletions src/Infrastructure/Scout/ScoutSearchCommandBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Hilsonxhero\ElasticVision\Domain\Query\Query;
use Hilsonxhero\ElasticVision\Domain\Syntax\Sort;
use Hilsonxhero\ElasticVision\Domain\Syntax\Term;
use Hilsonxhero\ElasticVision\Domain\Syntax\Terms;
use Hilsonxhero\ElasticVision\Domain\Syntax\MultiMatch;
use Hilsonxhero\ElasticVision\Application\SearchableFields;
use Hilsonxhero\ElasticVision\Domain\Syntax\Compound\BoolQuery;
Expand All @@ -26,7 +27,9 @@ class ScoutSearchCommandBuilder implements SearchCommandInterface

private array $filter = [];

private array $where = [];
private array $wheres = [];

private array $whereIns = [];

private array $fields = [];

Expand Down Expand Up @@ -63,8 +66,9 @@ public static function wrap(Builder $builder): ScoutSearchCommandBuilder
$normalizedBuilder->setMust($builder->must ?? []);
$normalizedBuilder->setShould($builder->should ?? []);
$normalizedBuilder->setFilter($builder->filter ?? []);
$normalizedBuilder->setWhere($builder->where ?? []);
$normalizedBuilder->setQuery($builder->query ?? '');
$normalizedBuilder->setWheres($builder->wheres);
$normalizedBuilder->setWhereIns($builder->whereIns);
$normalizedBuilder->setQuery($builder->query ?: '');
$normalizedBuilder->setAggregations($builder->aggregations ?? []);
$normalizedBuilder->setSort(self::getSorts($builder));
$normalizedBuilder->setFields($builder->fields ?? []);
Expand Down Expand Up @@ -103,9 +107,13 @@ public function getFilter(): array
return $this->filter;
}

public function getWhere(): array
public function getWheres(): array
{
return $this->wheres;
}
public function getWhereIns(): array
{
return $this->where;
return $this->whereIns;
}

public function getQuery(): string
Expand Down Expand Up @@ -168,9 +176,13 @@ public function setFilter(array $filter): void
$this->filter = $filter;
}

public function setWhere(array $where): void
public function setWheres(array $wheres): void
{
$this->where = $where;
$this->wheres = $wheres;
}
public function setWhereIns(array $whereIns): void
{
$this->whereIns = $whereIns;
}

public function setQuery(string $query): void
Expand Down Expand Up @@ -264,10 +276,13 @@ public function buildQuery(): array

$compound->minimumShouldMatch($this->getMinimumShouldMatch());

foreach ($this->where as $field => $value) {
$compound->add('must', new Term($field, $value));
foreach ($this->wheres as $field => $value) {
$compound->add('filter', new Term($field, $value));
}

foreach ($this->whereIns as $field => $values) {
$compound->add('filter', new Terms($field, $values));
}
$query->setQuery($compound);

return $query->build();
Expand Down
Loading

0 comments on commit d8d347c

Please sign in to comment.