mongodbelasticsearchrace-conditionfoselasticabundle

FosElasticaBundle and race condition


I'm using Elastic Search with FOSElasticaBundle, when I add a new object in MongoDB database, the Elastic listener fires and indexes it.

My problem is when I add the new object and page where you can see all objects is refreshed and, sometimes, Elastic hasn't indexed yet and the new object isn't shown in the list.

If I do sleep for two seconds or busy-waitting until the object is indexed, it works, but I'm looking for a smarter solution.

I've tried disabling the listener and forcing the index with ObjectPersister:::insertOne($object), but I have the same problem again.

Mi Elastic configuration is:

fos_elastica:
  serializer: ~
  default_manager: mongodb
  clients:
    default: { host: "%elastica_host%", port: "%elastica_port%", logger: false }
  indexes:
    filter:
      finder: ~
      client: default
      index_name: filter_%platform_prefix%_%kernel.environment%
      types:
        request:
          serializer:
            groups: [filter]
          persistence:
            elastica_to_model_transformer:
              ignore_missing: true
            identifier: "token"
            driver: mongodb
            model: Src\MyBundle\Document\Request
            repository: Src\MyBundle\Repository\RequestFilterRepository
            finder: ~
            provider:
              query_builder_method: createElasticaQueryBuilder

Thanks and regards.


Solution

  • I've got to solve adding:

    $type = $this->container->get('fos_elastica.index.filter.request');
    $type->getIndex()->refresh();
    

    You can read more about refresh in https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-refresh.html