Class: Xapit::Server::Indexer
- Inherits:
-
Object
- Object
- Xapit::Server::Indexer
- Defined in:
- lib/xapit/server/indexer.rb
Instance Method Summary collapse
- #database ⇒ Object
- #document ⇒ Object
- #facet_terms ⇒ Object
- #field_terms ⇒ Object
- #id ⇒ Object
- #id_term ⇒ Object
-
#initialize(data) ⇒ Indexer
constructor
A new instance of Indexer.
- #save_facets ⇒ Object
- #stemmed_text_terms ⇒ Object
- #terms ⇒ Object
- #text_terms ⇒ Object
- #values ⇒ Object
Constructor Details
#initialize(data) ⇒ Indexer
Returns a new instance of Indexer.
4 5 6 |
# File 'lib/xapit/server/indexer.rb', line 4 def initialize(data) @data = data end |
Instance Method Details
#database ⇒ Object
8 9 10 |
# File 'lib/xapit/server/indexer.rb', line 8 def database Xapit.database.xapian_database end |
#document ⇒ Object
12 13 14 15 16 17 18 19 20 |
# File 'lib/xapit/server/indexer.rb', line 12 def document document = Xapian::Document.new document.data = id terms.each { |term, weight| document.add_term(term, weight) } text_terms.each { |term, weight| database.add_spelling(term, weight) } if Xapit.config[:spelling] values.each { |index, value| document.add_value(index, value) } save_facets document end |
#facet_terms ⇒ Object
72 73 74 75 76 |
# File 'lib/xapit/server/indexer.rb', line 72 def facet_terms each_attribute(:facet) do |name, value, | ["F#{Xapit.facet_identifier(name, value)}", 1] end end |
#field_terms ⇒ Object
66 67 68 69 70 |
# File 'lib/xapit/server/indexer.rb', line 66 def field_terms each_attribute(:field) do |name, value, | ["X#{name}-#{parse_field(value)}", 1] end end |
#id ⇒ Object
22 23 24 |
# File 'lib/xapit/server/indexer.rb', line 22 def id "#{@data[:class]}-#{@data[:id]}" end |
#id_term ⇒ Object
26 27 28 |
# File 'lib/xapit/server/indexer.rb', line 26 def id_term "Q#{id}" end |
#save_facets ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/xapit/server/indexer.rb', line 78 def save_facets each_attribute(:facet) do |name, value, | id = Xapit.facet_identifier(name, value) unless database.term_exists("Xid-#{id}") document = Xapian::Document.new document.data = "#{name}|||#{value}" document.add_term("CFacetOption") document.add_term("Xid-#{id}") database.add_document(document) end end end |
#stemmed_text_terms ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/xapit/server/indexer.rb', line 54 def stemmed_text_terms if stemmer each_attribute(:text) do |name, value, | value.to_s.split(/\s+/u).map { |w| w.gsub(/[^\w]/u, "") }.map(&:downcase).map do |term| ["Z#{stemmer.call(term)}", [:weight] || 1] end end.flatten(1) else [] end end |
#terms ⇒ Object
30 31 32 |
# File 'lib/xapit/server/indexer.rb', line 30 def terms base_terms + text_terms + stemmed_text_terms + field_terms + facet_terms end |
#text_terms ⇒ Object
46 47 48 49 50 51 52 |
# File 'lib/xapit/server/indexer.rb', line 46 def text_terms each_attribute(:text) do |name, value, | value.to_s.split(/\s+/u).map { |w| w.gsub(/[^\w]/u, "") }.map(&:downcase).map do |term| [term, [:weight] || 1] end end.flatten(1) end |
#values ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/xapit/server/indexer.rb', line 34 def values values = {} each_value do |index, value| if values[index] values[index] += "\3#{value}" # multiple values are split back out on the query side else values[index] = value end end values end |