Class: Ferret::Search::FilteredQuery

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

Overview

A query that applies a filter to the results of another query.

Note: the bits are retrieved from the filter each time this query is used in a search - use a CachingWrapperFilter to avoid regenerating the bits every time.

Defined Under Namespace

Classes: FilteredScorer, FilteredWeight

Instance Attribute Summary collapse

Attributes inherited from Query

#boost

Instance Method Summary collapse

Methods inherited from Query

#combine, #merge_boolean_queries, #similarity, #weight

Constructor Details

#initialize(query, filter) ⇒ FilteredQuery

Constructs a new query which applies a filter to the results of the original query.

Filter.bits() will be called every time this query is used in a search.

query

Query to be filtered, cannot be nil.

filter

Filter to apply to query results, cannot be nil.



18
19
20
21
22
# File 'lib/ferret/search/filtered_query.rb', line 18

def initialize(query, filter) 
  super()
  @sub_query = query
  @filter = filter
end

Instance Attribute Details

#filterObject (readonly)

Returns the value of attribute filter.



9
10
11
# File 'lib/ferret/search/filtered_query.rb', line 9

def filter
  @filter
end

#sub_queryObject

Returns the value of attribute sub_query.



8
9
10
# File 'lib/ferret/search/filtered_query.rb', line 8

def sub_query
  @sub_query
end

Instance Method Details

#create_weight(searcher) ⇒ Object

Returns a Weight that applies the filter to the enclosed query’s Weight. This is accomplished by overriding the Scorer returned by the Weight.



26
27
28
29
30
# File 'lib/ferret/search/filtered_query.rb', line 26

def create_weight(searcher)
  sub_weight = @sub_query.create_weight(searcher)
  similarity = @sub_query.similarity(searcher)
  return FilteredWeight.new(self, sub_weight, similarity) 
end

#eql?(o) ⇒ Boolean Also known as: ==

Returns true iff o is equal to this.

Returns:

  • (Boolean)


119
120
121
122
# File 'lib/ferret/search/filtered_query.rb', line 119

def eql?(o) 
  return (o.instance_of?(FilteredQuery) and 
    (@sub_query == o.sub_query) and (@filter == o.filter))
end

#extract_terms(terms) ⇒ Object

inherit javadoc



109
110
111
# File 'lib/ferret/search/filtered_query.rb', line 109

def extract_terms(terms) 
  @sub_query.extract_terms(terms)
end

#hashObject

Returns a hash code value for this object.



126
127
128
# File 'lib/ferret/search/filtered_query.rb', line 126

def hash() 
  return @sub_query.hash ^ @filter.hash
end

#rewrite(reader) ⇒ Object

Rewrites the wrapped query.



97
98
99
100
101
102
103
104
105
106
# File 'lib/ferret/search/filtered_query.rb', line 97

def rewrite(reader)
  rewritten = @sub_query.rewrite(reader)
  if (rewritten != @sub_query) 
    clone = self.clone()
    clone.query = rewritten
    return clone
  else 
    return self
  end
end

#to_s(f = nil) ⇒ Object

Prints a user-readable version of this query.



114
115
116
# File 'lib/ferret/search/filtered_query.rb', line 114

def to_s(f = nil) 
  return "filtered(#{@sub_query.to_s(f)})->#{@filter}"
end