Class: ActsAsIndexed::SearchIndex

Inherits:
Object
  • Object
show all
Defined in:
lib/acts_as_indexed/search_index.rb

Instance Method Summary collapse

Constructor Details

#initialize(fields, config) ⇒ SearchIndex

fields

Fields or instance methods of ActiveRecord model to be indexed.

config

ActsAsIndexed::Configuration instance.



6
7
8
9
10
11
12
13
14
# File 'lib/acts_as_indexed/search_index.rb', line 6

def initialize(fields, config)
  @storage = Storage.new(config)
  @fields = fields
  @atoms = ActiveSupport::OrderedHash.new
  @min_word_size = config.min_word_size
  @records_size = @storage.record_count
  @case_sensitive = config.case_sensitive
  @if_proc = config.if_proc
end

Instance Method Details

#add_record(record) ⇒ Object

Adds record to the index.



17
18
19
20
21
22
23
24
# File 'lib/acts_as_indexed/search_index.rb', line 17

def add_record(record)
  return unless allow_indexing?(record)

  condensed_record = condense_record(record)
  atoms = add_occurences(condensed_record, record.id)

  @storage.add(atoms)
end

#add_records(records) ⇒ Object

Adds multiple records to the index. Accepts an array of records.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/acts_as_indexed/search_index.rb', line 27

def add_records(records)
  atoms = ActiveSupport::OrderedHash.new
  records_count = 0

  records.each do |record|
    next unless allow_indexing?(record)
    records_count += 1

    condensed_record = condense_record(record)
    atoms = add_occurences(condensed_record, record.id, atoms)
  end

  @storage.add(atoms, records_count)
end

#remove_record(record) ⇒ Object

Removes record from the index.



43
44
45
46
47
48
# File 'lib/acts_as_indexed/search_index.rb', line 43

def remove_record(record)
  condensed_record = condense_record(record)
  atoms = add_occurences(condensed_record,record.id)

  @storage.remove(atoms)
end

#search(query) ⇒ Object

Returns an array of IDs for records matching query.



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/acts_as_indexed/search_index.rb', line 63

def search(query)
  return [] if query.nil?

  @atoms = @storage.fetch(cleanup_atoms(query), query[/\^/])
  queries = parse_query(query.dup)
  positive = run_queries(queries[:positive])
  positive_quoted = run_quoted_queries(queries[:positive_quoted])
  negative = run_queries(queries[:negative])
  negative_quoted = run_quoted_queries(queries[:negative_quoted])
  starts_with = run_queries(queries[:starts_with], true)
  start_quoted = run_quoted_queries(queries[:start_quoted], true)

  results = ActiveSupport::OrderedHash.new

  if queries[:start_quoted].any?
    results = merge_query_results(results, start_quoted)
  end

  if queries[:starts_with].any?
    results = merge_query_results(results, starts_with)
  end

  if queries[:positive_quoted].any?
    results = merge_query_results(results, positive_quoted)
  end

  if queries[:positive].any?
    results = merge_query_results(results, positive)
  end

  negative_results = (negative.keys + negative_quoted.keys)
  results.delete_if { |r_id, w| negative_results.include?(r_id) }
  results
end

#update_record(record_new, record_old) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/acts_as_indexed/search_index.rb', line 50

def update_record(record_new, record_old)
  if !record_unchanged?(record_new, record_old)
    remove_record(record_old)
    add_record(record_new)

  # Always try to remove the record if it is non-indexable, in case proc
  # makes use of any methods or attributes exteral of the record.
  elsif !allow_indexing?(record_new)
    remove_record(record_old)
  end
end