Class: Xapit::Server::Indexer

Inherits:
Object
  • Object
show all
Defined in:
lib/xapit/server/indexer.rb

Instance Method Summary collapse

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

#databaseObject



8
9
10
# File 'lib/xapit/server/indexer.rb', line 8

def database
  Xapit.database.xapian_database
end

#documentObject



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_termsObject



72
73
74
75
76
# File 'lib/xapit/server/indexer.rb', line 72

def facet_terms
  each_attribute(:facet) do |name, value, options|
    ["F#{Xapit.facet_identifier(name, value)}", 1]
  end
end

#field_termsObject



66
67
68
69
70
# File 'lib/xapit/server/indexer.rb', line 66

def field_terms
  each_attribute(:field) do |name, value, options|
    ["X#{name}-#{parse_field(value)}", 1]
  end
end

#idObject



22
23
24
# File 'lib/xapit/server/indexer.rb', line 22

def id
  "#{@data[:class]}-#{@data[:id]}"
end

#id_termObject



26
27
28
# File 'lib/xapit/server/indexer.rb', line 26

def id_term
  "Q#{id}"
end

#save_facetsObject



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, options|
    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_termsObject



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, options|
      value.to_s.split(/\s+/u).map { |w| w.gsub(/[^\w]/u, "") }.map(&:downcase).map do |term|
        ["Z#{stemmer.call(term)}", options[:weight] || 1]
      end
    end.flatten(1)
  else
    []
  end
end

#termsObject



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_termsObject



46
47
48
49
50
51
52
# File 'lib/xapit/server/indexer.rb', line 46

def text_terms
  each_attribute(:text) do |name, value, options|
    value.to_s.split(/\s+/u).map { |w| w.gsub(/[^\w]/u, "") }.map(&:downcase).map do |term|
      [term, options[:weight] || 1]
    end
  end.flatten(1)
end

#valuesObject



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