zigexn_solr-ruby 0.0.1

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.
Files changed (49) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE.txt +201 -0
  3. data/README.md +41 -0
  4. data/lib/solr/connection.rb +179 -0
  5. data/lib/solr/document.rb +73 -0
  6. data/lib/solr/exception.rb +13 -0
  7. data/lib/solr/field.rb +39 -0
  8. data/lib/solr/importer/array_mapper.rb +26 -0
  9. data/lib/solr/importer/delimited_file_source.rb +38 -0
  10. data/lib/solr/importer/hpricot_mapper.rb +27 -0
  11. data/lib/solr/importer/mapper.rb +51 -0
  12. data/lib/solr/importer/solr_source.rb +43 -0
  13. data/lib/solr/importer/xpath_mapper.rb +35 -0
  14. data/lib/solr/importer.rb +19 -0
  15. data/lib/solr/indexer.rb +52 -0
  16. data/lib/solr/request/add_document.rb +63 -0
  17. data/lib/solr/request/base.rb +36 -0
  18. data/lib/solr/request/commit.rb +30 -0
  19. data/lib/solr/request/delete.rb +50 -0
  20. data/lib/solr/request/dismax.rb +46 -0
  21. data/lib/solr/request/index_info.rb +22 -0
  22. data/lib/solr/request/modify_document.rb +51 -0
  23. data/lib/solr/request/optimize.rb +21 -0
  24. data/lib/solr/request/ping.rb +36 -0
  25. data/lib/solr/request/select.rb +56 -0
  26. data/lib/solr/request/spellcheck.rb +30 -0
  27. data/lib/solr/request/standard.rb +376 -0
  28. data/lib/solr/request/update.rb +23 -0
  29. data/lib/solr/request.rb +26 -0
  30. data/lib/solr/response/add_document.rb +17 -0
  31. data/lib/solr/response/base.rb +42 -0
  32. data/lib/solr/response/commit.rb +17 -0
  33. data/lib/solr/response/delete.rb +13 -0
  34. data/lib/solr/response/dismax.rb +20 -0
  35. data/lib/solr/response/index_info.rb +26 -0
  36. data/lib/solr/response/modify_document.rb +17 -0
  37. data/lib/solr/response/optimize.rb +14 -0
  38. data/lib/solr/response/ping.rb +28 -0
  39. data/lib/solr/response/ruby.rb +42 -0
  40. data/lib/solr/response/select.rb +17 -0
  41. data/lib/solr/response/spellcheck.rb +20 -0
  42. data/lib/solr/response/standard.rb +60 -0
  43. data/lib/solr/response/xml.rb +42 -0
  44. data/lib/solr/response.rb +27 -0
  45. data/lib/solr/solrtasks.rb +27 -0
  46. data/lib/solr/util.rb +32 -0
  47. data/lib/solr/xml.rb +47 -0
  48. data/lib/solr.rb +21 -0
  49. metadata +119 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: aa1bd0ca5581c9884182b920ba9de9e41e015907
