0
votes

I want to add an if statement to an elasticsearch index in fos_elastica.yml file.

I have a column called reference with two possible values user or organization.

I want to index the data only if that value is organization

I'm using friendsofsymfony/elastica-bundle version 5.1

I found something related here but I cannot make it work: [1]https://www.elastic.co/guide/en/elasticsearch/reference/master/ingest-conditional-complex.html

My config looks like this:


....... 

permissions:
  type: nested
  properties:
    id:
      type: integer
    role:
      type: string
    reference:
       type: object
       properties:
         id:
           type: integer
         name:
           type: string
           copy_to: organizations_names
         slug:
           type: string
           index: not_analyzed

......

1

1 Answers

0
votes

What is your Symfony version ?

I use Symfony 3 with elastica 5.0, and that works:

Create a queryBuilder in the repository of your entity and use it as provider in the config.yml file:

CategorieRepository.php

/**
 *
 * @return QueryBuilder
 */
public function getQbIsActive()
{
    $qb = $this->createQueryBuilder('c');

    return $qb->where($qb->expr()->eq('c.actif', ':actif'))
        ->setParameter(':actif', true);
}

config.yml:

indexes:
    app:
        types:
        categorie:
            mappings:
                    libelle: { type: string }
                persistence:
                    driver: orm
                    model: AppBundle\Entity\Categorie
                    provider: 
                        query_builder_method: getQbIsActive
                    listener: ~
                    finder: ~

Source:https://www.christophe-meneses.fr/article/autocompletion-avec-elasticsearch-dans-une-application-symfony