Class: Ferret::Search::QueryFilter

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

Overview

Constrains search results to only match those which also match a provided query. Results are cached, so that searches after the first on the same index using this filter are much faster.

This could be used, for example, with a RangeQuery on a suitably formatted date field to implement date filtering. One could re-use a single QueryFilter that matches, e.g., only documents modified within the last week. The QueryFilter and RangeQuery would only need to be reconstructed once per day.

Instance Method Summary collapse

Constructor Details

#initialize(query) ⇒ QueryFilter

Constructs a filter which only matches documents matching query.



16
17
18
19
# File 'lib/ferret/search/query_filter.rb', line 16

def initialize(query) 
  @query = query
  @cache = nil 
end

Instance Method Details

#bits(reader) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/ferret/search/query_filter.rb', line 21

def bits(reader)

  if (@cache == nil) 
    @cache = Ferret::Utils::WeakKeyHash.new
  end

  @cache.synchronize() do # check cache
    bits = @cache[reader]
    if bits
      return bits
    end
  end

  bits = Ferret::Utils::BitVector.new()

  IndexSearcher.new(reader).search_each(@query) do |doc, score|
    bits.set(doc)  # set bit for hit
  end

  @cache.synchronize() do # update cache
    @cache[reader] = bits
  end

  return bits
end

#to_sObject



47
48
49
# File 'lib/ferret/search/query_filter.rb', line 47

def to_s() 
  return "QueryFilter(#{@query})"
end