Class: CLConsole

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, opts = {}) ⇒ CLConsole

Returns a new instance of CLConsole.



8
9
10
11
12
13
14
15
16
17
# File 'lib/cl_console.rb', line 8

def initialize(url, opts={})
  @cl_url = if url.start_with? "http"
              url
            else
              parts = url.split('/')
              "http://localhost:5984/#{parts[0]}/_fti/_design/#{parts[1]}/#{parts[2]}"
            end

  @options = opts
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



6
7
8
# File 'lib/cl_console.rb', line 6

def options
  @options
end

Class Method Details

.helpObject

display help message



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/cl_console.rb', line 57

def self.help
  puts <<-HELP_MSG
------------------------------------------------------------------------------
 CL-Console Help
==============================================================================
Example usage:

# initialize with "database/designdocument/index" (ddoc without _design)
cl = CLConsole.new("db/ddoc/index")

# or initialize with a full url
cl = CLConsole.new("http://localhost:5984/db/_fti/_design/ddoc/index")

# you can also access couchdb-lucene direct:
cl = CLConsole.new("http://localhost:5985/local/dewiki/_design/wiki/all")

# You can also pass a hash with options to be passed to couchdb-lucene, like
# limit, sorting, debug, ... see the couchdb-lucene documentation for details.

# query

cl.q "simple"   # issue a simple query and give the raw result from c-l
cl.qr "simple"  # same query but only the result rows
cl.qd "simple"  # and this time only return the documents

# do a more sophisticated request and let cl-console escape special chars
cl.qd "title:(couchdb OR lucene) AND body:(awesome AND \#{cl.escape(':-)')})"

# meta

cl.info         # get index infos
cl.help         # this info
cl.e "~^"       # helper: proper escaping of lucene query operators
=> \~\^
------------------------------------------------------------------------------
  HELP_MSG
end

Instance Method Details

#escape(str) ⇒ Object Also known as: e

helper to escape searchterms properly for lucene search queries



50
51
52
53
# File 'lib/cl_console.rb', line 50

def escape(str)
  lucene_chars = %w{+ - & | ! ( ) { } [ ] ^ " ~ * ? : \\}.collect {|e| Regexp.escape(e)}
  str.gsub(/(#{lucene_chars.join("|")})/, '\\\\\1')
end

#helpObject



94
# File 'lib/cl_console.rb', line 94

def help; CLConsole.help; end

#infoObject

fetch index infos



44
45
46
47
# File 'lib/cl_console.rb', line 44

def info
  puts "query: " << @cl_url if options[:debug]
  JSON.parse(open("#{@cl_url}").string)
end

#q(query, opts = {}) ⇒ Object

do the actual query



20
21
22
23
24
25
26
27
28
29
# File 'lib/cl_console.rb', line 20

def q(query, opts={})
  opts = options.merge(opts).merge({:q => query})
  url = "#{@cl_url}?#{opts.to_param}"
  puts "query: " << url if opts[:debug]
  res = JSON.parse(open(url).string)
  puts ">> q: #{res["q"]} (#{res["search_duration"]}ms / #{res["fetch_duration"]}ms)"
  res
rescue Exception => e
  puts "An exception occurred: #{e.message}"
end

#qd(query, opts = {}) ⇒ Object

do the query, ensure that cl fetches the documents and return only the documents



37
38
39
40
41
# File 'lib/cl_console.rb', line 37

def qd(query, opts={})
  qr(query,{:include_docs => true}.merge(opts)).inject([]) do |acc,row|
    acc << row["doc"]
  end
end

#qr(query, opts = {}) ⇒ Object

do the query, and only give me the rows



32
33
34
# File 'lib/cl_console.rb', line 32

def qr(query, opts={})
  q(query,opts)["rows"]
end