Skip to main content

Compound queries

Compound queries1 wrap other queries to either:

  • Combine their results
  • Change their behavior
  • Switch query contexts (in particular, from query to filter context)

In other words, you can use compound queries to combine any number of term-level and full-text queries (and in fact other compound queries as well).

Details

Below are the various kinds of compound queries. These are sorted with the most commonly used at the top, and cover their usual usage. Each one is linked to Elasticsearch's own documentation to provide greater details. (In most cases there are many more options for each kind of query than what's documented here.)

Bool

Bool queries combine multiple queries using must, should, must_not and filter clauses. These allow you to combine queries with logic like AND, OR and NOT.

ClauseDescriptionContext
mustQuery clauses must match the results, and will contribute to the score. These act like a logical AND operation.query
shouldQuery clauses should match the results, and will contribute to the score. These act like a logical OR operation.query
must_notQuery clauses must not match the results, and are used to either include or exclude results (no scoring). These act like a logical NOT operation.filter
filterQuery clauses must match the results, but will not contribute to the score.filter
Fluent search uses filter context exclusively

Note that the where clause in fluent search is actually translated to a filter clause, and not a must clause. Therefore, fluent search exclusively uses filter context. If you truly want to use query context you will need to construct your queries using lower-level Elastic queries rather than fluent search's clauses.

For example, this query would find all active (non-archived) tables with either a classification or a term assigned:

Build the query and request
IndexSearchRequest index = Table.select(client) // (1)
.whereSome(Asset.ATLAN_TAGS.hasAnyValue()) // (2)
.whereSome(Asset.ASSIGNED_TERMS.hasAnyValue())
.minSomes(1) // (3)
.toRequest(); // (4)
  1. You can build up a compound query progressively, starting from the type of asset you want to query using the select() method. This will start a query that narrows results to only active assets of this type (Table in this example). Because this operation may retrieve information from Atlan, you must provide it an AtlanClient through which to connect to the tenant.
  2. You can add any number of conditions where some of them must match using the whereSome() helper with a query as a condition. (You can add any number of mandatory conditions using the where() helper with a query as a condition, instead.) Each query you provide can either be from a helper (like these examples) or a full-fledged Elastic Query, if you need ultimate flexibility.
  3. You can specify how many of these whereSome() conditions must match using the minSomes() helper.
  4. Finally, you can build the compound query into a search request using the toRequest() helper method.

Footnotes

  1. This page is a summary of the details in the Elasticsearch Guide's Compound queries

Was this page helpful?