在我们的symfony2项目中,我们希望使用ongr-io捆绑包实现elasticsearch,但是我们的整个系统都是建立在原则之上的。是否有可能以某种方式使用 ongr-io elasticsearch bundle,而无需用文档而不是实体完全重做整个数据库?
我要编制索引的实体(用于搜索的字段为:名称、子类别和父类别):
/**
* Course
*
* @ORMTable(name="course")
* @ORMEntity(repositoryClass="CoreBundleRepositoryCourseRepository")
* @ORMHasLifecycleCallbacks
*/
class Course
{
/**
* @var int
*
* @ORMColumn(name="id", type="integer")
* @ORMId
* @ORMGeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORMColumn(name="name", type="string", length=255, unique=false)
*/
private $name;
/**
* @var ParentCategory
*
* @ORMManyToOne(targetEntity="ParentCategory")
* @ORMJoinColumn(name="parentCategory_id", referencedColumnName="id")
*/
private $parentCategory;
/**
* @var ChildCategory
*
* @ORMManyToOne(targetEntity="ChildCategory", inversedBy="courses")
* @ORMJoinColumn(name="childCategory_id", referencedColumnName="id")
*/
private $childCategory;
/**
* @var City
*
* @ORMManyToOne(targetEntity="City")
* @ORMJoinColumn(name="city_id", referencedColumnName="id")
*/
private $city;
/**
* @var Users
*
* @ORMManyToOne(targetEntity="UserBundleEntityUsers", inversedBy="course")
* @ORMJoinColumn(name="owner_id", referencedColumnName="id")
*/
private $owner;
/**
* @var text
*
* @ORMColumn(name="description", type="text")
*/
private $description;
/**
* @var Picture
*
* @ORMOneToMany(targetEntity="CoreBundleEntityPicture", mappedBy="course", cascade={"persist", "remove"})
*/
private $pictures;
/**
* @var Ratings
*
* @ORMOneToMany(targetEntity="CoreBundleEntityRatings", mappedBy="courseid")
*/
private $ratings;
public $avgRating;
}
很有趣。
第一件事是实体位置。ElasticsearchBundle 的文档必须位于Document
目录中。没什么大不了的。您需要做的就是创建一个空类并使用@Document
注释扩展实体。接下来是字段。使用name
字段完全没有问题,只需添加@property
注释即可。更有趣的是关系。我的建议是创建单独的属性,并在 getter 中从该字段的关系返回值。我希望你明白了。
更新:
下面是一个文档示例:
AppBundle/Entity/Post
<?php
namespace AppBundleEntity;
use DoctrineORMMapping as ORM;
use ONGRElasticsearchBundleAnnotation as ES;
/**
* Post
*
* @ORMTable(name="post")
* @ORMEntity(repositoryClass="AppBundleRepositoryPostRepository")
*/
class Post
{
/**
* @var int
*
* @ORMColumn(name="id", type="integer")
* @ORMId
* @ORMGeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ESProperty(type="string")
* @ORMColumn(name="title", type="string", length=255, nullable=true)
*/
private $title;
/**
* @var User
*
* @ORMManyToOne(targetEntity="User")
* @ORMJoinColumn(name="user_id", referencedColumnName="id")
*/
private $user;
/**
* @var string
*
* @ESProperty(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 AppBundleEntityUser $user
*
* @return Post
*/
public function setUser(AppBundleEntityUser $user = null)
{
$this->user = $user;
return $this;
}
/**
* Get user
*
* @return AppBundleEntityUser
*/
public function getUser()
{
return $this->user;
}
/**
* @return string
*/
public function getEsUser()
{
return $this->esUser;
}
/**
* @param string $esUser
*/
public function setEsUser($esUser)
{
$this->esUser = $esUser;
}
}
应用包/文档/发布
<?php
namespace AppBundleDocument;
use ONGRElasticsearchBundleAnnotation as ES;
use AppBundleEntityPost as OldPost;
/**
* @ESDocument()
*/
class Post extends OldPost
{
}