存储库删除查询后,Symfony ElasticSearch不会同步

时间:2017-08-17 12:39:54

标签: php symfony elasticsearch doctrine-orm foselasticabundle

我正在使用foselastica,我收到错误“找不到所有Elastica结果的相应Doctrine对象(0)(7).ID:52,51,50,48,49,47,46”在存储库中删除查询后,但查询确实在mysql数据库中删除:

class CandidatesRepository extends EntityRepository
{
    public function candidatesFileimportDelete($id)
    {
        $q = $this->createQueryBuilder('c')->delete()
            ->where('c.fileImportId = :id')->setParameter('id', $id);
        return $q->getQuery()->getResult();
    }
}

当我使用教条删除时,它会同步,所有候选人都会从ES中删除

$candidates = $this->getDoctrine()
    ->getRepository(Candidates::class)
    ->findAll();


foreach($candidates as $candidate){
    $em = $this->getDoctrine()->getManager();
    $em->remove($candidate);
    $em->flush();
}

代码示例:

实体

    /**
     * @ORM\Entity
     * @ORM\Table(name="candidates_user")
     * @ORM\Entity(repositoryClass="CandidatesBundle\Entity\Repository\CandidatesRepository")

     */
    class Candidates
    {
// filled with privatre properties and public getters and setters
    } 

config.yml

fos_elastica:
clients:
    default: { host: localhost, port: 9200 }
indexes:
    candidates:
        types:
            candidates:
                mappings:
                    id: { type: integer }
                    name: { type: string }
                    job: { type: string }
                    approached: {type: integer}
                    level: {type: string }
                    skills: {type: string}
                    email: {type: string}
                    mobile: {type: string}
                    city: {type: string}
                    province: {type: string}
                    country: {type: string}
                    linkedinUrl: {type: string}
                    addedCompany: {type: string}
                    note: {type: string}
                    dateAdded: {type: date}
                    deleted: {type: integer }
                    dateDeleted: {type: date}
                    platform: {type: string}
                    fileImportId: {type: integer}

                persistence:
                    driver: orm
                    model: CandidatesBundle\Entity\Candidates
                    provider:
                       batch_size: 10
                       debug_logging: false
                    listener:
                        is_indexable_callback: "isSearchable"
                        insert: true
                        update: true
                        delete: true
                    finder: ~
                    repository: CandidatesBundle\Entity\Repository\CandidatesRepository

我收集候选人的弹性服务的一部分

    /**
 *  Search all active candidates
 */
public function getAllCandidates($request, $showDeleted = false)
{

    $finder = $this->container->get('fos_elastica.finder.candidates.candidates');
    $boolQuery = new \Elastica\Query\BoolQuery();

    if (!empty($request->get('s')) && $request->get('c')) {
        if (in_array($request->get('c'), self::SEARCH_CATEGORIES, true)) {
            $boolQuery->addMust(
                $this->setQueryString(array($request->get('c')), $request->get('s'))
            );
        }
    }

    if ($showDeleted == false) {
        $boolQuery->addMust(
            $this->setTerm('deleted', array(0))
        );
    }

    $query = $this->setSort($boolQuery, array('dateAdded' => array('order' => 'asc')));

    if (!empty($request->get('sort')) && $request->get('cat')) {
        if (in_array($request->get('cat'), self::CAT, true) &&
            in_array($request->get('sort'), self::SORT)) {
            $query = $this->setSort($boolQuery, array($request->get('cat') => array('order' => $request->get('sort'))));
        }
    }

    $candidates = $finder->find($query, 3000);
    return $candidates;
}

必需的捆绑包:

"require": {
    "php": ">=5.5.9",
    "doctrine/doctrine-bundle": "^1.6",
    "doctrine/doctrine-migrations-bundle": "1.2",
    "doctrine/orm": "^2.5",
    "friendsofsymfony/elastica-bundle": "^4.0",
    "friendsofsymfony/user-bundle": "~2.0",
    "gedmo/doctrine-extensions": "^2.4",
    "incenteev/composer-parameter-handler": "^2.0",
    "liuggio/excelbundle": "^2.1",
    "sensio/distribution-bundle": "^5.0.19",
    "sensio/framework-extra-bundle": "^3.0.2",
    "symfony/monolog-bundle": "^3.1.0",
    "symfony/polyfill-apcu": "^1.0",
    "symfony/swiftmailer-bundle": "^2.3.10",
    "symfony/symfony": "3.3.*",
    "twig/twig": "^1.0||^2.0"
},
"require-dev": {
    "doctrine/doctrine-fixtures-bundle": "^2.3",
    "sensio/generator-bundle": "^3.0",
    "symfony/phpunit-bridge": "^3.0"
},

2 个答案:

答案 0 :(得分:1)

¿是否必须使用管理器执行删除才能触发FosElastica侦听的同步事件?如果是这样,您可以使用您的查询获得te实体,然后删除usign $ manager-> remove()

不确定100%,但我目前正在使用FosElastica并且它保持ES更新,而且我没有使用查询构建器进行删除。

答案 1 :(得分:0)

如果使用更新或删除之类的指导性命令,则不会运行FOSElastica的侦听器。在这种情况下,您应该利用fos:elastica:populate命令