I have url /posts
to list latest 10 active posts. When I edit the last post (1st of those 10), the change seem not sync to elasticsearch at realtime.
Here is my code:
$post = $this->findPostBy....;
/** @var EntityManager $entityManager */
$entityManager = $this->getDoctrine()->getManager();
$post->setStatus(PostModel::STATUS_DELETED);
$entityManager->persist($post);
$entityManager->flush();
return $this->redirect('/posts');
After above code is executed, I will be redirected to /posts
but the last post which has just been edited and has its status changed to deleted still get listed as latest 10 active posts (which is definitely wrong). It only disappears when I refresh the page (/posts
).
Code for /posts
:
/** @var TransformedFinder $finder */
$finder = $this->container->get('fos_elastica.finder.index_name.post');
$query = new Query();
$filter = new Term(array('status' => PostModel::STATUS_ACTIVE));
$query->setFilter($filter);
$query->addSort(array('tttAt' => array('order' => 'desc')));
$posts = $finder->find($query);
My mapping:
types:
post:
mappings:
title:
type: string
analyzer: post_analyzer
description:
type: string
analyzer: post_analyzer
status:
type: integer
tttAt :
type : date
createdAt :
type : date
persistence:
driver: orm
model: MyBundle\Entity\Post
finder: ~
provider: ~
listener: ~
Can someone explain me why and how to solve that problem?
Updated: If I use xdebug to delay the redirection in about 1 or 2 seconds, the post will not be listed in /posts
any more. So I guest that there was not enough time for ElasticSearch to re-index. Any ideas or solutions?
Just found a solution, maybe not the best way but at least it works:
/** @var EntityManager $entityManager */
$entityManager = $this->getDoctrine()->getManager();
$post->setStatus(PostModel::STATUS_DELETED);
$entityManager->persist($post);
$entityManager->flush();
/** @var Index $type */
$type = $this->container->get('fos_elastica.index.index_name');
$type->refresh();
Refreshing index before redirection will do the strick :)