简体   繁体   English

使用doctrine symfony2的ongr-io elasticsearch bundle实现

[英]ongr-io elasticsearch bundle implementation with doctrine symfony2

In our symfony2 project we want to implement elasticsearch with ongr-io bundle, but our whole system is built on doctrine. 在我们的symfony2项目中,我们希望使用ongr-io bundle实现elasticsearch,但我们的整个系统都是基于doctrine构建的。 Is it possible to somehow use ongr-io elasticsearch bundle without totally redoing whole database with documents instead of entities? 有可能以某种方式使用ongr-io elasticsearch bundle而不用文件而不是实体完全重做整个数据库吗?

Entity that I want to index (fields for search would be: name, ChildCategory and ParentCategory): 我想要索引的实体(搜索字段为:name,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;
    }

it's very interesting. 这很有趣。

The first thing is Entity location. 第一件事是实体位置。 The document for ElasticsearchBundle has to be in Document directory. ElasticsearchBundle的文档必须位于Document目录中。 It's not a big deal. 没什么大不了的。 All you need to do is to create an empty class and extend entity with @Document annotation. 您需要做的就是创建一个空类并使用@Document注释扩展实体。 Next are the fields. 接下来是字段。 With name field there is no problem at all, just add @property annotation and you are good. 使用name字段完全没有问题,只需添加@property注释即可。 More interesting is with relations. 更有趣的是关系。 My suggestion would be to create separate property and in the getter return value from the relation for that field. 我的建议是创建单独的属性,并从该字段的关系中获取getter返回值。 I hope you got the idea. 我希望你有这个主意。

Update: 更新:

Here's an example of documents: 这是一个文件的例子:

AppBundle/Entity/Post 的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/Document/Post 的appbundle /文件/后

<?php

namespace AppBundle\Document;

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

/**
 * @ES\Document()
 */
class Post extends OldPost
{
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM