Class: Oedipus::Index

Inherits:
Object
  • Object
show all
Defined in:
lib/oedipus/index.rb

Overview

Representation of a search index for querying.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, conn) ⇒ Index

Initialize the index named name on the connection conn.

Parameters:

  • name (Symbol)

    the name of an existing index in sphinx

  • conn (Connection)

    an instance of Oedipus::Connection for querying



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

#nameObject (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.

Examples:

index.delete(42)

Parameters:

  • id (Integer)

    the unique ID of the document in the index

Returns:

  • (Fixnum)

    the number of rows deleted (currently always 1 or 0)



89
90
91
# File 'lib/oedipus/index.rb', line 89

def delete(id)
  @conn.execute(*@builder.delete(id))
end

#faceted_search(*args) ⇒ Object

Deprecated.

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.

See Also:



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.

Parameters:

  • id (Fixnum)

    the ID of the document

Returns:

  • (Hash)

    the attributes of the record



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.

Examples:

index.insert(42, title: "example", views: 22)

Parameters:

  • id (Integer)

    the unique ID of the document in the index

  • hash (Hash)

    a symbol-keyed hash of data to insert

Returns:

  • (Fixnum)

    the number of rows inserted (currently always 1)



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.

Examples:

index.multi_search(
  cat_results: ["cats", { author_id: 57 }],
  dog_results: ["dogs", { author_id: 57 }]
)

Parameters:

  • queries (Hash)

    a hash whose keys map to queries

Returns:

  • (Hash)

    a Hash whose keys map 1:1 with the input Hash, each element containing the same results as those returned 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, meta = rs.shift, rs.shift
      result[key] = meta_to_hash(meta).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.

Examples:

index.replace(42, title: "New title", views: 25)

Parameters:

  • id (Integer)

    the unique ID of the document in the index

  • hash (Hash)

    a symbol-keyed hash of data to insert

Returns:

  • (Fixnum)

    the number of rows inserted (currentl always 1)



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.

Examples:

Fulltext search

index.search("cats AND dogs")

Fulltext search with attribute filters

index.search("cats AND dogs", author_id: 57)

Attribute search only

index.search(author_id: 57)

Performing a faceted search

index.search(
  "cats | dogs",
  category_id: 7,
  facets: {
    popular: {views:        Oedipus.gt(150)},
    recent:  {published_at: Oedipus.gt(Time.now.to_i - 7 * 86400)}
  }
)

Performing a n-dimensional faceted search

index.search(
  "cats | dogs",
  facets: {
    popular: {
      views: Oedipus.gte(1000),
      facets: {
        in_title: "@title (%{query})"
      }
    }
  }
)

Parameters:

  • query (String)

    a fulltext query

  • options (Hash)

    attribute filters, limits, sorting, facets and other options

  • [Hash] (Hash)

    a customizable set of options

  • [Array] (Hash)

    a customizable set of options

  • [Fixnum] (Hash)

    a customizable set of options

  • [Object] (Hash)

    a customizable set of options

Returns:

  • (Hash)

    a Hash containing meta data, with the records in :records, and if any facets were included, the facets inside the :facets Hash



182
183
184
# File 'lib/oedipus/index.rb', line 182

def search(*args)
  expand_facet_tree(multi_search(deep_merge_facets(args)))
end

#update(id, hash) ⇒ Fixnum

Update the record with the ID id.

Examples:

index.update(42, views: 25)

Parameters:

  • id (Integer)

    the unique ID of the document in the index

  • hash (Hash)

    a symbol-keyed hash of data to set

Returns:

  • (Fixnum)

    the number of rows updated (1 or 0)



58
59
60
# File 'lib/oedipus/index.rb', line 58

def update(id, hash)
  @conn.execute(*@builder.update(id, hash))
end