<commit_before># Symfony Search Bundle

This package will help you get your data indexed in a dedicated Search Engine

---
### New package

You're looking at the new major version of this package. If your looking for the previous one, it was moved to the [`2.x` branch](https://github.com/algolia/search-bundle/tree/2.x).

---

**Table of Contents**

- [Compatibility](#compatibility)
- [Install](#install)
- [Configuration](#configuration)
  - [Credentials](#credentials)
- [Search](#search)
  - [Pagination](#pagination)
  - [Count](#count)
  - [Advanced search](#advanced-search)
- [Index entities](#index-entities)
  - [Automatically](#automatically)
  - [Manually](#manually)
- [Normalizers](#normalizers)
  - [Custom Normalizers](#custom-normalizers)
    - [Using a dedicated normalizer](#using-a-dedicated-normalizer)
    - [Using `normalize` method in entity](#using-normalize-method-in-entity)
    - [Create Normalizer for `Algolia\SearchBundle` only](#create-normalizer-for-algoliasearchbundle-only)
  - [Using `@Groups` annotation](#using-normalizer-groups)
- [Engine](#engine)
  - [The `NullEngine`](#the-nullengine)
  - [Using another engine](#using-another-engine)
- [Using the Algolia Client (Advanced)](#using-the-algolia-client-advanced)
  - [Example](#example)
- [Managing settings](#managing-settings)
- [Tests](#tests)
  - [About `AlgoliaSyncEngine`](#about-algoliasyncengine)

## Compatibility

This package is compatible with Symfony 3.4 and higher.

If your app runs an older version, you can use the previous version, [available on the 2.x branch](https://github.com/algolia/search-bundle/tree/2.x).

## Install

With composer

```
composer require algolia/search-bundle
```

## Configuration

The following configuration assume you are using [Symfony/demo](http://github.com/Symfony/demo) project.

```yaml
algolia_search:
  prefix: demoapp_
  indices:
    - name: posts
      class: App\Entity\Post

    - name: comments
      class: App\Entity\Comment
      enable_serializer_groups: true
```

### Credentials

You will also need to provide Algolia App ID and Admin API key. By default they are loaded from env variables `ALGOLIA_APP_ID` and `ALGOLIA_API_KEY`.

If you don't use env variable, you can set them in your `parameters.yml`.

```yml
parameters:
    env(ALGOLIA_APP_ID): K7MLRQH1JG
    env(ALGOLIA_API_KEY): <KEY>
```

## Search

In this example we'll search for posts.

```php
$em           = $this->getDoctrine()->getManager();
$indexManager = $this->get('search.index_manager');

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

Note that this method will return an array of entities retrieved by Doctrine object manager (data are pulled from the database).

If you want to get the raw result from Algolia, use the `rawSearch` method.


```php
$indexManager = $this->get('search.index_manager');

$posts = $indexManager->rawSearch('query', Post::class);
```

### Pagination

To get a specific page, define the `page` (and `nbResults` if you want).

```php
$em           = $this->getDoctrine()->getManager();
$indexManager = $this->get('search.index_manager');

$posts = $indexManager->search('query', Post::class, $em, 2);
// Or
$posts = $indexManager->search('query', Post::class, $em, 2, 100);
```

### Count

```php
$indexManager = $this->get('search.index_manager');

$posts = $indexManager->count('query', Post::class);
```

### Advanced search

Pass anything you want in the `parameters` array. You can pass it in any search-related method.


```php
$indexManager = $this->get('search.index_manager');

$posts = $indexManager->count('query', Post::class, 0, 10, ['filters' => 'comment_count>10']);
```


## Index entities

### Automatically

The bundle will listen to `postPersist` and `preRemove` doctrine events to keep your data in sync. You have nothing to do.

### Manually

If you want to update a post manually, you can get the `IndexManager` from the container and call the `index` method manually.

```php
$em           = $this->getDoctrine()->getManager();
$indexManager = $this->get('search.index_manager');
$post         = $em->getRepository(Post::class)->findBy(['author' => 1]);

$indexManager->index($post, $em);

```

## Normalizers<commit_msg>Add note about command<commit_after># Symfony Search Bundle

This package will help you get your data indexed in a dedicated Search Engine

---
### New package

You're looking at the new major version of this package. If your looking for the previous one, it was moved to the [`2.x` branch](https://github.com/algolia/search-bundle/tree/2.x).

---

**Table of Contents**

- [Compatibility](#compatibility)
- [Install](#install)
- [Configuration](#configuration)
  - [Credentials](#credentials)
- [Search](#search)
  - [Pagination](#pagination)
  - [Count](#count)
  - [Advanced search](#advanced-search)
- [Index entities](#index-entities)
  - [Automatically](#automatically)
  - [Manually](#manually)
- [Normalizers](#normalizers)
  - [Custom Normalizers](#custom-normalizers)
    - [Using a dedicated normalizer](#using-a-dedicated-normalizer)
    - [Using `normalize` method in entity](#using-normalize-method-in-entity)
    - [Create Normalizer for `Algolia\SearchBundle` only](#create-normalizer-for-algoliasearchbundle-only)
  - [Using `@Groups` annotation](#using-normalizer-groups)
- [Engine](#engine)
  - [The `NullEngine`](#the-nullengine)
  - [Using another engine](#using-another-engine)
- [Using the Algolia Client (Advanced)](#using-the-algolia-client-advanced)
  - [Example](#example)
- [Managing settings](#managing-settings)
- [Tests](#tests)
  - [About `AlgoliaSyncEngine`](#about-algoliasyncengine)

## Compatibility

This package is compatible with Symfony 3.4 and higher.

If your app runs an older version, you can use the previous version, [available on the 2.x branch](https://github.com/algolia/search-bundle/tree/2.x).

## Install

With composer

```
composer require algolia/search-bundle
```

## Configuration

The following configuration assume you are using [Symfony/demo](http://github.com/Symfony/demo) project.

```yaml
algolia_search:
  prefix: demoapp_
  indices:
    - name: posts
      class: App\Entity\Post

    - name: comments
      class: App\Entity\Comment
      enable_serializer_groups: true
```

### Credentials

You will also need to provide Algolia App ID and Admin API key. By default they are loaded from env variables `ALGOLIA_APP_ID` and `ALGOLIA_API_KEY`.

If you don't use env variable, you can set them in your `parameters.yml`.

```yml
parameters:
    env(ALGOLIA_APP_ID): K7MLRQH1JG
    env(ALGOLIA_API_KEY): <KEY>
```

## Search

In this example we'll search for posts.

```php
$em           = $this->getDoctrine()->getManager();
$indexManager = $this->get('search.index_manager');

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

Note that this method will return an array of entities retrieved by Doctrine object manager (data are pulled from the database).

If you want to get the raw result from Algolia, use the `rawSearch` method.


```php
$indexManager = $this->get('search.index_manager');

$posts = $indexManager->rawSearch('query', Post::class);
```

### Pagination

To get a specific page, define the `page` (and `nbResults` if you want).

```php
$em           = $this->getDoctrine()->getManager();
$indexManager = $this->get('search.index_manager');

$posts = $indexManager->search('query', Post::class, $em, 2);
// Or
$posts = $indexManager->search('query', Post::class, $em, 2, 100);
```

### Count

```php
$indexManager = $this->get('search.index_manager');

$posts = $indexManager->count('query', Post::class);
```

### Advanced search

Pass anything you want in the `parameters` array. You can pass it in any search-related method.


```php
$indexManager = $this->get('search.index_manager');

$posts = $indexManager->count('query', Post::class, 0, 10, ['filters' => 'comment_count>10']);
```


## Index entities

### Automatically

The bundle will listen to `postPersist` and `preRemove` doctrine events to keep your data in sync. You have nothing to do.

### Manually

If you want to update a post manually, you can get the `IndexManager` from the container and call the `index` method manually.

```php
$em           = $this->getDoctrine()->getManager();
$indexManager = $this->get('search.index_manager');
$post         = $em->getRepository(Post::class)->findBy(['author' => 1]);

$indexManager->index($post, $em);

```

### With commands

The bundle ships with two commands to import data into an index or to clear it.

```
# Import all data
php bin/console search:import

# Select what data to import
php bin/console search:import --indices=posts,comments
```

It works the same way to clear data


```
# Clear all indices
php bin/console search:clear

# Select what indices to clear
php bin/console search:clear --indices=posts,comments
```

## Normalizers
{'fraction_non_alphanumeric': 0.14963380281690142, 'fraction_numerical': 0.00428169014084507, 'mean_word_length': 4.56140350877193, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 53, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3154263', 'n_tokens_mistral': 2843, 'n_tokens_neox': 2687, 'n_words': 822}