Search

MeiliSearch exposes 2 routes to perform searches:

  • A POST route: this is the preferred route when using API authentication, as it allows preflight requestSearch - 图1 (opens new window) caching and better performances.
  • A GET route: the usage of this route is discouraged, unless you have good reason to do otherwise (specific caching abilities for example).

Other than the differences mentioned above, the two routes are strictly equivalent.

Search in an index with POST route

POST

  1. /indexes/:index_uid/search

Search for documents matching a specific query in the given index.

This is the preferred route to perform search when an API key is required, as it allows for preflight requestsSearch - 图2 (opens new window) to be cached. Caching preflight requests considerably improves search speed.

Path variables

VariableDescription
index_uidThe index UID

Body

VariableTypeDescriptionDefault value
qStringQuery string _(mandatory)“”
offsetIntegerNumber of documents to skip0
limitIntegerMaximum number of documents returned20
filterString OR [Strings OR [Strings]]Filter queries by an attribute valuenull
facetsDistribution[Strings]Facets for which to retrieve the matching countnull
attributesToRetrieve[Strings]Attributes to display in the returned documents[“*”]
attributesToCrop[Strings]Attributes whose values have to be croppednull
cropLengthIntegerLength used to crop field values200
attributesToHighlight[Strings]Attributes whose values will contain highlighted matching termsnull
matchesBooleanDefines whether an object that contains information about the matches should be returned or notfalse
sort[Strings]Sort search results according to the attributes and sorting order (asc or desc) specifiednull

filter accepts a query string. You can find more about the filter syntax on our dedicated page.
cropLength is automatically rounded to match word boundaries.
sort requires attributes to be given as attribute:sorting_order. You can find more about the syntax on our dedicate page.

Learn more about how to use the search parameters.

Placeholder search is a search with an empty q parameter. Since there is no query term, the built-in ranking rulesSearch - 图3 (opens new window) do not apply. Only sort and custom ranking rules are taken into account.

If the index has no sort or custom ranking rules, the results are returned in the order of their internal database position.

Query terms enclosed in double quotes are treated as phrase searches.

Response

fieldDescriptiontype
hitsResults of the query[result]
offsetNumber of documents skippednumber
limitNumber of documents to takenumber
nbHitsTotal number of matchesnumber
exhaustiveNbHitsWhether nbHits is exhaustiveboolean
facetsDistributionDistribution of the given facetsobject
exhaustiveFacetsCountWhether facetsDistribution is exhaustiveboolean
processingTimeMsProcessing time of the querynumber
queryQuery originating the responsestring

Example

<>

cURL

JS

Python

PHP

Java

Ruby

Go

Rust

Swift

Dart

  1. curl \
  2. -X POST 'http://localhost:7700/indexes/movies/search' \
  3. -H 'Content-Type: application/json' \
  4. --data-binary '{ "q": "american ninja" }'
  1. client.index('movies').search('American ninja')
  1. client.index('movies').search('American ninja')
  1. // Do a search
  2. $searchResults = $client->index('movies')->search('american ninja');
  3. // Get results in an Array using a getter
  4. $hits = $searchResults->getHits();
  5. // Get the decoded response of MeiliSearch, see response below
  6. $response = $searchResults->getRaw();
  1. client.index("movies").search("American ninja");
  1. client.index('movies').search('american ninja')
  1. client.Index("movies").Search("american ninja", &meilisearch.SearchRequest{})
  1. let results: SearchResults<Movie> = movies
  2. .search()
  3. .with_query("American ninja")
  4. .execute()
  5. .await
  6. .unwrap();
  1. let searchParameters = SearchParameters.query("American ninja")
  2. client.index("movies").search(searchParameters) { (result: Result<SearchResult<Movie>, Swift.Error>) in
  3. switch result {
  4. case .success(let searchResult):
  5. print(searchResult)
  6. case .failure(let error):
  7. print(error)
  8. }
  9. }
  1. await client.index('movies').search('American ninja');

