Symfony Elastic FOSElasticaBundle搜索

时间:2015-10-05 13:15:25

标签: php symfony elasticsearch doctrine-orm

我尝试通过电子邮件找到实体团队,但是有空结果,当我创建实体时,实体创建弹性,但是当我在Elastic中更改旧实体的电子邮件时不更新。我有FOSElasticaBundle~3.0“我创建配置

fos_elastica:
clients:
    default: { host: '*******.us-east-1.aws.found.io', port: 9200 }
indexes:
    aog:
        types:
            user:
                mappings:
                    id:
                      type: integer
                    username:
                      type: string
                    company:
                      type: string
                    created:
                      type: date
                    email:
                      type: string
                persistence:
                    # the driver can be orm, mongodb or propel
                    # listener and finder are not supported by
                    # propel and should be removed
                    driver: orm
                    model: Artel\ProfileBundle\Entity\Teams
                    provider: ~
                    listener: ~
                    finder: ~

和实体

/**
  * Teams
  *
  * @ORM\Table(name="teams")
  * @Gedmo\SoftDeleteable(fieldName="deletedAt")
  * @ORM\Entity(repositoryClass="Artel\ProfileBundle\Entity\Repository\TeamsRepository")
  * @Search(repositoryClass="Artel\ProfileBundle\Entity\Repository\ArticleRepository")
  */

 class Teams
 {
/**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
protected $id;

/**
 * @Gedmo\Slug(fields={"company"})
 * @ORM\Column(name="name", type="string", length=255, nullable=false)
 * @Assert\Length(min=3, max=255)
 */
protected $username;

/**
 * @var string
 *
 * @ORM\Column(name="email", type="string", length=255,  unique=true, nullable=true)
 * @Assert\Length(min=3, max=255)
 */
protected $email;

我创建了模型

<?php

 namespace Artel\ProfileBundle\Model;

 use Symfony\Component\HttpFoundation\Request;

 class ArticleSearch
 {
// begin of publication range
protected $dateFrom;

// end of publication range
protected $dateTo;

// published or not
protected $created;

protected $username;

protected $company;

protected $email;

public function __construct()
{
    // initialise the dateFrom to "one month ago", and the dateTo to "today"
    $date = new \DateTime();
    $month = new \DateInterval('P1Y');
    $date->sub($month);
    $date->setTime('00','00','00');

    $this->dateFrom = $date;
    $this->dateTo = new \DateTime();
    $this->dateTo->setTime('23','59','59');
}

public function setDateFrom($dateFrom)
{
    if($dateFrom != ""){
        $dateFrom->setTime('00','00','00');
        $this->dateFrom = $dateFrom;
    }

    return $this;
}

public function getDateFrom()
{
    return $this->dateFrom;
}

public function setDateTo($dateTo)
{
    if($dateTo != ""){
        $dateTo->setTime('23','59','59');
        $this->dateTo = $dateTo;
    }

    return $this;
}

public function clearDates(){
    $this->dateTo = null;
    $this->dateFrom = null;
}

public function getDateTo()
{
    return $this->dateTo;
}

public function getCreated()
{
    return $this->created;
}

public function setCreated($created)
{
    $this->created = $created;

    return $this;
}

public function getUsername()
{
    return $this->username;
}

public function setUsername($username)
{
    $this->username = $username;

    return $this;
}

public function getCompany()
{
    return $this->company;
}

public function setCompany($company)
{
    $this->company = $company;

    return $this;
}

public function setEmail($email)
{
    $this->email = $email;

    return $this;
}

public function getEmail()
{
    return $this->email;
}
}

并创建表单

<?php

 namespace Artel\AdminBundle\Form;

 use Artel\ProfileBundle\Model\ArticleSearch;
 use Symfony\Component\Form\AbstractType;
 use Symfony\Component\Form\FormBuilderInterface;
 use Symfony\Component\OptionsResolver\OptionsResolverInterface;

 class ArticleSearchType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('username',null,array(
            'required' => false,
        ))
        ->add('company',null,array(
            'required' => false,
        ))
        ->add('email',null,array(
            'required' => false,
        ))
        ->add('dateFrom', 'date', array(
            'required' => false,
            'widget' => 'single_text',
        ))
        ->add('dateTo', 'date', array(
            'required' => false,
            'widget' => 'single_text',
        ))

        ->add('search','submit')
    ;
}

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    parent::setDefaultOptions($resolver);
    $resolver->setDefaults(array(
        // avoid to pass the csrf token in the url (but it's not protected anymore)
        'csrf_protection' => false,
        'data_class' => 'Artel\ProfileBundle\Model\ArticleSearch'
    ));
}

public function getName()
{
    return 'article_search_type';
}
}

和我的控制器

class ArticleController extends Controller
 {
/**
 * Displays a form to edit an existing Project entity.
 *
 * @Route("/team/search", name="admin-article-search")
 * @Method("GET")
 * @Template()
 */
public function listAction(Request $request)
{
    $articleSearch = new ArticleSearch();

    $articleSearchForm = $this->get('form.factory')
        ->createNamed(
            '',
            'article_search_type',
            $articleSearch,
            array(
                'action' => $this->generateUrl('admin-article-search'),
                'method' => 'GET'
            )
        );
    $articleSearchForm->handleRequest($request);
    $articleSearch = $articleSearchForm->getData();

    $elasticaManager = $this->container->get('fos_elastica.manager');
    $results = $elasticaManager->getRepository('ArtelProfileBundle:Teams')->search($articleSearch);

    return [
        'results' => $results,
        'articleSearchForm' => $articleSearchForm->createView(),
    ];
}
}

在我的行动中我使用了存储库

class ArticleRepository extends Repository
{
public function search(ArticleSearch $articleSearch)
{
    if ($articleSearch->getEmail() != null && $articleSearch != '') {
        $query = new \Elastica\Query\Match();
        $query->setFieldQuery('article.email', $articleSearch->getEmail());
    } else {
        $query = new \Elastica\Query\MatchAll();
    }
    $baseQuery = $query;
    return $this->find($query);
}
}

为什么弹性找不到团队,为什么不按字段更新数据???

1 个答案:

答案 0 :(得分:1)

我在这行代码中看到第一个错误:

$query->setFieldQuery('article.email', $articleSearch->getEmail());

您正在按照&#39; article.email&#39;进行搜索但你没有映射。在映射中,您只有“电子邮件”。试试这个:

$query->setFieldQuery('email', $articleSearch->getEmail());