Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update the docs and add match phrase prefix class #113

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
.phpunit.result.cache
composer.lock
infection.log
/.vscode
134 changes: 134 additions & 0 deletions docs/advanced-queries.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
]
}
}
}
```
22 changes: 19 additions & 3 deletions docs/debugging.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I rather have the shorter examples I had.

{
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,
]);
}
}
```
6 changes: 4 additions & 2 deletions docs/index-settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
<?php
Expand All @@ -13,10 +13,11 @@ 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 Laravel\Scout\Searchable;

class Post extends Model implements Explored, IndexSettings
class Post extends Model implements Explored, IndexSettings, Aliased
{
use HasFactory;
use Searchable;
Expand Down Expand Up @@ -58,6 +59,7 @@ If you want to use the configuration array notation (see [mapping](mapping.md)),
return [
'indexes' => [
'posts' => [
'aliased' => true, // this is required for custom analyzers
'settings' => [
'analysis' => [
'analyzer' => [
Expand Down
3 changes: 2 additions & 1 deletion docs/text-analysis.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ 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;
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;
Expand Down
25 changes: 25 additions & 0 deletions src/Domain/Syntax/MatchPhrasePrefix.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace JeroenG\Explorer\Domain\Syntax;

class MatchPhrasePrefix implements SyntaxInterface
{
private string $field;

private mixed $value;

public function __construct(string $field, $value = null)
{
$this->field = $field;
$this->value = $value;
}

public function build(): array
{
$query = [ 'query' => $this->value ];

return ['match_phrase_prefix' => [ $this->field => $query ] ];
}
}