Skip to content
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
4 changes: 2 additions & 2 deletions .code-samples.meilisearch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ search_parameter_guide_highlight_tag_1: |-
'highlightPostTag' => '</span>'
]);
search_parameter_guide_matches_1: |-
$client->index('movies')->search('winter feast', ['attributesToHighlight' => ['overview'], 'matches' => true]);
$client->index('movies')->search('winter feast', ['attributesToHighlight' => ['overview'], 'showMatchesPosition' => true]);
settings_guide_synonyms_1: |-
$client->index('tops')->updateSettings([
'synonyms' => [
Expand Down Expand Up @@ -468,7 +468,7 @@ faceted_search_update_settings_1: |-
faceted_search_filter_1: |-
$client->index('movies')->search('thriller', ['filter' => [['genres = Horror', 'genres = Mystery'], 'director = "Jordan Peele"']]);
faceted_search_facets_distribution_1: |-
$client->index('movies')->search('Batman', ['facetsDistribution' => ['genres']]);
$client->index('movies')->search('Batman', ['facets' => ['genres']]);
faceted_search_walkthrough_filter_1: |-
$client->index('movies')->search('thriller', ['filter' => [['genres = Horror', 'genres = Mystery'], 'director = "Jordan Peele"']]);
post_dump_1: |-
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ $index->search(
],
"offset": 0,
"limit": 20,
"nbHits": 1,
Copy link
Member

Choose a reason for hiding this comment

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

Just to remind myself, but we need to deal with the nbHits in the meilisearch-php to prevent the breaking in the laravel/scout!

Copy link
Member Author

Choose a reason for hiding this comment

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

I completely forgot to change this despite I discussed this with you 😄
You can indeed take my branch!

"estimatedTotalHits": 1,
"processingTimeMs": 0,
"query": "wonder"
}
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ services:
- ./:/home/package

meilisearch:
image: getmeili/meilisearch:latest
image: getmeili/meilisearch:v0.28.0rc0
ports:
- "7700"
environment:
Expand Down
7 changes: 6 additions & 1 deletion src/Endpoints/Indexes.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,12 @@ public function rawSearch(?string $query, array $searchParams = []): array
$searchParams
);

return $this->http->post(self::PATH.'/'.$this->uid.'/search', $parameters);
$result = $this->http->post(self::PATH.'/'.$this->uid.'/search', $parameters);

// patch to prevent breaking in laravel/scout getTotalCount method.
$result['nbHits'] = $result['estimativeNbHits'];

return $result;
}

// Stats
Expand Down
48 changes: 16 additions & 32 deletions src/Search/SearchResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,23 @@ class SearchResult implements Countable, IteratorAggregate
private array $hits;

/**
* `nbHits` is the attributes returned by the Meilisearch server
* `estimatedTotalHits` is the attributes returned by the Meilisearch server
* and its value will not be modified by the methods in this class.
* Please, use `hitsCount` if you want to know the real size of the `hits` array at any time.
*/
private int $nbHits;
private int $estimatedTotalHits;

private int $hitsCount;
private bool $exhaustiveNbHits;
private int $offset;
private int $limit;
private int $processingTimeMs;

private string $query;
private ?bool $exhaustiveFacetsCount;

/**
* @var array<string, mixed>
*/
private array $facetsDistribution;
private array $facetDistribution;

/**
* @var array<string, mixed>
Expand All @@ -46,21 +44,19 @@ public function __construct(array $body)
$this->hits = $body['hits'] ?? [];
$this->offset = $body['offset'];
$this->limit = $body['limit'];
$this->nbHits = $body['nbHits'];
$this->estimatedTotalHits = $body['estimatedTotalHits'];
$this->hitsCount = \count($body['hits']);
$this->exhaustiveNbHits = $body['exhaustiveNbHits'] ?? false;
$this->processingTimeMs = $body['processingTimeMs'];
$this->query = $body['query'];
$this->exhaustiveFacetsCount = $body['exhaustiveFacetsCount'] ?? null;
$this->facetsDistribution = $body['facetsDistribution'] ?? [];
$this->facetDistribution = $body['facetDistribution'] ?? [];
$this->raw = $body;
}

/**
* Return a new {@see SearchResult} instance.
*
* The $options parameter is an array, and the following keys are accepted:
* - transformFacetsDistribution (callable)
* - transformFacetDistribution (callable)
* - transformHits (callable)
*
* The method does NOT trigger a new search.
Expand All @@ -72,8 +68,8 @@ public function applyOptions($options): self
if (\array_key_exists('transformHits', $options) && \is_callable($options['transformHits'])) {
$this->transformHits($options['transformHits']);
}
if (\array_key_exists('transformFacetsDistribution', $options) && \is_callable($options['transformFacetsDistribution'])) {
$this->transformFacetsDistribution($options['transformFacetsDistribution']);
if (\array_key_exists('transformFacetDistribution', $options) && \is_callable($options['transformFacetDistribution'])) {
$this->transformFacetDistribution($options['transformFacetDistribution']);
}

return $this;
Expand All @@ -87,9 +83,9 @@ public function transformHits(callable $callback): self
return $this;
}

public function transformFacetsDistribution(callable $callback): self
public function transformFacetDistribution(callable $callback): self
{
$this->facetsDistribution = $callback($this->facetsDistribution);
$this->facetDistribution = $callback($this->facetDistribution);

return $this;
}
Expand Down Expand Up @@ -127,14 +123,9 @@ public function count(): int
return $this->hitsCount;
}

public function getNbHits(): int
public function getEstimatedTotalHits(): int
{
return $this->nbHits;
}

public function getExhaustiveNbHits(): bool
{
return $this->exhaustiveNbHits;
return $this->estimatedTotalHits;
}

public function getProcessingTimeMs(): int
Expand All @@ -147,17 +138,12 @@ public function getQuery(): string
return $this->query;
}

public function getExhaustiveFacetsCount(): ?bool
{
return $this->exhaustiveFacetsCount;
}

/**
* @return array<string, mixed>
*/
public function getFacetsDistribution(): array
public function getFacetDistribution(): array
{
return $this->facetsDistribution;
return $this->facetDistribution;
}

/**
Expand All @@ -176,13 +162,11 @@ public function toArray(): array
'hits' => $this->hits,
'offset' => $this->offset,
'limit' => $this->limit,
'nbHits' => $this->nbHits,
'estimatedTotalHits' => $this->estimatedTotalHits,
'hitsCount' => $this->hitsCount,
'exhaustiveNbHits' => $this->exhaustiveNbHits,
'processingTimeMs' => $this->processingTimeMs,
'query' => $this->query,
'exhaustiveFacetsCount' => $this->exhaustiveFacetsCount,
'facetsDistribution' => $this->facetsDistribution,
'facetDistribution' => $this->facetDistribution,
];
}

Expand Down
Loading