diff --git a/.gitignore b/.gitignore index b7fa9a3..c5e7dac 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ .phpunit.result.cache composer.lock infection.log +/.vscode diff --git a/docs/advanced-queries.md b/docs/advanced-queries.md index cab64da..c03d0d8 100644 --- a/docs/advanced-queries.md +++ b/docs/advanced-queries.md @@ -40,3 +40,137 @@ $results = Post::search('Self-steering') ->field('published_at') ->get(); ``` + +The debug json will be like this +```JSON +{ + "query": { + "bool": { + "must": [ + { + "multi_match": { + "query": "Self-steering", + "fuzziness": "auto" + } + } + ], + "should": [], + "filter": [] + } + }, + "fields": [ + "id", + "published_at" + ] +} +``` + +For example, to get all posts that: + +are published +have "lorem" somewhere in the document +have "ipsum" in the title +maybe have a tag "featured", if so boost its score by 2 +You could execute this search query: + +```php +$posts = Post::search('lorem') + ->must(new Matching('title', 'ipsum')) + ->should(new Terms('tags', ['featured'], 2)) + ->filter(new Term('published', true)) + ->get(); +``` + +```JSON +{ + "query": { + "bool": { + "must": [ + { + "match": { + "title": { + "query": "lorem", + "fuzziness": "auto" + } + } + }, + { + "multi_match": { + "query": "lorem", + "fuzziness": "auto" + } + } + ], + "should": [ + { + "terms": { + "tags": [ + "featured" + ], + "boost": 2 + } + } + ], + "filter": [ + { + "term": { + "published": { + "value": true, + "boost": 1 + } + } + } + ] + } + } +} +``` + +If you don not want to multi_match in that case you can set query empty string + +```php +$results = Post::search('') + ->must(new Matching('title', 'lorem')) + ->should(new Terms('tags', ['featured'], 2)) + ->filter(new Term('published', true)) + ->get(); +``` + +```JSON +{ + "query": { + "bool": { + "must": [ + { + "match": { + "title": { + "query": "lorem", + "fuzziness": "auto" + } + } + } + ], + "should": [ + { + "terms": { + "tags": [ + "featured" + ], + "boost": 2 + } + } + ], + "filter": [ + { + "term": { + "published": { + "value": true, + "boost": 1 + } + } + } + ] + } + } +} +``` diff --git a/docs/debugging.md b/docs/debugging.md index 0ab126c..89e8c0b 100644 --- a/docs/debugging.md +++ b/docs/debugging.md @@ -29,6 +29,22 @@ The debug class that this method returns can give you the last executed query as You should be able to copy-paste the json as a direct query to Elasticsearch. ```php -$lastQueryAsArray = ElasticEngine::debug()->array(); -$lastQueryAsJson = ElasticEngine::debug()->json(); -``` +class SearchController +{ + public function __invoke(SearchFormRequest $request) + { + $people = Cartographer::search($request->get('keywords'))->get(); + + // $lastQueryAsArray = ElasticEngine::debug()->array(); + // $lastQueryAsJson = ElasticEngine::debug()->json(); + + // return $lastQueryAsArray; + // or + // return lastQueryAsJson; + + return view('search', [ + 'people' => $people, + ]); + } +} +``` \ No newline at end of file diff --git a/docs/index-settings.md b/docs/index-settings.md index d7cfcbe..8efb9af 100644 --- a/docs/index-settings.md +++ b/docs/index-settings.md @@ -4,7 +4,7 @@ However, if for example you want to define more advanced Elasticsearch settings Be aware that any time you change the index settings, you need to [recreate](commands.md) the index. -To start using index settings, we will expand on the Post model with an `indexSettings` function to set an analyzer. +To start using index settings, we will expand on the Post model with an `indexSettings` and `Aliased` function to set an analyzer. ```php [ 'posts' => [ + 'aliased' => true, // this is required for custom analyzers 'settings' => [ 'analysis' => [ 'analyzer' => [ diff --git a/docs/text-analysis.md b/docs/text-analysis.md index db4df9a..4a5eb1f 100644 --- a/docs/text-analysis.md +++ b/docs/text-analysis.md @@ -11,6 +11,7 @@ namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; +use JeroenG\Explorer\Application\Aliased; use JeroenG\Explorer\Application\Explored; use JeroenG\Explorer\Application\IndexSettings; use JeroenG\Explorer\Domain\Analysis\Analysis; @@ -18,7 +19,7 @@ use JeroenG\Explorer\Domain\Analysis\Analyzer\StandardAnalyzer; use JeroenG\Explorer\Domain\Analysis\Filter\SynonymFilter; use Laravel\Scout\Searchable; -class Post extends Model implements Explored, IndexSettings +class Post extends Model implements Explored, IndexSettings, Aliased { use HasFactory; use Searchable; diff --git a/src/Domain/Syntax/MatchPhrasePrefix.php b/src/Domain/Syntax/MatchPhrasePrefix.php new file mode 100644 index 0000000..3a10ea0 --- /dev/null +++ b/src/Domain/Syntax/MatchPhrasePrefix.php @@ -0,0 +1,25 @@ +field = $field; + $this->value = $value; + } + + public function build(): array + { + $query = [ 'query' => $this->value ]; + + return ['match_phrase_prefix' => [ $this->field => $query ] ]; + } +}