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

Implemented the composite aggregation feature #205

Merged
merged 4 commits into from
Apr 10, 2024
Merged
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
18 changes: 16 additions & 2 deletions docs/searchclass.md
Original file line number Diff line number Diff line change
Expand Up @@ -252,14 +252,28 @@ The `facet()` method allows you to add a facet (aggregation) to your search quer
$search->facet($field, $group = null, $limit = null, $sortField = null, $sortDirection = 'desc');
```
Parameters:
* attribute name - This is a required parameter and can also be an expression name.
* facet alias - If not provided, the attribute name will be utilized.
* field - name of the attribute to group by. This is a required parameter and can also be an expression name.
* group - Facet name. If not provided, the attribute name will be utilized.
* limit - Defines the maximum number of facet values to return.
* sortField - Field the facet values will be sorted by. Also, can be set as `COUNT(*)` or `FACET()`. For details, see [Ordering in facet result](https://manual.manticoresearch.com/Searching/Faceted_search#Ordering-in-facet-result).
* sortDirection - Direction of sorting, `desc` by default

Facets will be included in the result set and can be accessed using [ResultSet:getFacets()](searchresults.md#resultset-object).

### multiFacet()

The `multiFacet()` method allows you to add a facet (aggregation) composed by multiple fields to your search query.

```php
$search->multiFacet($group = null, $limit = null);
```
Parameters:
* group - Facet name. This is a required parameter.
* limit - Defines the maximum number of facet values to return.

Multi field facets will be included in the result set and can be accessed using [ResultSet:getFacets()](searchresults.md#resultset-object).


### option()

Pass options to the search query.
Expand Down
52 changes: 44 additions & 8 deletions src/Manticoresearch/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,22 +258,57 @@ public function maxMatches($maxmatches): self {
return $this;
}

public function facet($field, $group = null, $limit = null, $sortField = null, $sortDirection = 'desc') : self {
// reset facets
if ($field === false) {
$this->params['aggs'] = [];
}
public function facet(
$field,
$group = null,
$limit = null,
$sortField = null,
$sortDirection = 'desc',
$multiGroup = null
): self {
if ($group === null) {
$group = $field;
}
$terms = ['field' => $field];
$terms = ['field' => $field];
if ($limit !== null) {
$terms['size'] = $limit;
}
$this->params['aggs'][$group] = ['terms' => $terms];
$facet = ['terms' => $terms];
if ($sortField !== null) {
$this->params['aggs'][$group]['sort'] = [ [$sortField => $sortDirection] ];
$facet['sort'] = [ [$sortField => $sortDirection] ];
}
if ($multiGroup !== null && isset($this->params['aggs'], $this->params['aggs'][$multiGroup])) {
// reset facets
if ($field === false) {
$this->params['aggs'][$multiGroup]['sources'] = [];
}
$this->params['aggs'][$multiGroup]['composite']['sources'][] = [ $group => $facet ];
} else {
// reset facets
if ($field === false) {
$this->params['aggs'] = [];
}
$this->params['aggs'][$group] = $facet;
}

return $this;
}

public function multiFacet($group, $limit = null): self {
// reset multi facets
if ($group === false) {
$this->params['aggs'] = array_filter(
$this->params['aggs'],
function ($v) {
return isset($v['composite']);
}
);
}
$this->params['aggs'][$group] = [ 'composite' => [ 'sources' => [] ] ];
if ($limit !== null) {
$this->params['aggs'][$group]['composite']['size'] = $limit;
}

return $this;
}

Expand Down Expand Up @@ -332,6 +367,7 @@ public function profile(): self {
*/
public function get() {
$this->body = $this->compile();
print_r($this->body);
$resp = $this->client->search(['body' => $this->body], true);
return new ResultSet($resp);
}
Expand Down
11 changes: 11 additions & 0 deletions test/Manticoresearch/SearchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -849,6 +849,17 @@ public function testFacets() {
$this->assertCount(3, $facets['_year']['buckets']);
}

public function multiFacets() {
$results = static::$search->filter('_year', 'range', [1960,1992])->multiFacet('multi')
->facet('_year', null, null, null, 'desc', 'multi')
->facet('rating', null, null, null, 'desc', 'multi')
->get();
$facets = $results->getFacets();
$this->assertCount(1, $facets);
$this->assertArrayHasKey('_year', $facets);
$this->assertCount(3, $facets['_year']['buckets']);
}

public function testKnnSearchByDocId() {
$results = static::$search->knn('kind', 3, 5)->get();
$resultIds = [4,5,2,6,10];
Expand Down