Skip to content
Clémentine Urquizar - curqui edited this page Jan 27, 2022 · 20 revisions

Usage

search() is a method of the Indexes class.

public function search($query, array $searchParams = [], array $options = [])

Parameters:

$query: the query words.

$searchParams (optional): the search parameters handled by Meilisearch.

$options (optional): options you can pass during the search. There are described in the options section.

Return:

The search() method returns a SearchResult instance.

💡 If you don't want the search method to return a class instance, use the rawSearch() method or set raw to true in the $options parameter. No instance class will be created during the process which might improve the performances in some situations.

👍 All the accessible methods from this instance are described in this section.

Search Parameters

All the supported parameters are described in the search parameters section of the documentation.

You will be able to filter, use facets, restrict the fields to retrieve...

Filters Examples

With filters, both single and double quotes are supported.

// Enclosing with double quotes
$index->search('prince', $searchParams = ['filters' => "title = 'Le Petit Prince' OR author = 'J. R. R. Tolkien'"]);

// Enclosing with single quotes
$index->search('hobbit', $searchParams = ['filters' => 'title = "The Hitchhiker\'s Guide to the Galaxy" OR author = "J. R. R. Tolkien"']);

Options

Here are the different options you can pass in the $options parameter.

removeZeroFacets

Default: false

Remove the facetsDistribution values that are equal to zero returned by Meilisearch.

More about the Faceted Search.

$index->search(
    'prince',
    $searchParams = ['facetsDistribution' => ['genre']],
    $options = [removeZeroFacets => true]
);

// With removeZeroFacets = false
// "facetsDistribution": {
//   "genre": {
//     "horror: 443,
//     "thriller": 200,
//     "comedy": 0
//   }
// }

// With removeZeroFacets = true
// "facetsDistribution": {
//   "genre": {
//     "horror: 443,
//     "thriller": 200,
//   }
// }

transformFacetsDistribution

The callback that will be applied to the facetsDistribution array returned by Meilisearch.

$facetsToUpperFunc = function (array $facets) {
    $changeOneFacet = function (array $facet) {
        $result = [];
        foreach ($facet as $k => $v) {
            $result[strtoupper($k)] = $v;
        }
        return $result;
    };
    return array_map($changeOneFacet, $facets);
};

$response = $this->index->search(
    'prince',
    ['facetsDistribution' => ['genre']],
    ['transformFacetsDistribution' => $facetsToUpperFunc]
);

transformHits

The callback that will be applied to the hits array returned by Meilisearch.

// Before PHP 7.4
function titlesToUpperCase(array $hits) {
    return array_map($hits, function (array $hit) {
        $hit['title'] = strtoupper($hit['title']);
            return $hit;
    });
}

// PHP 7.4 and later
function titlesToUpperCase(array $hits) {
    return array_map($hits, function (array $hit) {
        return [
            ...$hit,
            title: strtoupper($hit['title']
        ]
    });
}

$index->search('prince', $searchParams = [], $options = ['transformHits' => 'titlesToUpperCase'])

raw

Default: false

The search() will return the raw information returned by the Meilisearch server in an Array. No class instance will be created.

$index->search('prince', $searchParams = [], $options = [raw => true]);

⚠️ Because no class instance is created if you use this option, it means the other options passed as parameters will not be applied.
If you only want to get an Array with your transformed hits and facetsDistribution, please use toArray instead:

$index->search(
    'prince',
    $searchParams = ['facetsDistribution' => ['genre']],
    $options = [removeZeroFacets => true]
)->toArray();

More Option Examples

  • Oder your facetsDistribution by alphanumerical order:
$facetsToUpperFunc = function (array $facets) {
    $sortOneFacet = function (array $facet) {
        ksort($facet);
        return $facet;
    };
    return array_map($sortOneFacet, $facets);
};

$response = $this->index->search(
    'prince,
    ['facetsDistribution' => ['genre']],
    ['transformFacetsDistribution' => $facetsToUpperFunc]
);
  • To remove some specifics hits in the results:
$removeLePetitPrince = function (array $hits) {
    return array_filter(
        $hits,
        function (array $hit) { 'Le Petit Prince' !== $hit['title']; }
    );
};

$response = $index->search('prince', $searchParams = [], $options = ['transformHits' => $removeLePetitPrince]);

Accessible methods from the SearchResult instance

  • All the getter to get the Meilisearch fields returned by Meilisearch
$searchResult = $index->search('prince', ['facetsDistribution' => ['genre']]);
$hits = $searchResult->getHits();
$facetsDistribution = $searchResult->getFacetsDistribution();
$offset = $searchResult->getOffset();
  • getHitsCount() or count(): the length of the hits field.
$hitsCount = $searchResult->getHitsCount();
$hitsCount = $searchResult->count();
  • getRaw(): the raw response returned by Meilisearch before applying any change.
$response = $searchResult->getRaw();
  • toJson(): convert to JSON.
$jsonResponse $searchResult->toJson();
  • toArray(): return the information of the class instance as an Array.
$searchResultArray = $searchResult->toArray();
  • The $options can also be used as method: removeZeroFacets(), transformFacetsDistribution(), and transformHits().
$searchResult->removeZeroFacets();
$searchResult->transformFacetsDistribution($callable);
$searchResult->transformHits($callable);