symfony - How to properly add a custom property in a specific type -


in search field, want suggest users cities same names sorting number of restaurants found (in descending order).

therefore want add custom field me sort similar city names (i.e. 'washington' => in arkansas, illinois, california, etc) number of restaurants.

i've read following cookbook add custom property lack of knowledge in sf , elastica preventing me putting listener in correct place.

let's have city , restaurant type, configure :

city:   mappings:     name: ~     location: { type: geo_point } restaurant:   mappings:     name: ~     location: { type: geo_point } 

i know each city added in index, should geolocate every restaurant nearby (~ 5miles example). how should ?

context :

  • php 5.6
  • symfony 2.3
  • friendsofsymfony/elastica-bundle 3.1.x

i'll respond question if can :

if want geolocate (or other operations) on specific type, need filter :

// assume correct namespaces 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 service 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));         }     } } 

the $container defined in services definition file. example in yaml :

    acme.listener.custom_property:     class: acmebundle\eventlistener\custompropertylistener     arguments: ['@service_container']     tags:         - { name: kernel.event_subscriber } 

it should noted elastic + elastica-bundle poorly documented such complex , critical component. of vital infos scattered through web or in code. hope devs aware of !


Comments

Popular posts from this blog

searchKeyword not working in AngularJS filter -

sequelize.js - Sequelize: sort by enum cases -

user interface - how to replace an ongoing process of image capture from another process call over the same ImageLabel in python's GUI TKinter -