How do I effectively combine query statements?

(Luhn) #1

I'm using Elasticsearch to perform what I assume is a fairly standard search on a database of restaurants.

First, I want to use one or more of the following criteria to determine if a document should be included in the search results at all:

  • Full text search match or partial match
  • Within n miles of the given coordinates.
  • Classified as the given cuisine.
  • Not marked as deleted
  • Mark as verified
  • Within the given price range (prices ranked from 1 to 4)

And then I want to take the documents satisfying the criteria and rank them by how well they satisfy the following criteria:

  • Not marked as closed.
  • Accepts reservations.
  • Best if within n miles of the given coordinates, then decreasing rank as we move outward.
  • How well the full text search matches.

Perhaps each of these would have a multiplier that I could play around with to find the right mixture.

Now, each one of these criterion are easy enough to do independently. Elasticsearch has a wide variety of queries and filters that can achieve every criterion I want to do. The problem arises when I try to combine them to construct a query. For example, here's one of the godawful queries that I end up with. It's completely unintelligible and unmaintainable.

I must be missing something here—something I don't grok. How can I combine these criteria into a sane query? Are there any resources on how to achieve things like this?

(David Pilato) #2

I'd use a bool query with should clauses.
And probably compute my own score using function_score.

(system) #3