1

在我们的 symfony2 项目中,我们想用 ongr-io 包实现弹性搜索,但是我们的整个系统是建立在教义上的。是否有可能以某种方式使用 ongr-io elasticsearch 包而不用文档而不是实体完全重做整个数据库?

我要索引的实体(搜索字段为:名称、ChildCategory 和 ParentCategory):

/**
 * Course
 *
 * @ORM\Table(name="course")
 * @ORM\Entity(repositoryClass="CoreBundle\Repository\CourseRepository")
 * @ORM\HasLifecycleCallbacks
 */
    class Course
    {
        /**
         * @var int
         *
         * @ORM\Column(name="id", type="integer")
         * @ORM\Id
         * @ORM\GeneratedValue(strategy="AUTO")
         */
        private $id;

        /**
         * @var string
         *
         * @ORM\Column(name="name", type="string", length=255, unique=false)
         */
        private $name;

        /**
         * @var ParentCategory
         *
         * @ORM\ManyToOne(targetEntity="ParentCategory")
         * @ORM\JoinColumn(name="parentCategory_id", referencedColumnName="id")
         */
        private $parentCategory;

        /**
         * @var ChildCategory
         *
         * @ORM\ManyToOne(targetEntity="ChildCategory", inversedBy="courses")
         * @ORM\JoinColumn(name="childCategory_id", referencedColumnName="id")
         */
        private $childCategory;

        /**
         * @var City
         *
         * @ORM\ManyToOne(targetEntity="City")
         * @ORM\JoinColumn(name="city_id", referencedColumnName="id")
         */
        private $city;

        /**
         * @var Users
         *
         * @ORM\ManyToOne(targetEntity="UserBundle\Entity\Users", inversedBy="course")
         * @ORM\JoinColumn(name="owner_id", referencedColumnName="id")
         */
        private $owner;

        /**
         * @var text
         *
         * @ORM\Column(name="description", type="text")
         */
        private $description;

        /**
         * @var Picture
         *
         * @ORM\OneToMany(targetEntity="CoreBundle\Entity\Picture", mappedBy="course", cascade={"persist", "remove"})
         */
        private $pictures;

        /**
         * @var Ratings
         *
         * @ORM\OneToMany(targetEntity="CoreBundle\Entity\Ratings", mappedBy="courseid")
         */
        private $ratings;

        public $avgRating;
    }
4

1 回答 1

0

这很有趣。

首先是实体位置。ElasticsearchBundle 的文档必须在Document目录中。没什么大不了的。您需要做的就是创建一个空类并使用@Document注释扩展实体。接下来是字段。有了name字段完全没有问题,只需添加@property注释就可以了。更有趣的是关系。我的建议是创建单独的属性,并在该字段的关系的 getter 返回值中。我希望你明白了。

更新:

以下是文档示例:

AppBundle/实体/帖子

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use ONGR\ElasticsearchBundle\Annotation as ES;

/**
 * Post
 *
 * @ORM\Table(name="post")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\PostRepository")
 */
class Post
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ES\Property(type="string")
     * @ORM\Column(name="title", type="string", length=255, nullable=true)
     */
    private $title;

    /**
     * @var User
     *
     * @ORM\ManyToOne(targetEntity="User")
     * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
     */
    private $user;

    /**
     * @var string
     *
     * @ES\Property(name="user", type="string")
     */
    private $esUser;

    /**
     * Get id
     *
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set title
     *
     * @param string $title
     *
     * @return Post
     */
    public function setTitle($title)
    {
        $this->title = $title;

        return $this;
    }

    /**
     * Get title
     *
     * @return string
     */
    public function getTitle()
    {
        return $this->title;
    }

    /**
     * Set user
     *
     * @param \AppBundle\Entity\User $user
     *
     * @return Post
     */
    public function setUser(\AppBundle\Entity\User $user = null)
    {
        $this->user = $user;

        return $this;
    }

    /**
     * Get user
     *
     * @return \AppBundle\Entity\User
     */
    public function getUser()
    {
        return $this->user;
    }

    /**
     * @return string
     */
    public function getEsUser()
    {
        return $this->esUser;
    }

    /**
     * @param string $esUser
     */
    public function setEsUser($esUser)
    {
        $this->esUser = $esUser;
    }
}

AppBundle/文档/帖子

<?php

namespace AppBundle\Document;

use ONGR\ElasticsearchBundle\Annotation as ES;
use AppBundle\Entity\Post as OldPost;

/**
 * @ES\Document()
 */
class Post extends OldPost
{
}
于 2016-06-10T13:18:49.093 回答