cl-console 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/LICENSE +20 -0
  2. data/README.md +9 -0
  3. data/lib/cl_console.rb +86 -0
  4. metadata +92 -0
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Sebastian Cohnen
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,9 @@
1
+ # CouchDB-Lucene Console
2
+
3
+ $ gem install cl-console
4
+ $ irb -rrubygems -rcl_console
5
+ >> cl = CLConsole.new("database/ddoc/index")
6
+ => #<CLConsole:0x101df9d08 [...]>
7
+ >> cl.q "test"
8
+ => {"total_rows"=>1, "etag"=>"11a69b3141bc67e8", "search_duration"=>0, "q"=>"default:test", "rows"=>[{"id"=>"f3fba9f61520e0ef2a245772a900074c", "score"=>1}], "fetch_duration"=>0, "limit"=>25, "skip"=>0}
9
+ >> cl.help
data/lib/cl_console.rb ADDED
@@ -0,0 +1,86 @@
1
+ require "active_support/core_ext"
2
+ require "json"
3
+ require 'open-uri'
4
+
5
+ class CLConsole
6
+ attr_accessor :options
7
+
8
+ def initialize(url, opts={})
9
+ @cl_url = if url.start_with? "http"
10
+ url
11
+ else
12
+ parts = url.split('/')
13
+ "http://localhost:5984/#{parts[0]}/_fti/_design/#{parts[1]}/#{parts[2]}"
14
+ end
15
+
16
+ @options = opts
17
+ end
18
+
19
+ # do the actual query
20
+ def q(query, opts={})
21
+ opts = options.merge(opts).merge({:q => query})
22
+ url = "#{@cl_url}?#{opts.to_param}"
23
+ puts "query: " << url if opts[:debug]
24
+ res = JSON.parse(open(url).string)
25
+ puts ">> q: #{res["q"]} (#{res["search_duration"]}ms / #{res["fetch_duration"]}ms)"
26
+ res
27
+ end
28
+
29
+ # do the query, and only give me the rows
30
+ def qr(query, opts={})
31
+ q(query,opts)["rows"]
32
+ end
33
+
34
+ # do the query, ensure that cl fetches the documents and return only the documents
35
+ def qd(query, opts={})
36
+ qr(query,{:include_docs => true}.merge(opts)).inject([]) do |acc,row|
37
+ acc << row["doc"]
38
+ end
39
+ end
40
+
41
+ # fetch index infos
42
+ def info
43
+ puts "query: " << @cl_url if options[:debug]
44
+ JSON.parse(open("#{@cl_url}").string)
45
+ end
46
+
47
+ # helper to escape searchterms properly for lucene search queries
48
+ def escape(str)
49
+ lucene_chars = %w{+ - & | ! ( ) { } [ ] ^ " ~ * ? : \\}.collect {|e| Regexp.escape(e)}
50
+ str.gsub!(/(#{lucene_chars.join("|")})/, '\\\\\1')
51
+ end
52
+
53
+ # display help message
54
+ def help
55
+ puts <<-HELP_MSG
56
+ ------------------------------------------------------------------------------
57
+ CL-Console Help
58
+ ==============================================================================
59
+ Example usage:
60
+
61
+ # initialize with "database/designdocument/index" (ddoc without _design)
62
+ cl = CLConsole.new("db/ddoc/index")
63
+
64
+ # or initialize with a full url
65
+ cl = CLConsole.new("http://localhost:5984/db/_fti/_design/ddoc/index")
66
+
67
+ # You can also pass a hash with options to be passed to couchdb-lucene, like
68
+ # limit, sorting, debug, ... see the couchdb-lucene documentation for details.
69
+
70
+ # query
71
+
72
+ cl.q "simple" # issue a simple query and give the raw result from c-l
73
+ cl.qr "simple" # same query but only the result rows
74
+ cl.qd "simple" # and this time only return the documents
75
+
76
+ # do a more sophisticated request and let cl-console escape special chars
77
+ cl.qd "title:(couchdb OR lucene) AND body:(awesome AND \#{cl.escape(':-)')})"
78
+
79
+ # meta
80
+
81
+ cl.info # get index infos
82
+ cl.help # this info
83
+ ------------------------------------------------------------------------------
84
+ HELP_MSG
85
+ end
86
+ end
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cl-console
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Sebastian Cohnen
13
+ autorequire: lib/cl_console.rb
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-04-21 00:00:00 +02:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: activesupport
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
29
+ version: "0"
30
+ type: :runtime
31
+ version_requirements: *id001
32
+ - !ruby/object:Gem::Dependency
33
+ name: json
34
+ prerelease: false
35
+ requirement: &id002 !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ segments:
40
+ - 1
41
+ - 2
42
+ - 0
43
+ version: 1.2.0
44
+ type: :runtime
45
+ version_requirements: *id002
46
+ description: This is a little helper to query CouchDB-Lucene indices from an IRB session. It is meant to be used for a very easy inspection, query-testing and exploration of c-l indices.
47
+ email: sebastian.cohnen@gmx.net
48
+ executables: []
49
+
50
+ extensions: []
51
+
52
+ extra_rdoc_files:
53
+ - LICENSE
54
+ - README.md
55
+ files:
56
+ - lib/cl_console.rb
57
+ - LICENSE
58
+ - README.md
59
+ has_rdoc: true
60
+ homepage: http://github.com/tisba/cl-console
61
+ licenses: []
62
+
63
+ post_install_message:
64
+ rdoc_options:
65
+ - --charset=UTF-8
66
+ require_paths:
67
+ - lib
68
+ - lib
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ segments:
75
+ - 0
76
+ version: "0"
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ segments:
82
+ - 0
83
+ version: "0"
84
+ requirements: []
85
+
86
+ rubyforge_project:
87
+ rubygems_version: 1.3.6
88
+ signing_key:
89
+ specification_version: 3
90
+ summary: Little gem meant to be used in IRB sessions for querying CouchDB-Lucene indices.
91
+ test_files: []
92
+