solr-ruby 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/solr/importer/hpricot_mapper.rb +13 -6
- data/lib/solr/importer/xpath_mapper.rb +18 -10
- data/lib/solr/indexer.rb +4 -6
- data/test/unit/document_test.rb +1 -1
- data/test/unit/hpricot_mapper_test.rb +26 -22
- data/test/unit/request_test.rb +1 -1
- data/test/unit/xpath_mapper_test.rb +20 -15
- metadata +2 -2
@@ -10,11 +10,18 @@
|
|
10
10
|
# See the License for the specific language governing permissions and
|
11
11
|
# limitations under the License.
|
12
12
|
|
13
|
-
|
13
|
+
begin
|
14
|
+
require 'hpricot'
|
14
15
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
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
|
19
20
|
end
|
20
|
-
|
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
|
@@ -10,18 +10,26 @@
|
|
10
10
|
# See the License for the specific language governing permissions and
|
11
11
|
# limitations under the License.
|
12
12
|
|
13
|
-
|
13
|
+
begin
|
14
|
+
require 'xml/libxml'
|
14
15
|
|
15
|
-
# For files with the first line containing field names
|
16
|
-
class Solr::Importer::XPathMapper < Solr::Importer::Mapper
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
16
|
+
# For files with the first line containing field names
|
17
|
+
class Solr::Importer::XPathMapper < Solr::Importer::Mapper
|
18
|
+
def field_data(doc, xpath)
|
19
|
+
doc.find(xpath.to_s).collect do |node|
|
20
|
+
case node
|
21
|
+
when XML::Attr
|
22
|
+
node.value
|
23
|
+
when XML::Node
|
24
|
+
node.content
|
25
|
+
end
|
24
26
|
end
|
25
27
|
end
|
26
28
|
end
|
29
|
+
rescue LoadError => e # If we can't load libxml
|
30
|
+
class Solr::Importer::XPathMapper
|
31
|
+
def initialize(mapping, options={})
|
32
|
+
raise "libxml not installed"
|
33
|
+
end
|
34
|
+
end
|
27
35
|
end
|
data/lib/solr/indexer.rb
CHANGED
@@ -11,12 +11,9 @@
|
|
11
11
|
# limitations under the License.
|
12
12
|
|
13
13
|
class Solr::Indexer
|
14
|
-
|
15
|
-
def self.index(data_source, mapper_or_mapping, options={})
|
16
|
-
indexer = Solr::Indexer.new(data_source, mapper_or_mapping, options)
|
17
|
-
indexer.index
|
18
|
-
end
|
14
|
+
attr_reader :solr
|
19
15
|
|
16
|
+
# TODO: document options!
|
20
17
|
def initialize(data_source, mapper_or_mapping, options={})
|
21
18
|
solr_url = options[:solr_url] || ENV["SOLR_URL"] || "http://localhost:8983/solr"
|
22
19
|
@solr = Solr::Connection.new(solr_url, options) #TODO - these options contain the solr_url and debug keys also, so tidy up what gets passed
|
@@ -33,7 +30,8 @@ class Solr::Indexer
|
|
33
30
|
@data_source.each do |record|
|
34
31
|
document = @mapper.map(record)
|
35
32
|
|
36
|
-
|
33
|
+
# TODO: check arrity of block, if 3, pass counter as 3rd argument
|
34
|
+
yield(record, document) if block_given? # TODO check return of block, if not true then don't index, or perhaps if document.empty?
|
37
35
|
|
38
36
|
buffer << document
|
39
37
|
|
data/test/unit/document_test.rb
CHANGED
@@ -59,7 +59,7 @@ class DocumentTest < Test::Unit::TestCase
|
|
59
59
|
def test_boost
|
60
60
|
doc = Solr::Document.new :name => "McGrump"
|
61
61
|
doc.boost = 300.28
|
62
|
-
assert_match(/<doc boost=['"]300.28['"]>[\s]
|
62
|
+
assert_match(/<doc boost=['"]300.28['"]>[\s]*<field name=['"]name['"]>McGrump<\/field>[\s]*<\/doc>/, doc.to_xml.to_s)
|
63
63
|
end
|
64
64
|
|
65
65
|
end
|
@@ -10,31 +10,35 @@
|
|
10
10
|
# See the License for the specific language governing permissions and
|
11
11
|
# limitations under the License.
|
12
12
|
|
13
|
-
|
14
|
-
require '
|
15
|
-
require '
|
13
|
+
begin
|
14
|
+
require 'solr'
|
15
|
+
require 'test/unit'
|
16
|
+
require 'hpricot'
|
16
17
|
|
17
|
-
class HpricotMapperTest < Test::Unit::TestCase
|
18
|
+
class HpricotMapperTest < Test::Unit::TestCase
|
18
19
|
|
19
|
-
|
20
|
-
|
21
|
-
|
20
|
+
def setup
|
21
|
+
@doc = open(File.expand_path(File.dirname(__FILE__)) + "/hpricot_test_file.xml"){|f| Hpricot.XML(f)}
|
22
|
+
end
|
22
23
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
24
|
+
def test_simple_hpricot_path
|
25
|
+
mapping = {:field1 => :'child[@attribute="attribute1"]',
|
26
|
+
:field2 => :'child[@attribute="attribute2"]',
|
27
|
+
:field3 => :'child[@attribute="attribute3"]',
|
28
|
+
:field4 => :'child[@attribute="attribute3"] grandchild',
|
29
|
+
:field5 => :'child'}
|
29
30
|
|
30
|
-
|
31
|
-
|
31
|
+
mapper = Solr::Importer::HpricotMapper.new(mapping)
|
32
|
+
mapped_data = mapper.map(@doc)
|
32
33
|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
34
|
+
assert_equal ['text1'], mapped_data[:field1]
|
35
|
+
assert_equal ['text2'], mapped_data[:field2]
|
36
|
+
assert_equal ['text3<grandchild>grandchild 3 text</grandchild>'], mapped_data[:field3]
|
37
|
+
assert_equal ['grandchild 3 text'], mapped_data[:field4]
|
38
|
+
assert_equal ['text1', 'text2', 'text3<grandchild>grandchild 3 text</grandchild>'], mapped_data[:field5]
|
39
|
+
end
|
39
40
|
|
40
|
-
end
|
41
|
+
end
|
42
|
+
rescue LoadError => e
|
43
|
+
puts "HpricotMapperTest not run because #{e}"
|
44
|
+
end
|
data/test/unit/request_test.rb
CHANGED
@@ -22,7 +22,7 @@ class RequestTest < Test::Unit::TestCase
|
|
22
22
|
request = Solr::Request::Commit.new
|
23
23
|
assert_equal :xml, request.response_format
|
24
24
|
assert_equal 'update', request.handler
|
25
|
-
|
25
|
+
assert_match(/<commit waitSearcher=["']true["'] waitFlush=["'']true["'']\/>/, request.to_s)
|
26
26
|
end
|
27
27
|
|
28
28
|
def test_add_doc_request
|
@@ -10,24 +10,29 @@
|
|
10
10
|
# See the License for the specific language governing permissions and
|
11
11
|
# limitations under the License.
|
12
12
|
|
13
|
-
|
14
|
-
require '
|
13
|
+
begin
|
14
|
+
require 'solr'
|
15
|
+
require 'test/unit'
|
16
|
+
require 'xml/libxml'
|
15
17
|
|
16
|
-
class XPathMapperTest < Test::Unit::TestCase
|
18
|
+
class XPathMapperTest < Test::Unit::TestCase
|
17
19
|
|
18
|
-
|
19
|
-
|
20
|
-
|
20
|
+
def setup
|
21
|
+
@doc = XML::Document.file(File.expand_path(File.dirname(__FILE__)) + "/xpath_test_file.xml")
|
22
|
+
end
|
21
23
|
|
22
|
-
|
23
|
-
|
24
|
-
|
24
|
+
def test_simple_xpath
|
25
|
+
mapping = {:solr_field1 => :'/root/parent/child',
|
26
|
+
:solr_field2 => :'/root/parent/child/@attribute'}
|
25
27
|
|
26
|
-
|
27
|
-
|
28
|
+
mapper = Solr::Importer::XPathMapper.new(mapping)
|
29
|
+
mapped_data = mapper.map(@doc)
|
28
30
|
|
29
|
-
|
30
|
-
|
31
|
-
|
31
|
+
assert_equal ['text1', 'text2'], mapped_data[:solr_field1]
|
32
|
+
assert_equal ['attribute1', 'attribute2'], mapped_data[:solr_field2]
|
33
|
+
end
|
32
34
|
|
33
|
-
end
|
35
|
+
end
|
36
|
+
rescue LoadError => e
|
37
|
+
puts "XPathMapperTest not run because #{e}"
|
38
|
+
end
|
metadata
CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.2
|
|
3
3
|
specification_version: 1
|
4
4
|
name: solr-ruby
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.0.
|
7
|
-
date: 2007-05-
|
6
|
+
version: 0.0.3
|
7
|
+
date: 2007-05-22 00:00:00 -04:00
|
8
8
|
summary: Ruby library for working with Apache Solr
|
9
9
|
require_paths:
|
10
10
|
- lib
|