Skip to content

Commit

Permalink
Merge #475
Browse files Browse the repository at this point in the history
475: Changes related to the next Meilisearch release (v1.1.0) r=curquiza a=meili-bot

Related to this issue: meilisearch/integration-guides#251

This PR:
- gathers the changes related to the next Meilisearch release (v1.1.0) so that this package is ready when the official release is out.
- should pass the tests against the [latest pre-release of Meilisearch](https://github.com/meilisearch/meilisearch/releases).
- might eventually contain test failures until the Meilisearch v1.1.0 is out.

⚠️ This PR should NOT be merged until the next release of Meilisearch (v1.1.0) is out.

_This PR is auto-generated for the [pre-release week](https://github.com/meilisearch/integration-guides/blob/main/resources/pre-release-week.md) purpose._


- [x] `csvDelimiter`
- [x] `multiSearch`
- [ ] release changelog
- [x] facetStats

Co-authored-by: meili-bot <74670311+meili-bot@users.noreply.github.com>
Co-authored-by: Bruno Casali <brunoocasali@gmail.com>
Co-authored-by: cvermand <33010418+bidoubiwa@users.noreply.github.com>
  • Loading branch information
4 people committed Apr 3, 2023
2 parents 75b4dfa + 04c548f commit 950d587
Show file tree
Hide file tree
Showing 10 changed files with 435 additions and 12 deletions.
2 changes: 2 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Meilisearch\Endpoints\Delegates\HandlesDumps;
use Meilisearch\Endpoints\Delegates\HandlesIndex;
use Meilisearch\Endpoints\Delegates\HandlesKeys;
use Meilisearch\Endpoints\Delegates\HandlesMultiSearch;
use Meilisearch\Endpoints\Delegates\HandlesSystem;
use Meilisearch\Endpoints\Delegates\HandlesTasks;
use Meilisearch\Endpoints\Dumps;
Expand All @@ -30,6 +31,7 @@ class Client
use HandlesTasks;
use HandlesKeys;
use HandlesSystem;
use HandlesMultiSearch;

private HttpContract $http;
private Indexes $index;
Expand Down
178 changes: 178 additions & 0 deletions src/Contracts/SearchQuery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
<?php

declare(strict_types=1);

namespace Meilisearch\Contracts;

class SearchQuery
{
private string $indexUid;

private string $q;
private array $filter;
private array $attributesToRetrieve;
private array $attributesToCrop;
private ?int $cropLength;
private array $attributesToHighlight;
private string $cropMarker;
private string $highlightPreTag;
private string $highlightPostTag;
private array $facets;
private ?bool $showMatchesPosition;
private array $sort;
private string $matchingStrategy;
private ?int $offset;
private ?int $limit;
private ?int $hitsPerPage;
private ?int $page;

public function setQuery(string $q): SearchQuery
{
$this->q = $q;

return $this;
}

public function setFilter(array $filter): SearchQuery
{
$this->filter = $filter;

return $this;
}

public function setAttributesToRetrieve(array $attributesToRetrieve): SearchQuery
{
$this->attributesToRetrieve = $attributesToRetrieve;

return $this;
}

public function setAttributesToCrop(array $attributesToCrop): SearchQuery
{
$this->attributesToCrop = $attributesToCrop;

return $this;
}

public function setCropLength(?int $cropLength): SearchQuery
{
$this->cropLength = $cropLength;

return $this;
}

public function setAttributesToHighlight(array $attributesToHighlight): SearchQuery
{
$this->attributesToHighlight = $attributesToHighlight;

return $this;
}

public function setCropMarker(string $cropMarker): SearchQuery
{
$this->cropMarker = $cropMarker;

return $this;
}

public function setHighlightPreTag(string $highlightPreTag): SearchQuery
{
$this->highlightPreTag = $highlightPreTag;

return $this;
}

public function setHighlightPostTag(string $highlightPostTag): SearchQuery
{
$this->highlightPostTag = $highlightPostTag;

return $this;
}

public function setFacets(array $facets): SearchQuery
{
$this->facets = $facets;

return $this;
}

public function setShowMatchesPosition(?bool $showMatchesPosition): SearchQuery
{
$this->showMatchesPosition = $showMatchesPosition;

return $this;
}

public function setSort(array $sort): SearchQuery
{
$this->sort = $sort;

return $this;
}

public function setMatchingStrategy(string $matchingStrategy): SearchQuery
{
$this->matchingStrategy = $matchingStrategy;

return $this;
}

public function setOffset(?int $offset): SearchQuery
{
$this->offset = $offset;

return $this;
}

public function setLimit(?int $limit): SearchQuery
{
$this->limit = $limit;

return $this;
}

public function setHitsPerPage(?int $hitsPerPage): SearchQuery
{
$this->hitsPerPage = $hitsPerPage;

return $this;
}

public function setPage(?int $page): SearchQuery
{
$this->page = $page;

return $this;
}

public function setIndexUid(string $uid): SearchQuery
{
$this->indexUid = $uid;

return $this;
}

public function toArray(): array
{
return array_filter([
'indexUid' => $this->indexUid ?? null,
'q' => $this->q ?? null,
'filter' => $this->filter ?? null,
'attributesToRetrieve' => $this->attributesToRetrieve ?? null,
'attributesToCrop' => $this->attributesToCrop ?? null,
'cropLength' => $this->cropLength ?? null,
'attributesToHighlight' => $this->attributesToHighlight ?? null,
'cropMarker' => $this->cropMarker ?? null,
'highlightPreTag' => $this->highlightPreTag ?? null,
'highlightPostTag' => $this->highlightPostTag ?? null,
'facets' => $this->facets ?? null,
'showMatchesPosition' => $this->showMatchesPosition ?? null,
'sort' => $this->sort ?? null,
'matchingStrategy' => $this->matchingStrategy ?? null,
'offset' => $this->offset ?? null,
'limit' => $this->limit ?? null,
'hitsPerPage' => $this->hitsPerPage ?? null,
'page' => $this->page ?? null,
], function ($item) { return null !== $item; });
}
}
17 changes: 9 additions & 8 deletions src/Endpoints/Delegates/HandlesDocuments.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ public function addDocumentsJson(string $documents, ?string $primaryKey = null)
return $this->http->post(self::PATH.'/'.$this->uid.'/documents', $documents, ['primaryKey' => $primaryKey], 'application/json');
}

public function addDocumentsCsv(string $documents, ?string $primaryKey = null)
public function addDocumentsCsv(string $documents, ?string $primaryKey = null, ?string $delimiter = null)
{
return $this->http->post(self::PATH.'/'.$this->uid.'/documents', $documents, ['primaryKey' => $primaryKey], 'text/csv');
return $this->http->post(self::PATH.'/'.$this->uid.'/documents', $documents, ['primaryKey' => $primaryKey, 'csvDelimiter' => $delimiter], 'text/csv');
}

public function addDocumentsNdjson(string $documents, ?string $primaryKey = null)
Expand All @@ -56,11 +56,12 @@ public function addDocumentsInBatches(array $documents, ?int $batchSize = 1000,
return $promises;
}

public function addDocumentsCsvInBatches(string $documents, ?int $batchSize = 1000, ?string $primaryKey = null)
public function addDocumentsCsvInBatches(string $documents, ?int $batchSize = 1000, ?string $primaryKey = null, ?string $delimiter = null)
{
$promises = [];

foreach (self::batchCsvString($documents, $batchSize) as $batch) {
$promises[] = $this->addDocumentsCsv($batch, $primaryKey);
$promises[] = $this->addDocumentsCsv($batch, $primaryKey, $delimiter);
}

return $promises;
Expand All @@ -86,9 +87,9 @@ public function updateDocumentsJson(string $documents, ?string $primaryKey = nul
return $this->http->put(self::PATH.'/'.$this->uid.'/documents', $documents, ['primaryKey' => $primaryKey], 'application/json');
}

public function updateDocumentsCsv(string $documents, ?string $primaryKey = null)
public function updateDocumentsCsv(string $documents, ?string $primaryKey = null, ?string $delimiter = null)
{
return $this->http->put(self::PATH.'/'.$this->uid.'/documents', $documents, ['primaryKey' => $primaryKey], 'text/csv');
return $this->http->put(self::PATH.'/'.$this->uid.'/documents', $documents, ['primaryKey' => $primaryKey, 'csvDelimiter' => $delimiter], 'text/csv');
}

public function updateDocumentsNdjson(string $documents, ?string $primaryKey = null)
Expand All @@ -106,11 +107,11 @@ public function updateDocumentsInBatches(array $documents, ?int $batchSize = 100
return $promises;
}

public function updateDocumentsCsvInBatches(string $documents, ?int $batchSize = 1000, ?string $primaryKey = null)
public function updateDocumentsCsvInBatches(string $documents, ?int $batchSize = 1000, ?string $primaryKey = null, ?string $delimiter = null)
{
$promises = [];
foreach (self::batchCsvString($documents, $batchSize) as $batch) {
$promises[] = $this->updateDocumentsCsv($batch, $primaryKey);
$promises[] = $this->updateDocumentsCsv($batch, $primaryKey, $delimiter);
}

return $promises;
Expand Down
22 changes: 22 additions & 0 deletions src/Endpoints/Delegates/HandlesMultiSearch.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Meilisearch\Endpoints\Delegates;

trait HandlesMultiSearch
{
/**
* @param list<\Meilisearch\Contracts\SearchQuery> $queries
*/
public function multiSearch(array $queries = [])
{
$body = [];

foreach ($queries as $query) {
$body[] = $query->toArray();
}

return $this->http->post('/multi-search', ['queries' => $body]);
}
}
14 changes: 14 additions & 0 deletions src/Search/SearchResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ class SearchResult implements \Countable, \IteratorAggregate
*/
private array $facetDistribution;

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

/**
* @var array<string, mixed>
*/
Expand Down Expand Up @@ -64,6 +69,7 @@ public function __construct(array $body)
$this->processingTimeMs = $body['processingTimeMs'];
$this->query = $body['query'];
$this->facetDistribution = $body['facetDistribution'] ?? [];
$this->facetStats = $body['facetStats'] ?? [];
$this->raw = $body;
}

Expand Down Expand Up @@ -179,6 +185,14 @@ public function getFacetDistribution(): array
return $this->facetDistribution;
}

/**
* @return array<string, mixed>
*/
public function getFacetStats(): array
{
return $this->facetStats;
}

/**
* Return the original search result.
*
Expand Down
Loading

0 comments on commit 950d587

Please sign in to comment.