chapter eleven

11 Compound queries

 

This chapter covers

  • Introduction to compound queries
  • Boolean search queries
  • Constant score queries
  • Boosting queries
  • Disjunction maximum queries
  • Function score queries

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

In addition to providing queries for complex criteria, we may sometimes need to boost the scores based on a certain criteria, while at the same time negating the score for negative matches (for example, searching through all books that were launched during a training program may get a positive boost while simultaneously suppressing (negating) books that were pricey). Or maybe we want to set the scores based on custom requirements rather than using Elasticsearch’s inbuilt relevance algorithms.

11.1 Compound queries

11.2 Sample products data

11.2.1 Products schema

11.2.2 Indexing products

11.3 The Boolean (bool) query

11.3.1 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

11.3.6 The should clause

11.3.7 The filter clause

11.3.8 All clauses combined

11.3.9 Named queries

11.4 Constant scores

11.5 The boosting query

11.5.1 Boosting query combined with bool queries

11.6 Disjunction max (dis_max) query

11.7 The function score queries

11.7.1 Random_score function

11.7.2 Script_score function

11.7.3 Field_value_factor function