Response: 200 Ok

  1. {
  2. "hits": [
  3. {
  4. "id": "2770",
  5. "title": "American Pie 2",
  6. "poster": "https://image.tmdb.org/t/p/w1280/q4LNgUnRfltxzp3gf1MAGiK5LhV.jpg",
  7. "overview": "The whole gang are back and as close as ever. They decide to get even closer by spending the summer together at a beach house. They decide to hold the biggest…",
  8. "release_date": 997405200
  9. },
  10. {
  11. "id": "190859",
  12. "title": "American Sniper",
  13. "poster": "https://image.tmdb.org/t/p/w1280/svPHnYE7N5NAGO49dBmRhq0vDQ3.jpg",
  14. "overview": "U.S. Navy SEAL Chris Kyle takes his sole mission—protect his comrades—to heart and becomes one of the most lethal snipers in American history. His pinpoint accuracy not only saves countless lives but also makes him a prime…",
  15. "release_date": 1418256000
  16. },
  17. ],
  18. "offset": 0,
  19. "limit": 20,
  20. "nbHits": 976,
  21. "exhaustiveNbHits": false,
  22. "processingTimeMs": 35,
  23. "query": "american "
  24. }

Search in an index with GET route

GET

  1. /indexes/:index_uid/search

Search for documents matching a specific query in the given index.

This route should only be used when no API key is required. If an API key is required, use the POST route instead.

Path variables

VariableDescription
index_uidThe index UID

Query parameters

Query ParameterDescriptionDefault Value
qQuery string“”
offsetNumber of documents to skip0
limitMaximum number of documents returned20
filterFilter queries by an attribute valuenull
facetsDistributionFacets for which to retrieve the matching countnull
attributesToRetrieveAttributes to display in the returned documents[“*”]
attributesToCropAttributes whose values have to be croppednull
cropLengthLength used to crop field values200
attributesToHighlightAttributes whose values will contain highlighted matching termsnull
matchesDefines whether an object that contains information about the matches should be returned or notfalse
sortSort search results according to the attributes and sorting order (asc or desc) specifiednull

filter accepts a query string. You can find about the filter syntax on our dedicated page.
cropLength is automatically rounded to match word boundaries.
sort requires attributes to be given as attribute:sorting_order. You can find more about the syntax on our dedicate page.

Learn more about how to use the search parameters.

When no search query is specified, a placeholder search is run instead.

Query terms enclosed in double quotes are treated as phrase searches.

Response

fieldDescriptiontype
hitsResults of the query[result]
offsetNumber of documents skippednumber
limitNumber of documents to takenumber
nbHitsTotal number of matchesnumber
exhaustiveNbHitsWhether nbHits is exhaustiveboolean
facetsDistributionDistribution of the given facetsobject
exhaustiveFacetsCountWhether facetsDistribution is exhaustiveboolean
processingTimeMsProcessing time of the querynumber
queryQuery originating the responsestring

Example

cURL

JS

  1. curl \
  2. -X GET 'http://localhost:7700/indexes/movies/search?q=american%20ninja'
  1. client.index('movies').search('American ninja')

Response: 200 Ok

  1. {
  2. "hits": [
  3. {
  4. "id": "2770",
  5. "title": "American Pie 2",
  6. "poster": "https://image.tmdb.org/t/p/w1280/q4LNgUnRfltxzp3gf1MAGiK5LhV.jpg",
  7. "overview": "The whole gang are back and as close as ever. They decide to get even closer by spending the summer together at a beach house. They decide to hold the biggest…",
  8. "release_date": 997405200
  9. },
  10. {
  11. "id": "190859",
  12. "title": "American Sniper",
  13. "poster": "https://image.tmdb.org/t/p/w1280/svPHnYE7N5NAGO49dBmRhq0vDQ3.jpg",
  14. "overview": "U.S. Navy SEAL Chris Kyle takes his sole mission—protect his comrades—to heart and becomes one of the most lethal snipers in American history. His pinpoint accuracy not only saves countless lives but also makes him a prime…",
  15. "release_date": 1418256000
  16. },
  17. ],
  18. "offset": 0,
  19. "limit": 20,
  20. "nbHits": 976,
  21. "exhaustiveNbHits": false,
  22. "processingTimeMs": 35,
  23. "query": "american "
  24. }