我正在使用 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(),
);
}
当候选人通过以下任何领域搜索工作时,它工作得非常好:位置、行业、技能或教育。但是我有两个问题:
在这个过程中,对于每个领域(位置、技能、行业或教育),只有一个职位搜索适用,而我想要单独的搜索表单,例如在位置搜索中,只有位置字段匹配,而不匹配其他剩余字段。
第二个问题是当雇主删除他的工作时,被删除工作的索引也会显示在搜索过程中,而在工作被删除后,被删除工作的索引也应该从搜索索引中删除。
我试图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()
;
}
但是通过这段代码,当我创建许多作业并只删除一个作业时,所有作业都会从搜索索引中删除,即使剩余的作业仍然存在于数据库中。我不知道我做错了什么。