documentation-elasticsearch 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4d1d3cb0ccddd456ac76d3338a004f915100ccb9
4
+ data.tar.gz: b336c03b271ab6563d3c5d4a35f78f8cceb4ae97
5
+ SHA512:
6
+ metadata.gz: 302a6c63c579cd2107dd01f87939e91219e3563747cb164b4f4c23f2f7676b2a865154aed48932f1845fc238525476a095d974d40d9b5091d9e1e83e41477182
7
+ data.tar.gz: 5cc74e6b96db6d63164afb0ca6c721721cc903447a4a903aabd3e18639c03a2f0b13da3ba4ab7c5388602edcdf7445552481d3d335b519bde679b68b75f00e40
@@ -0,0 +1,20 @@
1
+ Copyright 2013 aTech Media Limited.
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.
@@ -0,0 +1,95 @@
1
+ module Documentation
2
+ module Searchers
3
+ class Elasticsearch < Documentation::Searchers::Abstract
4
+
5
+ def setup
6
+ require 'elasticsearch'
7
+ end
8
+
9
+ def index(page)
10
+ client.index(:index => index_name, :type => 'page', :id => page.id, :body => page_to_hash(page))
11
+ end
12
+
13
+ def delete(page)
14
+ client.delete(:index => index_name, :type => 'page', :id => page.id)
15
+ rescue ::Elasticsearch::Transport::Transport::Errors::NotFound
16
+ false
17
+ end
18
+
19
+ def reset
20
+ client.indices.delete(:index => index_name)
21
+ end
22
+
23
+ def search(query, options = {})
24
+ # Default options
25
+ options[:page] ||= 1
26
+ options[:per_page] ||= 15
27
+
28
+ # Prepare our query
29
+ body = {
30
+ :query => {
31
+ :simple_query_string => {:query => query, :fields => [:title, :content]}
32
+ }
33
+ }
34
+ # Get the total number of results
35
+ count = client.count(:index => index_name, :body => body)
36
+
37
+ # Get some actual results for the requested page
38
+ result = client.search(:index => index_name, :body => body.merge({
39
+ :from => (options[:page].to_i - 1) * options[:per_page].to_i,
40
+ :size => options[:per_page].to_i,
41
+ :highlight => {
42
+ :pre_tags => ["{{{"],
43
+ :post_tags => ["}}}"],
44
+ :fields => {:content => {}}
45
+ }
46
+ }))
47
+
48
+ # Create a result object to be returned
49
+ search_result = Documentation::SearchResult.new
50
+ search_result.page = options[:page].to_i
51
+ search_result.per_page = options[:per_page].to_i
52
+ search_result.total_results = count['count'].to_i
53
+ search_result.query = query
54
+ search_result.time = result['took']
55
+ search_result.raw_results = result['hits']['hits'].inject(Hash.new) do |hash, hit|
56
+ hash[hit['_id'].to_i] = {
57
+ :score => hit['_score'],
58
+ :highlights => hit['highlight'] && hit['highlight']['content']
59
+ }
60
+ hash
61
+ end
62
+
63
+ # Return it
64
+ search_result
65
+ rescue ::Elasticsearch::Transport::Transport::Errors::NotFound
66
+ Documentation::SearchResult.new
67
+ end
68
+
69
+ private
70
+
71
+ def index_name
72
+ options[:index_name] || 'pages'
73
+ end
74
+
75
+ def page_to_hash(page)
76
+ {
77
+ :id => page.id,
78
+ :title => page.title,
79
+ :permalink => page.permalink,
80
+ :full_permalink => page.full_permalink,
81
+ :content => page.content,
82
+ :created_at => page.created_at,
83
+ :updated_at => page.updated_at
84
+ }
85
+ end
86
+
87
+ private
88
+
89
+ def client
90
+ @client ||= ::Elasticsearch::Client.new(options[:client] || {})
91
+ end
92
+
93
+ end
94
+ end
95
+ end
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: documentation-elasticsearch
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Adam Cooke
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-07-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: documentation
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 1.0.0
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '2.0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: 1.0.0
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '2.0'
33
+ - !ruby/object:Gem::Dependency
34
+ name: elasticsearch
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: 1.0.0
40
+ - - "<"
41
+ - !ruby/object:Gem::Version
42
+ version: '2.0'
43
+ type: :runtime
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: 1.0.0
50
+ - - "<"
51
+ - !ruby/object:Gem::Version
52
+ version: '2.0'
53
+ description: Adds support for Elasticsearch to the Documentation gem
54
+ email:
55
+ - adam@atechmedia.com
56
+ executables: []
57
+ extensions: []
58
+ extra_rdoc_files: []
59
+ files:
60
+ - MIT-LICENSE
61
+ - lib/documentation/searchers/elasticsearch.rb
62
+ homepage: http://adamcooke.io
63
+ licenses:
64
+ - MIT
65
+ metadata: {}
66
+ post_install_message:
67
+ rdoc_options: []
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ requirements: []
81
+ rubyforge_project:
82
+ rubygems_version: 2.2.2
83
+ signing_key:
84
+ specification_version: 4
85
+ summary: An Elasticsearch module for the Documentation gem
86
+ test_files: []
87
+ has_rdoc: