11 Compound queries

 

This chapter covers

  • Working with compound queries
  • Boolean search queries
  • Constant score queries
  • Boosting queries
  • Disjunction maximum queries
  • Function score queries

In the last two chapters, we looked at term-level and full-text queries. We discussed searching structured and unstructured data using queries, some producing relevance scores and others working in a filter context where scores are irrelevant. Most queries allow setting simple search criteria and working on a limited set of fields, such as finding books written by an author or searching for best-selling books.

In addition to providing queries for complex criteria, we sometimes need to boost scores based on certain criteria while at the same time negating scores for negative matches (for example, all books launched during a training program may get a positive boost while simultaneously, expensive books are suppressed [negated]). Or maybe we want to set scores based on custom requirements rather than using Elasticsearch’s built-in relevance algorithms.

11.1 Sample product data

11.1.1 The products schema

11.1.2 Indexing products

11.2 Compound queries

11.3 The Boolean (bool) query

11.3.1 The bool query structure

11.3.2 The must clause

11.3.3 Enhancing the must clause

11.3.4 The must_not clause

11.3.5 Enhancing the must_not clause