Class: CLConsole
- Inherits:
-
Object
- Object
- CLConsole
- Defined in:
- lib/cl_console.rb
Instance Attribute Summary collapse
-
#options ⇒ Object
Returns the value of attribute options.
Class Method Summary collapse
-
.help ⇒ Object
display help message.
Instance Method Summary collapse
-
#escape(str) ⇒ Object
(also: #e)
helper to escape searchterms properly for lucene search queries.
- #help ⇒ Object
-
#info ⇒ Object
fetch index infos.
-
#initialize(url, opts = {}) ⇒ CLConsole
constructor
A new instance of CLConsole.
-
#q(query, opts = {}) ⇒ Object
do the actual query.
-
#qd(query, opts = {}) ⇒ Object
do the query, ensure that cl fetches the documents and return only the documents.
-
#qr(query, opts = {}) ⇒ Object
do the query, and only give me the rows.
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
#options ⇒ Object
Returns the value of attribute options.
6 7 8 |
# File 'lib/cl_console.rb', line 6 def @options end |
Class Method Details
.help ⇒ Object
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 |
#info ⇒ Object
fetch index infos
44 45 46 47 |
# File 'lib/cl_console.rb', line 44 def info puts "query: " << @cl_url if [: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 = .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.}" 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 |