如何在特定类型中正确添加自定义 属性
How to properly add a custom property in a specific type
在搜索字段中,我想向用户建议按找到的餐厅数量(降序)排序的相同名称的城市。
因此我想添加一个自定义字段,它可以帮助我根据餐厅数量对相似的城市名称(即 'Washington' => 在阿肯色州、伊利诺伊州、加利福尼亚州等)进行排序。
我已阅读以下内容 cookbook 以添加自定义 属性 但是我对 sf 和 elastica 的知识不足使我无法将听众放在正确的位置。
假设我有一个 city
和一个 restaurant
类型,配置如下:
city:
mappings:
name: ~
location: { type: geo_point }
restaurant:
mappings:
name: ~
location: { type: geo_point }
我知道对于添加到我的索引中的每个城市,我应该 地理定位 它附近的每家餐馆(例如约 5 英里)。我应该怎么做?
上下文:
- php 5.6
- symfony 2.3
- friendsofsymfony/elastica-bundle3.1.x
如果对我有帮助,我会回答我的问题:
如果您想对特定类型进行地理定位(或执行任何其他操作),只需对其进行过滤即可:
// assume correct namespaces are used...
class CustomPropertyListener implements EventSubscriberInterface
{
private $container;
public function __construct(Container $container)
{
$this->container = $container;
}
public function addCustomProperty(TransformEvent $event)
{
if ($event->getDocument()->getType() === 'city') {
$coord = $event->getDocument()->get('location');
// call to the service which can find restaurants
$finder = $this->container->get('fos_elastica.finder.restaurant');
$filter = new GeoDistance('location', $coord, '5mi');
$all = new Query(new MatchAll());
$query = new Filtered($all, $filter);
$entities = $finder->find($query);
$document->set('numRestaurants', count($entities));
}
}
}
$container
定义在services
定义文件中。例如在 YAML 中:
acme.listener.custom_property:
class: AcmeBundle\EventListener\CustomPropertyListener
arguments: ['@service_container']
tags:
- { name: kernel.event_subscriber }
应该注意的是,elastic
+ elastica-bundle
对于如此复杂和关键的组件的记录很少。许多重要信息分散在网络或代码中。我希望开发人员意识到这一点!
在搜索字段中,我想向用户建议按找到的餐厅数量(降序)排序的相同名称的城市。
因此我想添加一个自定义字段,它可以帮助我根据餐厅数量对相似的城市名称(即 'Washington' => 在阿肯色州、伊利诺伊州、加利福尼亚州等)进行排序。
我已阅读以下内容 cookbook 以添加自定义 属性 但是我对 sf 和 elastica 的知识不足使我无法将听众放在正确的位置。
假设我有一个 city
和一个 restaurant
类型,配置如下:
city:
mappings:
name: ~
location: { type: geo_point }
restaurant:
mappings:
name: ~
location: { type: geo_point }
我知道对于添加到我的索引中的每个城市,我应该 地理定位 它附近的每家餐馆(例如约 5 英里)。我应该怎么做?
上下文:
- php 5.6
- symfony 2.3
- friendsofsymfony/elastica-bundle3.1.x
如果对我有帮助,我会回答我的问题:
如果您想对特定类型进行地理定位(或执行任何其他操作),只需对其进行过滤即可:
// assume correct namespaces are used...
class CustomPropertyListener implements EventSubscriberInterface
{
private $container;
public function __construct(Container $container)
{
$this->container = $container;
}
public function addCustomProperty(TransformEvent $event)
{
if ($event->getDocument()->getType() === 'city') {
$coord = $event->getDocument()->get('location');
// call to the service which can find restaurants
$finder = $this->container->get('fos_elastica.finder.restaurant');
$filter = new GeoDistance('location', $coord, '5mi');
$all = new Query(new MatchAll());
$query = new Filtered($all, $filter);
$entities = $finder->find($query);
$document->set('numRestaurants', count($entities));
}
}
}
$container
定义在services
定义文件中。例如在 YAML 中:
acme.listener.custom_property:
class: AcmeBundle\EventListener\CustomPropertyListener
arguments: ['@service_container']
tags:
- { name: kernel.event_subscriber }
应该注意的是,elastic
+ elastica-bundle
对于如此复杂和关键的组件的记录很少。许多重要信息分散在网络或代码中。我希望开发人员意识到这一点!