Search code examples
symfonyelasticsearchfilteringfaceted-searchtechnical-debt

Elastic Search with Symfony2 + Aggregation [Complex]


Facet Filter Demo

I'm creating a search page with Elastic search + Symfony2 using FOSElastica. I have setup repository and model transform and pagination perfectly fine.

I have geo-location search with couple of aggregation which are following

Distance Range

  • < 5 miles
  • 25 miles
  • 50 miles
  • 100 miles

Duration between two points

  • 30 minutes
  • 1 hour
  • 2 hour
  • 3 hour

Categories

  • Car
  • Truck
  • Bike
  • Hover board

location: geo point (default is set by country)

Question

There will be multiple aggregation click by user to filter their data to narrow the data. What would be the best way to track which aggregation is active by the user?

And how can I show send and receive filter aggregation between controller and service methods?

Would it be best to just track everything by session?


Solution

  • Since the default Elasticsearch response doc does not contain the query that was submitted, you'll need to keep track of the filter selection state in a MVC application in order to properly render your filter controls.

    Some options:

    • filters encoded into form elements (possibly hidden) on the page. Form could post via Ajax.
    • filters encoded into url (added benefit of bookmarkable/shareable searches)
    • filters stored in session (not sure why you'd want this, but it would be possible)

    Once you've submitted a search request, your controller which calls Elasticsearch, is now responsible for passing the user's filter selection state to Elastica (and also back to the view along with search results)


    This sort of echoing filter state back from the search controller is useful especially when you need to redraw the section of the page housing search filters.