Skip to content

Commit ef9387b

Browse files
feat: Add sortableAttributes property to index settings
1 parent 53e7f84 commit ef9387b

File tree

4 files changed

+45
-46
lines changed

4 files changed

+45
-46
lines changed

.github/workflows/create-release.yml

-24
This file was deleted.

composer.json

+5-8
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"rss": "https://github.com/fostercommerce/meilisearch-connect/releases.atom"
1212
},
1313
"require": {
14-
"php": ">=8.1",
14+
"php": "^8.1",
1515
"craftcms/cms": "^4.6.0|^5.0.0",
1616
"meilisearch/meilisearch-php": "^1.6.0"
1717
},
@@ -36,18 +36,15 @@
3636
},
3737
"scripts": {
3838
"phpstan": "phpstan --memory-limit=1G",
39-
"ecs-check": "ecs check --ansi --memory-limit=1G",
40-
"ecs-fix": "ecs check --ansi --fix --memory-limit=1G",
41-
"rector": "rector process --config rector.php",
42-
"rector-dry-run": "rector process --dry-run --config rector.php"
39+
"ecs:check": "ecs check --ansi --memory-limit=1G",
40+
"ecs:fix": "ecs check --ansi --fix --memory-limit=1G",
41+
"rector:fix": "rector process --config rector.php",
42+
"rector:dry-run": "rector process --dry-run --config rector.php"
4343
},
4444
"minimum-stability": "dev",
4545
"prefer-stable": true,
4646
"config": {
4747
"sort-packages": true,
48-
"platform": {
49-
"php": "8.1"
50-
},
5148
"allow-plugins": {
5249
"yiisoft/yii2-composer": true,
5350
"craftcms/plugin-installer": true,

src/models/IndexSettings.php

+29-3
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,50 @@
66

77
class IndexSettings extends Model
88
{
9-
public ?string $primaryKey = null;
9+
public const DEFAULT_PRIMARY_KEY = 'id';
1010

1111
/**
12+
* Primary key of the index. If not specified, Meilisearch guesses your primary key from the first document you add to the index.
13+
*
14+
* This plugin defaults the primary key to `id` which matches most Craft elements.
15+
*/
16+
public ?string $primaryKey = self::DEFAULT_PRIMARY_KEY;
17+
18+
/**
19+
* Ranking rules are built-in rules that rank search results according to certain criteria. They are applied in the same order in which they appear in the `rankingRules` array.
20+
*
1221
* @var string[]
1322
*/
1423
public array $ranking = [];
1524

1625
/**
17-
* @var string[]
26+
* The values associated with attributes in the `searchableAttributes` list are searched for matching query words. The order of the list also determines the attribute ranking order.
27+
*
28+
* By default, the `searchableAttributes` array is equal to all fields in your dataset. This behavior is represented by the value `["*"]`.
29+
*
30+
* @var ?string[]
1831
*/
19-
public array $searchableAttributes = [];
32+
public ?array $searchableAttributes = null;
2033

2134
/**
35+
* Attributes in the `filterableAttributes` list can be used as filters or facets.
36+
*
2237
* @var string[]
2338
*/
2439
public array $filterableAttributes = [];
2540

2641
/**
42+
* Attributes that can be used when sorting search results using the sort search parameter.
43+
*
44+
* @var string[]
45+
*/
46+
public array $sortableAttributes = [];
47+
48+
/**
49+
* With Meilisearch, you can create faceted search interfaces. This setting allows you to:
50+
* - Define the maximum number of values returned by the facets search parameter
51+
* - Sort facet values by value count or alphanumeric order
52+
*
2753
* @var array{maxValuesPerFacet?: int, sortFacetValuesBy?: array<non-empty-string, 'count'|'alpha'>}
2854
*/
2955
public array $faceting = [];

src/services/Sync.php

+11-11
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,13 @@
55
use fostercommerce\meilisearch\models\Index;
66
use fostercommerce\meilisearch\models\Settings;
77
use fostercommerce\meilisearch\Plugin;
8+
use Meilisearch\Exceptions\TimeOutException;
89
use yii\base\Component;
910

1011
class Sync extends Component
1112
{
1213
use Meili;
1314

14-
public const DEFAULT_PRIMARY_KEY = 'id';
15-
1615
private ?Settings $_settings = null;
1716

1817
public function init(): void
@@ -23,6 +22,9 @@ public function init(): void
2322
$this->_settings = Plugin::getInstance()->settings;
2423
}
2524

25+
/**
26+
* @throws TimeOutException
27+
*/
2628
public function syncSettings(?string $indexName = null): void
2729
{
2830
foreach ($this->getIndices($indexName) as $indexHandle => $indexConfig) {
@@ -33,8 +35,12 @@ public function syncSettings(?string $indexName = null): void
3335
$indexSettings = $indexConfig->settings;
3436

3537
$index->updateRankingRules($indexSettings->ranking);
36-
$index->updateSearchableAttributes($indexSettings->searchableAttributes);
38+
if ($indexSettings->searchableAttributes !== null) {
39+
$index->updateSearchableAttributes($indexSettings->searchableAttributes);
40+
}
41+
3742
$index->updateFilterableAttributes($indexSettings->filterableAttributes);
43+
$index->updateSortableAttributes($indexSettings->sortableAttributes);
3844
$index->updateFaceting($indexSettings->faceting);
3945
}
4046
}
@@ -44,10 +50,7 @@ public function syncIndices(?string $indexName = null, ?string $identifier = nul
4450
foreach ($this->getIndices($indexName) as $indexHandle => $indexConfig) {
4551
$index = $this->meiliClient->index($indexHandle);
4652
$fetch = $indexConfig->fetch;
47-
$index->addDocuments(
48-
$fetch($identifier),
49-
$indexConfig->settings->primaryKey ?? self::DEFAULT_PRIMARY_KEY
50-
);
53+
$index->addDocuments($fetch($identifier), $indexConfig->settings->primaryKey);
5154
}
5255
}
5356

@@ -57,10 +60,7 @@ public function refreshIndices(?string $indexName = null): void
5760
$index = $this->meiliClient->index($indexHandle);
5861
$index->deleteAllDocuments();
5962
$fetch = $indexConfig->fetch;
60-
$index->addDocuments(
61-
$fetch(null),
62-
$indexConfig->settings->primaryKey ?? self::DEFAULT_PRIMARY_KEY
63-
);
63+
$index->addDocuments($fetch(null), $indexConfig->settings->primaryKey);
6464
}
6565
}
6666

0 commit comments

Comments
 (0)