Class: Xapit::IndexBlueprint
- Inherits:
-
Object
- Object
- Xapit::IndexBlueprint
- Defined in:
- lib/xapit/index_blueprint.rb
Overview
This is the object used in the block of the xapit method in Xapit::Membership. It keeps track of the index settings for a given class. It also provides some indexing functionality.
Instance Attribute Summary collapse
-
#facets ⇒ Object
readonly
Returns the value of attribute facets.
-
#field_attributes ⇒ Object
readonly
Returns the value of attribute field_attributes.
-
#sortable_attributes ⇒ Object
readonly
Returns the value of attribute sortable_attributes.
-
#text_attributes ⇒ Object
readonly
Returns the value of attribute text_attributes.
Class Method Summary collapse
-
.index_all ⇒ Object
Indexes all classes known to have an index blueprint defined.
Instance Method Summary collapse
-
#create_record(member_id) ⇒ Object
Add a single record to the index if it matches the xapit options.
-
#destroy_record(member_id) ⇒ Object
Remove a single record from the index.
-
#facet(*args, &block) ⇒ Object
Adds a facet attribute.
-
#field(*attributes) ⇒ Object
Adds a field attribute.
-
#index_all ⇒ Object
Indexes all records of this blueprint class.
-
#initialize(member_class, *args) ⇒ IndexBlueprint
constructor
A new instance of IndexBlueprint.
-
#position_of_field(field_attribute) ⇒ Object
The Xapian value index position of a field attribute.
-
#position_of_sortable(sortable_attribute) ⇒ Object
The Xapian value index position of a sortable attribute.
-
#sortable(*attributes) ⇒ Object
Adds a sortable attribute for use with the :order option in a search call.
-
#text(*attributes, &proc) ⇒ Object
Adds a text attribute.
-
#update_record(member_id) ⇒ Object
Update a single record in the index.
Constructor Details
#initialize(member_class, *args) ⇒ IndexBlueprint
Returns a new instance of IndexBlueprint.
19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/xapit/index_blueprint.rb', line 19 def initialize(member_class, *args) @member_class = member_class @args = args @text_attributes = {} @field_attributes = [] @sortable_attributes = [] @facets = [] @@instances ||= {} @@instances[member_class] = self # TODO make this thread safe @indexer = SimpleIndexer.new(self) end |
Instance Attribute Details
#facets ⇒ Object (readonly)
Returns the value of attribute facets.
8 9 10 |
# File 'lib/xapit/index_blueprint.rb', line 8 def facets @facets end |
#field_attributes ⇒ Object (readonly)
Returns the value of attribute field_attributes.
6 7 8 |
# File 'lib/xapit/index_blueprint.rb', line 6 def field_attributes @field_attributes end |
#sortable_attributes ⇒ Object (readonly)
Returns the value of attribute sortable_attributes.
7 8 9 |
# File 'lib/xapit/index_blueprint.rb', line 7 def sortable_attributes @sortable_attributes end |
#text_attributes ⇒ Object (readonly)
Returns the value of attribute text_attributes.
5 6 7 |
# File 'lib/xapit/index_blueprint.rb', line 5 def text_attributes @text_attributes end |
Class Method Details
.index_all ⇒ Object
Indexes all classes known to have an index blueprint defined.
11 12 13 14 15 16 17 |
# File 'lib/xapit/index_blueprint.rb', line 11 def self.index_all load_models @@instances.each do |member_class, blueprint| yield(member_class) if block_given? blueprint.index_all end end |
Instance Method Details
#create_record(member_id) ⇒ Object
Add a single record to the index if it matches the xapit options.
97 98 99 100 |
# File 'lib/xapit/index_blueprint.rb', line 97 def create_record(member_id) member = @member_class.xapit_adapter.find_single(member_id, *@args) @indexer.add_member(member) if member end |
#destroy_record(member_id) ⇒ Object
Remove a single record from the index.
114 115 116 |
# File 'lib/xapit/index_blueprint.rb', line 114 def destroy_record(member_id) Xapit::Config.writable_database.delete_document("Q#{@member_class}-#{member_id}") end |
#facet(*args, &block) ⇒ Object
Adds a facet attribute. See Xapit::FacetBlueprint and Xapit::Facet for details.
65 66 67 |
# File 'lib/xapit/index_blueprint.rb', line 65 def facet(*args, &block) @facets << FacetBlueprint.new(@member_class, @facets.size, *args, &block) end |
#field(*attributes) ⇒ Object
Adds a field attribute. Field terms are not split by word so it is not designed for full text search. Instead you can filter through a field using the :conditions hash in a search query.
Article.search("", :conditions => { :priority => 5 })
Multiple field values are supported if the given attribute is an array.
def priority
[3, 5] # will match priority search for 3 or 5
end
60 61 62 |
# File 'lib/xapit/index_blueprint.rb', line 60 def field(*attributes) @field_attributes += attributes end |
#index_all ⇒ Object
Indexes all records of this blueprint class. It does this using the “.find_each” method on the member class. You will likely want to call Xapit.remove_database before this.
76 77 78 79 80 |
# File 'lib/xapit/index_blueprint.rb', line 76 def index_all @member_class.xapit_adapter.find_each(*@args) do |member| @indexer.add_member(member) end end |
#position_of_field(field_attribute) ⇒ Object
The Xapian value index position of a field attribute
90 91 92 93 94 |
# File 'lib/xapit/index_blueprint.rb', line 90 def position_of_field(field_attribute) index = field_attributes.map(&:to_s).index(field_attribute.to_s) raise "Unable to find indexed field attribute \"#{field_attribute}\" in #{@member_class} field attributes: #{field_attributes.inspect}" if index.nil? index + facets.size + sortable_attributes.size end |
#position_of_sortable(sortable_attribute) ⇒ Object
The Xapian value index position of a sortable attribute
83 84 85 86 87 |
# File 'lib/xapit/index_blueprint.rb', line 83 def position_of_sortable(sortable_attribute) index = sortable_attributes.map(&:to_s).index(sortable_attribute.to_s) raise "Unable to find indexed sortable attribute \"#{sortable_attribute}\" in #{@member_class} sortable attributes: #{sortable_attributes.inspect}" if index.nil? index + facets.size end |
#sortable(*attributes) ⇒ Object
Adds a sortable attribute for use with the :order option in a search call.
70 71 72 |
# File 'lib/xapit/index_blueprint.rb', line 70 def sortable(*attributes) @sortable_attributes += attributes end |
#text(*attributes, &proc) ⇒ Object
Adds a text attribute. Each word in the text will be indexed as a separate term allowing full text searching. Text terms are what is searched by the primary string in a search query.
Article.search("kite")
You can specify a :weight option to give a text attribute more importance. This will cause search terms matching that attribute to have a higher rank. The default weight is 1. Decimal (0.5) weight values are not supported.
index.text :name, :weight => 10
41 42 43 44 45 46 47 |
# File 'lib/xapit/index_blueprint.rb', line 41 def text(*attributes, &proc) = attributes. [:proc] ||= proc attributes.each do |attribute| @text_attributes[attribute] = end end |
#update_record(member_id) ⇒ Object
Update a single record in the index. If the record does not match the xapit conditions then it is removed from the index instead.
104 105 106 107 108 109 110 111 |
# File 'lib/xapit/index_blueprint.rb', line 104 def update_record(member_id) member = @member_class.xapit_adapter.find_single(member_id, *@args) if member @indexer.update_member(member) else destroy_record(member_id) end end |