Class: ElasticGraph::GraphQL::Filtering::BooleanQuery

Inherits:
Object
  • Object
show all
Defined in:
lib/elastic_graph/graphql/filtering/boolean_query.rb

Overview

BooleanQuery is an internal class for composing a datastore query: www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html

It is composed of:

1) The occurrence type (:filter, :should, or :must_not)
2) A list of query clauses evaluated by the given occurrence type
3) An optional flag indicating whether the occurrence should be negated

Note: since we never do anything with the score, we always prefer ‘filter` over `must`. If we ever decide to do something with the score (such as sorting by it), then we’ll want to introduce ‘must`.

Constant Summary collapse

ALWAYS_FALSE_FILTER =
filter({match_none: {}})

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.filter(*clauses) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/elastic_graph/graphql/filtering/boolean_query.rb', line 23

def self.filter(*clauses)
  unwrapped_clauses = clauses.map do |clause|
    __skip__ = case clause
    in {bool: {minimum_should_match: 1, should: [::Hash => single_should], **nil}, **nil}
      # This case represents an `anyOf` with a single subfilter (`filter: {anyOf: [X]}`).
      # Such an expression is semantically equivalent to `filter: X`, and we can unwrap the
      # should clause in this case since there is only a single one.
      #
      # While it adds a bit of complexity to do this unwrapping, we believe it's worth it because
      # it preserves the datastore's ability to apply caching. As the Elasticsearch documentation[^1]
      # explains, the results of `filter` clauses can be cached, but not `should` clauses.
      #
      # [^1]: https://www.elastic.co/docs/reference/query-languages/query-dsl/query-dsl-bool-query
      single_should
    else
      clause
    end
  end

  new(:filter, unwrapped_clauses)
end

.should(*clauses) ⇒ Object



45
46
47
# File 'lib/elastic_graph/graphql/filtering/boolean_query.rb', line 45

def self.should(*clauses)
  new(:should, clauses)
end

Instance Method Details

#merge_into(bool_node) ⇒ Object



49
50
51
# File 'lib/elastic_graph/graphql/filtering/boolean_query.rb', line 49

def merge_into(bool_node)
  bool_node[occurrence].concat(clauses)
end