4
+ data.tar.gz: cefc5be187ab53102da3097ba08f7311876ecf0c
5
+ SHA512:
6
+ metadata.gz: 2c55e63dc374bebca57cfe28e004ba4e59618c26fa689b148d0d7b656fa39c1259bc44f8bcfa240a49577a13631bb1e2bf2f2910792bd6eabef89b1a2da1a539
7
+ data.tar.gz: 39f0f5ba6d1d14ed1743c18583af570098956416a89c26ff711d923a7affc1daca90838b43a184ebc8faef753a6f442b68e1d69700d0669fe74736fc70de33ed
data/LICENSE.txt ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
data/README.md ADDED
@@ -0,0 +1,41 @@
1
+ # Solr::Ruby
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/zigexn_solr/ruby`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'zigexn_solr-ruby'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install zigexn_solr-ruby
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/zigexn_solr-ruby.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
+
@@ -0,0 +1,179 @@
1
+ # The ASF licenses this file to You under the Apache License, Version 2.0
2
+ # (the "License"); you may not use this file except in compliance with
3
+ # the License. You may obtain a copy of the License at
4
+ #
5
+ # http://www.apache.org/licenses/LICENSE-2.0
6
+ #
7
+ # Unless required by applicable law or agreed to in writing, software
8
+ # distributed under the License is distributed on an "AS IS" BASIS,
9
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
+ # See the License for the specific language governing permissions and
11
+ # limitations under the License.
12
+
13
+ require 'net/http'
14
+
15
+ # TODO: add a convenience method to POST a Solr .xml file, like Solr's example post.sh
16
+
17
+ class Solr::Connection
18
+ attr_reader :url, :autocommit, :connection
19
+
20
+ # create a connection to a solr instance using the url for the solr
21
+ # application context:
22
+ #
23
+ # conn = Solr::Connection.new("http://example.com:8080/solr")
24
+ #
25
+ # if you would prefer to have all adds/updates autocommitted,
26
+ # use :autocommit => :on
27
+ #
28
+ # conn = Solr::Connection.new('http://example.com:8080/solr',
29
+ # :autocommit => :on)
30
+
31
+ def initialize(url="http://localhost:8983/solr", opts={})
32
+ @url = URI.parse(url)
33
+ unless @url.kind_of? URI::HTTP
34
+ raise "invalid http url: #{url}"
35
+ end
36
+
37
+ # TODO: Autocommit seems nice at one level, but it currently is confusing because
38
+ # only calls to Connection#add/#update/#delete, though a Connection#send(AddDocument.new(...))
39
+ # does not autocommit. Maybe #send should check for the request types that require a commit and
40
+ # commit in #send instead of the individual methods?
41
+ @autocommit = opts[:autocommit] == :on
42
+
43
+ # Not actually opening the connection yet, just setting up the persistent connection.
44
+ @connection = Net::HTTP.new(@url.host, @url.port)
45
+
46
+ @connection.read_timeout = opts[:timeout] if opts[:timeout]
47
+ end
48
+
49
+ # add a document to the index. you can pass in either a hash
50
+ #
51
+ # conn.add(:id => 123, :title => 'Tlon, Uqbar, Orbis Tertius')
52
+ #
53
+ # or a Solr::Document
54
+ #
55
+ # conn.add(Solr::Document.new(:id => 123, :title = 'On Writing')
56
+ #
57
+ # true/false will be returned to designate success/failure
58
+
59
+ def add(doc)
60
+ request = Solr::Request::AddDocument.new(doc)
61
+ response = send(request)
62
+ commit if @autocommit
63
+ return response.ok?
64
+ end
65
+
66
+ # update a document in the index (really just an alias to add)
67
+
68
+ def update(doc)
69
+ return add(doc)
70
+ end
71
+
72
+ # performs a standard query and returns a Solr::Response::Standard
73
+ #
74
+ # response = conn.query('borges')
75
+ #
76
+ # alternative you can pass in a block and iterate over hits
77
+ #
78
+ # conn.query('borges') do |hit|
79
+ # puts hit
80
+ # end
81
+ #
82
+ # options include:
83
+ #
84
+ # :sort, :default_field, :rows, :filter_queries, :debug_query,
85
+ # :explain_other, :facets, :highlighting, :mlt,
86
+ # :operator => :or / :and
87
+ # :start => defaults to 0
88
+ # :field_list => array, defaults to ["*", "score"]
89
+
90
+ def query(query, options={}, &action)
91
+ # TODO: Shouldn't this return an exception if the Solr status is not ok? (rather than true/false).
92
+ create_and_send_query(Solr::Request::Standard, options.update(:query => query), &action)
93
+ end
94
+
95
+ # performs a dismax search and returns a Solr::Response::Standard
96
+ #
97
+ # response = conn.search('borges')
98
+ #
99
+ # options are same as query, but also include:
100
+ #
101
+ # :tie_breaker, :query_fields, :minimum_match, :phrase_fields,
102
+ # :phrase_slop, :boost_query, :boost_functions
103
+
104
+ def search(query, options={}, &action)
105
+ create_and_send_query(Solr::Request::Dismax, options.update(:query => query), &action)
106
+ end
107
+
108
+ # sends a commit message to the server
109
+ def commit(options={})
110
+ response = send(Solr::Request::Commit.new(options))
111
+ return response.ok?
112
+ end
113
+
114
+ # sends an optimize message to the server
115
+ def optimize
116
+ response = send(Solr::Request::Optimize.new)
117
+ return response.ok?
118
+ end
119
+
120
+ # pings the connection and returns true/false if it is alive or not
121
+ def ping
122
+ begin
123
+ response = send(Solr::Request::Ping.new)
124
+ return response.ok?
125
+ rescue
126
+ return false
127
+ end
128
+ end
129
+
130
+ # delete a document from the index using the document id
131
+ def delete(document_id)
132
+ response = send(Solr::Request::Delete.new(:id => document_id))
133
+ commit if @autocommit
134
+ response.ok?
135
+ end
136
+
137
+ # delete using a query
138
+ def delete_by_query(query)
139
+ response = send(Solr::Request::Delete.new(:query => query))
140
+ commit if @autocommit
141
+ response.ok?
142
+ end
143
+
144
+ def info
145
+ send(Solr::Request::IndexInfo.new)
146
+ end
147
+
148
+ # send a given Solr::Request and return a RubyResponse or XmlResponse
149
+ # depending on the type of request
150
+ def send(request)
151
+ data = post(request)
152
+ Solr::Response::Base.make_response(request, data)
153
+ end
154
+
155
+ # send the http post request to solr; for convenience there are shortcuts
156
+ # to some requests: add(), query(), commit(), delete() or send()
157
+ def post(request)
158
+ response = @connection.post(@url.path + "/" + request.handler,
159
+ request.to_s,
160
+ { "Content-Type" => request.content_type })
161
+
162
+ case response
163
+ when Net::HTTPSuccess then response.body
164
+ else
165
+ response.error!
166
+ end
167
+
168
+ end
169
+
170
+ private
171
+
172
+ def create_and_send_query(klass, options = {}, &action)
173
+ request = klass.new(options)
174
+ response = send(request)
175
+ return response unless action
176
+ response.each {|hit| action.call(hit)}
177
+ end
178
+
179
+ end
@@ -0,0 +1,73 @@
1
+ # The ASF licenses this file to You under the Apache License, Version 2.0
2
+ # (the "License"); you may not use this file except in compliance with
3
+ # the License. You may obtain a copy of the License at
4
+ #
5
+ # http://www.apache.org/licenses/LICENSE-2.0
6
+ #
7
+ # Unless required by applicable law or agreed to in writing, software
8
+ # distributed under the License is distributed on an "AS IS" BASIS,
9
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
+ # See the License for the specific language governing permissions and
11
+ # limitations under the License.
12
+
13
+ require 'solr/xml'
14
+ require 'solr/field'
15
+
16
+ class Solr::Document
17
+ include Enumerable
18
+ attr_accessor :boost
19
+
20
+ # Create a new Solr::Document, optionally passing in a hash of
21
+ # key/value pairs for the fields
22
+ #
23
+ # doc = Solr::Document.new(:creator => 'Jorge Luis Borges')
24
+ def initialize(hash={})
25
+ @fields = []
26
+ self << hash
27
+ end
28
+
29
+ # Append a Solr::Field
30
+ #
31
+ # doc << Solr::Field.new(:creator => 'Jorge Luis Borges')
32
+ #
33
+ # If you are truly lazy you can simply pass in a hash:
34
+ #
35
+ # doc << {:creator => 'Jorge Luis Borges'}
36
+ def <<(fields)
37
+ case fields
38
+ when Hash
39
+ fields.each_pair do |name,value|
40
+ if value.respond_to?(:each) && !value.is_a?(String)
41
+ value.each {|v| @fields << Solr::Field.new(name => v)}
42
+ else
43
+ @fields << Solr::Field.new(name => value)
44
+ end
45
+ end
46
+ when Solr::Field
47
+ @fields << fields
48
+ else
49
+ raise "must pass in Solr::Field or Hash"
50
+ end
51
+ end
52
+
53
+ # shorthand to allow hash lookups
54
+ # doc['name']
55
+ def [](name)
56
+ field = @fields.find {|f| f.name == name.to_s}
57
+ return field.value if field
58
+ return nil
59
+ end
60
+
61
+ # shorthand to assign as a hash
62
+ def []=(name,value)
63
+ @fields << Solr::Field.new(name => value)
64
+ end
65
+
66
+ # convert the Document to a REXML::Element
67
+ def to_xml
68
+ e = Solr::XML::Element.new 'doc'
69
+ e.attributes['boost'] = @boost.to_s if @boost
70
+ @fields.each {|f| e.add_element(f.to_xml)}
71
+ return e
72
+ end
73
+ end
@@ -0,0 +1,13 @@
1
+ # The ASF licenses this file to You under the Apache License, Version 2.0
2
+ # (the "License"); you may not use this file except in compliance with
3
+ # the License. You may obtain a copy of the License at
4
+ #
5
+ # http://www.apache.org/licenses/LICENSE-2.0
6
+ #
7
+ # Unless required by applicable law or agreed to in writing, software
8
+ # distributed under the License is distributed on an "AS IS" BASIS,
9
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
+ # See the License for the specific language governing permissions and
11
+ # limitations under the License.
12
+
13
+ class Solr::Exception < Exception; end
data/lib/solr/field.rb ADDED
@@ -0,0 +1,39 @@
1
+ # The ASF licenses this file to You under the Apache License, Version 2.0
2
+ # (the "License"); you may not use this file except in compliance with
3
+ # the License. You may obtain a copy of the License at
4
+ #
5
+ # http://www.apache.org/licenses/LICENSE-2.0
6
+ #
7
+ # Unless required by applicable law or agreed to in writing, software
8
+ # distributed under the License is distributed on an "AS IS" BASIS,
9
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
+ # See the License for the specific language governing permissions and
11
+ # limitations under the License.
12
+
13
+ require 'solr/xml'
14
+ require 'time'
15
+
16
+ class Solr::Field
17
+ VALID_PARAMS = [:boost]
18
+ attr_accessor :name
19
+ attr_accessor :value
20
+ attr_accessor :boost
21
+
22
+ # Accepts an optional <tt>:boost</tt> parameter, used to boost the relevance of a particular field.
23
+ def initialize(params)
24
+ @boost = params[:boost]
25
+ name_key = (params.keys - VALID_PARAMS).first
26
+ @name, @value = name_key.to_s, params[name_key]
27
+ # Convert any Time values into UTC/XML schema format (which Solr requires).
28
+ @value = @value.respond_to?(:utc) ? @value.utc.xmlschema : @value.to_s
29
+ end
30
+
31
+ def to_xml
32
+ e = Solr::XML::Element.new 'field'
33
+ e.attributes['name'] = @name
34
+ e.attributes['boost'] = @boost.to_s if @boost
35
+ e.text = @value
36
+ return e
37
+ end
38
+
39
+ end
@@ -0,0 +1,26 @@
1
+ # The ASF licenses this file to You under the Apache License, Version 2.0
2
+ # (the "License"); you may not use this file except in compliance with
3
+ # the License. You may obtain a copy of the License at
4
+ #
5
+ # http://www.apache.org/licenses/LICENSE-2.0
6
+ #
7
+ # Unless required by applicable law or agreed to in writing, software
8
+ # distributed under the License is distributed on an "AS IS" BASIS,
9
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
+ # See the License for the specific language governing permissions and
11
+ # limitations under the License.
12
+
13
+
14
+
15
+ class Solr::Importer::ArrayMapper < Solr::Importer::Mapper
16
+ # TODO document that initializer takes an array of Mappers [mapper1, mapper2, ... mapperN]
17
+
18
+ # TODO: make merge conflict handling configurable. as is, the last map fields win.
19
+ def map(orig_data_array)
20
+ mapped_data = {}
21
+ orig_data_array.each_with_index do |data,i|
22
+ mapped_data.merge!(@mapping[i].map(data))
23
+ end
24
+ mapped_data
25
+ end
26
+ end
@@ -0,0 +1,38 @@
1
+ # The ASF licenses this file to You under the Apache License, Version 2.0
2
+ # (the "License"); you may not use this file except in compliance with
3
+ # the License. You may obtain a copy of the License at
4
+ #
5
+ # http://www.apache.org/licenses/LICENSE-2.0
6
+ #
7
+ # Unless required by applicable law or agreed to in writing, software
8
+ # distributed under the License is distributed on an "AS IS" BASIS,
9
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
+ # See the License for the specific language governing permissions and
11
+ # limitations under the License.
12
+
13
+ # For files with the first line containing field names
14
+ # Currently not designed for enormous files, as all lines are
15
+ # read into an array
16
+ class Solr::Importer::DelimitedFileSource
17
+ include Enumerable
18
+
19
+ def initialize(filename, splitter=/\t/)
20
+ @filename = filename
21
+ @splitter = splitter
22
+ end
23
+
24
+ def each
25
+ lines = IO.readlines(@filename)
26
+ headers = lines[0].split(@splitter).collect{|h| h.chomp}
27
+
28
+ lines[1..-1].each do |line|
29
+ data = headers.zip(line.split(@splitter).collect{|s| s.chomp})
30
+ def data.[](key)
31
+ self.assoc(key.to_s)[1]
32
+ end
33
+
34
+ yield(data)
35
+ end
36
+ end
37
+
38
+ end
@@ -0,0 +1,27 @@
1
+ # The ASF licenses this file to You under the Apache License, Version 2.0
2
+ # (the "License"); you may not use this file except in compliance with
3
+ # the License. You may obtain a copy of the License at
4
+ #
5
+ # http://www.apache.org/licenses/LICENSE-2.0
6
+ #
7
+ # Unless required by applicable law or agreed to in writing, software
8
+ # distributed under the License is distributed on an "AS IS" BASIS,
9
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
+ # See the License for the specific language governing permissions and
11
+ # limitations under the License.
12
+
13
+ begin
14
+ require 'hpricot'
15
+
16
+ class Solr::Importer::HpricotMapper < Solr::Importer::Mapper
17
+ def field_data(doc, path)
18
+ doc.search(path.to_s).collect { |e| e.inner_html }
19
+ end
20
+ end
21
+ rescue LoadError => e # If we can't load hpricot
22
+ class Solr::Importer::HpricotMapper
23
+ def initialize(mapping, options={})
24
+ raise "Hpricot not installed."
25
+ end
26
+ end
27
+ end