Class: Ferret::Search::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/ferret/search/query.rb

Overview

The abstract base class for queries. Instantiable subclasses are:

* TermQuery
* MultiTermQuery
* BooleanQuery
* WildcardQuery
* PhraseQuery
* PrefixQuery
* MultiPhraseQuery
* FuzzyQuery
* RangeQuery
* Span::SpanQuery

A parser for queries is contained in:

* Ferret::QueryParser::QueryParser

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeQuery

Returns a new instance of Query.



24
25
26
# File 'lib/ferret/search/query.rb', line 24

def initialize()
  @boost = 1.0
end

Instance Attribute Details

#boostObject

documents matching this query clause will (in addition to the normal weightings) have their score multiplied by the boost factor. It is 1.0 be default.



22
23
24
# File 'lib/ferret/search/query.rb', line 22

def boost
  @boost
end

Instance Method Details

#combine(queries) ⇒ Object

Expert: called when re-writing queries under MultiSearcher.

Only implemented by derived queries, with no #create_weight() implementatation.



67
68
69
70
71
72
73
74
# File 'lib/ferret/search/query.rb', line 67

def combine(queries) 
  queries.each do |query|
    if self != query
      raise ArgumentError
    end
  end
  return self
end

#create_weight(searcher) ⇒ Object

Expert: Constructs an appropriate Weight implementation for this query.

Only implemented by primitive queries, which re-write to themselves.

Raises:

  • (NotImplementedError)


44
45
46
# File 'lib/ferret/search/query.rb', line 44

def create_weight(searcher)
  raise NotImplementedError
end

#extract_terms(terms) ⇒ Object

Expert: adds all terms occuring in this query to the terms set

Raises:

  • (NotImplementedError)


77
78
79
# File 'lib/ferret/search/query.rb', line 77

def extract_terms(terms) 
  raise NotImplementedError
end

#merge_boolean_queries(queries) ⇒ Object

Expert: merges the clauses of a set of BooleanQuery’s into a single BooleanQuery.

A utility for use by #combine() implementations.



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/ferret/search/query.rb', line 86

def merge_boolean_queries(queries) 
  all_clauses = Set.new
  queries.each do |query|
    query.clauses.each do |clause|
      all_clauses << clause
    end
  end

  coord_disabled = queries.size==0 ? false : queries[0].coord_disabled?
  result = BooleanQuery.new(coord_disabled)
  all_clauses.each do |clause|
    result << clause
  end
  return result
end

#rewrite(reader) ⇒ Object

Expert: called to re-write queries into primitive queries.



59
60
61
# File 'lib/ferret/search/query.rb', line 59

def rewrite(reader)
  return self
end

#similarity(searcher) ⇒ Object

Expert: Returns the Similarity implementation to be used for this query. Subclasses may override this method to specify their own Similarity implementation, perhaps one that delegates through that of the Searcher. By default the Searcher’s Similarity implementation is returned.



107
108
109
# File 'lib/ferret/search/query.rb', line 107

def similarity(searcher) 
  return searcher.similarity
end

#to_s(field = nil) ⇒ Object

Prints a query to a string, with field as the default field for terms. The representation used is one that is supposed to be readable by Ferret::QueryParser::QueryParser. However, there are the following limitations:

  • If the query was created by the parser, the printed representation may not be exactly what was parsed. For example, characters that need to be escaped will be represented without the required backslash.

  • Some of the more complicated queries (e.g. span queries) don’t have a representation that can be parsed by QueryParser.

Raises:

  • (NotImplementedError)


37
38
39
# File 'lib/ferret/search/query.rb', line 37

def to_s(field=nil)
  raise NotImplementedError
end

#weight(searcher) ⇒ Object

Expert: Constructs and initializes a Weight for a top-level query.



49
50
51
52
53
54
55
56
# File 'lib/ferret/search/query.rb', line 49

def weight(searcher)
  query = searcher.rewrite(self)
  weight = query.create_weight(searcher)
  sum = weight.sum_of_squared_weights()
  norm = similarity(searcher).query_norm(sum)
  weight.normalize(norm)
  return weight
end