删除实体值时不删除搜索索引

时间:2013-02-23 09:31:48

标签: php doctrine symfony-2.1

我正在使用Symfony 2.1构建的工作门户网站。我已经为求职过程实施了ewz/EWZSearchBundle,如下所示:

/**
 * Displays a form to create a new Job entity.
 *
 * @Route("/new", name="job_new")
 * @Template("JobBundle:Job:new.html.twig")
 */
public function newAction(Request $request)
{
    $entity = new Job();
    $form   = $this->createForm(new JobType(), $entity);

    if('POST'===$request->getMethod()){
        $form->bind($request);

        if ($form->isValid()) {

            $entity->setOwner($this->getEmployer());

            $em = $this->getDoctrine()->getManager();
            $em->persist($entity);
            $em->flush();

            $location = $entity->getLocation();
            $loName = array();
            foreach ($location as $key => $loc) {
                $loName[] = $loc->getName();
            }

            $industry = $entity->getIndustry();
            $inName = array();
            foreach ($industry as $key => $ind) {
                $inName[] = $ind->getTitle();
                }
            $skills = $entity->getSkills();
            $skillName = array();
            foreach ($skills as $key => $skl) {
                $skillName[] = $skl->getTitle();
                }

            $education = $entity->getEducation();
            $educationName = array();
            foreach ($education as $key => $edu) {
                $educationName[] = $edu->getTitle();
                }

            $search = $this->get('ewz_search.lucene');

            $document = new Document();
            $document->addField(Field::keyword('key', $entity->getId()));
            $document->addField(Field::text('title', $entity->getTitle()));
            $document->addField(Field::text('url', $this->generateUrl('job_show', array('slug' => $entity->getSlug()))));
            $document->addField(Field::unstored('body', $entity->getDescription()));
            $document->addField(Field::text('location', implode(" ", $loName)));
            $document->addField(Field::text('industry', implode(" ", $inName)));
            $document->addField(Field::text('skills', implode(" ", $skillName)));
            $document->addField(Field::text('education', implode(" ", $educationName)));


            $search->addDocument($document);
            $search->updateIndex();

            return $this->redirect($this->generateUrl('job_show', array('slug' => $entity->getSlug())));
        }

    }

    return array(
        'entity' => $entity,
        'form'   => $form->createView(),
    );
}

当候选人通过以下任何领域搜索工作时,工作非常好:地点,行业,技能或教育。但是我的两个问题是:

  1. 在每个领域(地点,技能,行业或教育)的这个过程中,只有一个职位搜索适用,而我想要单独的搜索表单,例如在位置搜索中,只有位置字段不匹配另一个其余的领域。

  2. 第二个问题是当雇主删除其工作时,删除作业的索引也会在搜索过程中显示,而在删除作业后,还应从搜索索引中删除已删除作业的索引。

  3. 我尝试通过以下代码删除job_delete操作时的搜索索引:

    /**
     * Deletes a Job entity
     * @Route("/{slug}/delete", name="job_delete")
     * @Method("POST")
     */
    public function deleteAction(Request $request, $slug)
    {
        $em = $this->getDoctrine()->getManager();
                $entity = $em->getRepository('JobBundle:Job')
               ->findOneBy(array('slug' => $slug));
    
         if (!$entity) {
                throw $this->createNotFoundException('Unable to find Job entity.');
            }       
    
        $form = $this->createDeleteForm($entity->getSlug());
        $form->bind($request);       
        if ($form->isValid()) {
    
        $this->checkOwnerSecurity($entity); 
    
            $em->remove($entity);
            $em->flush();
            $search = $this->get('ewz_search.lucene');
            $index = $search->getIndex();
            $removePath = 'http://localhost/Jobtook/web/app_dev.php/employer/job/{slug}/delete';
            $hits = $index->find('job_delete' . $removePath);
            foreach ($hits as $hit) {
            $index->delete($hit->id);
            }
        }   
    
        return $this->redirect($this->generateUrl('job'));
    }
    
    private function createDeleteForm($slug)
    {
        return $this->createFormBuilder(array('slug' => $slug))
            ->add('slug', 'hidden')
            ->getForm()
        ;
    }
    

    但是当我创建许多作业并且只删除一个作业时,通过此代码,所有作业都将从搜索索引中删除,即使是仍存在于数据库中的剩余作业。我不知道自己做错了什么。

0 个答案:

没有答案