Class: Oedipus::Index
- Inherits:
-
Object
- Object
- Oedipus::Index
- Defined in:
- lib/oedipus/index.rb
Overview
Representation of a search index for querying.
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
-
#delete(id) ⇒ Fixnum
Delete the record with the ID
id
. - #faceted_search(*args) ⇒ Object deprecated Deprecated.
-
#fetch(id) ⇒ Hash
Fetch a single document by its ID.
-
#initialize(name, conn) ⇒ Index
constructor
Initialize the index named
name
on the connectionconn
. -
#insert(id, hash) ⇒ Fixnum
Insert the record with the ID
id
. -
#multi_search(queries) ⇒ Hash
Perform a a batch search on the index.
-
#replace(id, hash) ⇒ Fixnum
Completely replace the record with the ID
id
. -
#search(*args) ⇒ Hash
Perform a search on the index.
-
#update(id, hash) ⇒ Fixnum
Update the record with the ID
id
.
Constructor Details
#initialize(name, conn) ⇒ Index
Initialize the index named name
on the connection conn
.
22 23 24 25 26 |
# File 'lib/oedipus/index.rb', line 22 def initialize(name, conn) @name = name.to_sym @conn = conn @builder = QueryBuilder.new(name) end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
13 14 15 |
# File 'lib/oedipus/index.rb', line 13 def name @name end |
Instance Method Details
#delete(id) ⇒ Fixnum
Delete the record with the ID id
.
89 90 91 |
# File 'lib/oedipus/index.rb', line 89 def delete(id) @conn.execute(*@builder.delete(id)) end |
#faceted_search(*args) ⇒ Object
Perform a faceted search on the index, using a base query and one or more facets.
This method is deprecated and will be removed in version 1.0. Use #search instead.
193 194 195 |
# File 'lib/oedipus/index.rb', line 193 def faceted_search(*args) search(*args) end |
#fetch(id) ⇒ Hash
Fetch a single document by its ID.
Returns the Hash of attributes if found, otherwise nil.
102 103 104 |
# File 'lib/oedipus/index.rb', line 102 def fetch(id) search(id: id)[:records].first end |
#insert(id, hash) ⇒ Fixnum
Insert the record with the ID id
.
41 42 43 |
# File 'lib/oedipus/index.rb', line 41 def insert(id, hash) @conn.execute(*@builder.insert(id, hash)) end |
#multi_search(queries) ⇒ Hash
Perform a a batch search on the index.
A Hash of queries is passed, whose keys are used to collate the results in the return value.
Each query may either by a string (fulltext search), a Hash (attribute search) or an array containing both. In other words, the same arguments accepted by the #search method.
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
# File 'lib/oedipus/index.rb', line 218 def multi_search(queries) unless queries.kind_of?(Hash) raise ArgumentError, "Argument must be a Hash of named queries (#{queries.class} given)" end stmts = [] bind_values = [] queries.each do |key, args| str, *values = @builder.select(*extract_query_data(args)) stmts.push(str, "SHOW META") bind_values.push(*values) end rs = @conn.multi_query(stmts.join(";\n"), *bind_values) Hash[].tap do |result| queries.keys.each do |key| records, = rs.shift, rs.shift result[key] = ().tap do |r| r[:records] = records.map { |hash| hash.inject({}) { |o, (k, v)| o.merge!(k.to_sym => v) } } end end end end |
#replace(id, hash) ⇒ Fixnum
Completely replace the record with the ID id
.
75 76 77 |
# File 'lib/oedipus/index.rb', line 75 def replace(id, hash) @conn.execute(*@builder.replace(id, hash)) end |
#search(*args) ⇒ Hash
Perform a search on the index.
Either one or two arguments may be passed, with either one being mutually optional.
When performing a faceted search, the base query is inherited by each facet, which may override (or refine) the query.
The results returned include a :facets key, containing the results for each facet.
To perform an n-dimensional faceted search, add a :facets option to each facet. Each facet will inherit from its immediate parent, which inerits from its parent, up to the root query.
The results in a n-dimensional faceted search are returned with each set of facet results in turn containing a :facets element.
182 183 184 |
# File 'lib/oedipus/index.rb', line 182 def search(*args) (multi_search(deep_merge_facets(args))) end |
#update(id, hash) ⇒ Fixnum
Update the record with the ID id
.
58 59 60 |
# File 'lib/oedipus/index.rb', line 58 def update(id, hash) @conn.execute(*@builder.update(id, hash)) end |