Git Product home page Git Product logo

dm-is-searchable's Introduction

dm-is-searchable

Description

A DataMapper search plugin api to search for resources from one repository and load from another.

Typically a full text search adapter that can only produce partial resources is searched and the resulting resource collection is then loaded from your default repository.

Synopsis

Resources

require "rubygems"
require "dm-core"
require "dm-is-searchable"

DataMapper.setup(:default, :adapter => 'in_memory')

# Your search adapter configuration. See your search adapters setup options.
DataMapper.setup(:search, :adapter => 'your_adapter')

class Cow
  include DataMapper::Resource
  property :name, String, :key => true
  property :likes, String
  property :cowpats, Integer

  is :searchabe # This defaults to repository(:search), you could also do
  # is :searchable, :repository => :some_searchable_repository
end

class Chicken
  include DataMapper::Resource
  property :name,  String, :key => true
  property :likes, String
  property :eggs, Integer

  is :searchable
  repository(:search) do
    # We only want to be able to search by name.
    properties(:search).clear
    property :name, String
  end
end

Searching

Mixing in is-searchable by default defines a single class method in your resource with the signature of:

# ==== Parameters
# search_options<Hash>::
#   DM::Query conditions to pass to the searchable repository. Unless you
#   explicitly defined a searchable repository this is repository(:search).
#
# options<Hash>::
#   Optionsal DM::Query conditions to pass to the repository holding the full
#   resource. Without a scoped search this is repository(:default).
#
# ==== Returns
# DM::Collection:: Zero or more DM::Resource objects.
MyModel#search(search_options = {}, options = {})

A basic full text search for cows called ‘Pete’, ‘Peter’, ‘Pedro’ (dependant on the search adapter) would look like:

puts Cow.search(:name => 'pete').inspect
#=> [<Cow name="peter" cowpats="12" ...>, <Cow name='pete' cowpats="1024" ...>, ...]

Adding extra conditions to apply to the default repository allows you to do interesting things to the search adapter results. For example conditions on properties not available in your search adapter or unsupported operators:

# Unsupported #gt operator in search adapter.
puts Cow.search({:name => 'pete'}, {:cowpats.gt => 1000}).inspect
#=> [<Cow name="pete" cowpats="1024" ...>]

# Unknown property in search adapter.
puts Chicken.search({:name => 'steve'}, {:eggs => (100..200)}).inspect
#=> [<Chicken name="steve" eggs="120" ...>]

Adapter

Like all DM adapters a custom search adapter implements the DM::AbstractAdapter interface.

The key differences from a typical adapter are:

DM::AbstractAdapter#read_many

An Array of Hashes in the form of [{:id => 12}, {:id => 53}] will work just fine. In fact so long as the following snippet would run on the returned value you are free to return whatever you like.

ids = read_many_result.collect{|doc| doc[:id]} #=> Array of ID's.

DM::AbstractAdapter#read_one

No need to DM::Model#load here. Just return your partial resource as a Hash in the form of {:id => 12}. Like #read_many the returned value really only needs to respond to #[:id].

read_one_result[:id] #=> The resource ID to load.

Compatible Search Adapters

Ferret

Gem

dm-ferret-adapter

Git

git://github.com/datamapper/dm-ferret-adapter.git

Sphinx

(not currently longer maintained)

Gem

dm-sphinx-adapter

Git

git://github.com/shanna/dm-sphinx-adapter.git

dm-is-searchable's People

Contributors

dkubb avatar snusnu avatar bernerdschaefer avatar myabc avatar namelessjon avatar michaelklishin avatar solnic avatar somebee avatar paul avatar sholton311 avatar shanna avatar

Watchers

Jacob Basham avatar The Killswitch Collective avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.