> ## Documentation Index
> Fetch the complete documentation index at: https://algolia.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Server-side search

> How to search on your server using Algolia SearchBundle.

export const Records = () => <Tooltip tip="A record is a searchable object in an Algolia index. Each record consists of named attributes." cta="Algolia records" href="/doc/guides/sending-and-managing-data/prepare-your-data#algolia-records">
    records
  </Tooltip>;

export const Index = () => <Tooltip tip="An Algolia index is a searchable dataset that consists of records and configuration settings. These settings define how the records are searched and ranked.">
    index
  </Tooltip>;

## Basic search

You can search for documents in your Algolia <Index /> with a single line of code.
The `search` method queries Algolia for matching results and returns an array of Doctrine-managed objects (ORM entities or ODM documents).
The objects are loaded from your database through the Doctrine Manager you pass in.

<Check>
  Both `SearchService` and `Doctrine\Persistence\ManagerRegistry` must be [injected into your class](/doc/framework-integration/symfony/getting-started/quick-start),
  so that you can use `$this->searchService` and `$this->managerRegistry`.
</Check>

```php PHP icon=code theme={"system"}
$em = $this->managerRegistry->getManagerForClass(Post::class);

$posts = $this->searchService->search($em, Post::class, 'query');
```

The `search` method only retrieves a list of IDs from the engine and uses them
to hydrate the corresponding Doctrine objects from your database.
You can only pass parameters to modify what to search, not modify the type of response.

If you want to modify the attributes to retrieve or retrieve data like `facets`, `facets_stats`, or `_rankingInfo`,
use the [`rawSearch`](#raw-search) method.

## Request options

The search and write methods in `SearchService` take a `$requestOptions` array as their last argument.
For `search()`, `rawSearch()`, and `count()`, any top-level keys that aren't HTTP options become search parameters sent to Algolia, for example, to change the page, filter the results, or choose what attributes to retrieve.
You can find all possibilities in the [list of search API parameters](/doc/api-reference/search-api-parameters).

For write methods (`index()`, `remove()`, `clear()`, `delete()`), `$requestOptions` is passed directly to the PHP client, so only supported request-option keys (such as `headers`, `queryParameters`, and timeouts) apply.

```php PHP icon=code theme={"system"}
$em = $this->managerRegistry->getManagerForClass(Post::class);

$posts = $this->searchService->search($em, Post::class, 'query', [
    'page' => 0,
    'hitsPerPage' => 10,
    'filters' => 'comment_count>10',
]);
```

## Raw search

If you want to get the raw results from Algolia,
use the `rawSearch` method.
This is the method you'll need to use if you want to retrieve the highlighted snippets or ranking information for instance.
It takes a `$requestOptions` array as an optional last parameter.

```php PHP icon=code theme={"system"}
$posts = $this->searchService->rawSearch(Post::class, 'query');

$posts = $this->searchService->rawSearch(Post::class, 'query', [
    'page' => 0,
    'hitsPerPage' => 10,
    'filters' => 'comment_count>10'
    // ...
]);
```

The difference with the `search` method is that you won't retrieve Doctrine-managed objects,
but a PHP associative array containing the raw engine response.

## Count

Use this method when you only need the number of results for a query.
`count` still triggers a search request.

```php PHP icon=code theme={"system"}
$posts = $this->searchService->count(Post::class, 'query');
```

## Clear

The `clear` method deletes the <Records /> in an index without affecting its settings.
It takes the class name associated with an index and, optionally, `$requestOptions` as the last parameter.
This method is [*waitable*](/doc/libraries/sdk/methods/search/wait-for-task).

```php PHP icon=code theme={"system"}
$posts = $this->searchService->clear(Post::class);

$posts = $this->searchService->clear(Post::class, [
    // ..any request options
]);

$posts = $this->searchService->clear(Post::class)->wait();
```

## Delete

Delete an index and all its settings, including links to its replicas.
It takes the class name associated with an index and, optionally, `$requestOptions` as the last parameter.
This method is [*waitable*](/doc/libraries/sdk/methods/search/wait-for-task) as well.

```php PHP icon=code theme={"system"}
$posts = $this->searchService->delete(Post::class);

$posts = $this->searchService->delete(Post::class, [
    // ..any request options
]);

$posts = $this->searchService->delete(Post::class)->wait();
```
