mirror of https://github.com/apache/lucene.git
Add XPathMapper, useful for mapping XML file data into Solr
git-svn-id: https://svn.apache.org/repos/asf/lucene/solr/trunk@525033 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
7ab87c4293
commit
a85eefd70a
|
@ -13,3 +13,4 @@
|
|||
module Solr; module Importer; end; end
|
||||
require 'solr/importer/mapper'
|
||||
require 'solr/importer/tab_delimited_file_source'
|
||||
require 'solr/importer/xpath_mapper'
|
|
@ -0,0 +1,27 @@
|
|||
# The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
# (the "License"); you may not use this file except in compliance with
|
||||
# the License. You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
require 'xml/libxml'
|
||||
|
||||
# For files with the first line containing field names
|
||||
class Solr::Importer::XPathMapper < Solr::Importer::Mapper
|
||||
def field_data(doc, xpath)
|
||||
doc.find(xpath.to_s).collect do |node|
|
||||
case node
|
||||
when XML::Attr
|
||||
node.value
|
||||
when XML::Node
|
||||
node.content
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -0,0 +1,33 @@
|
|||
# The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
# (the "License"); you may not use this file except in compliance with
|
||||
# the License. You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
require 'solr'
|
||||
require 'test/unit'
|
||||
|
||||
class XPathMapperTest < Test::Unit::TestCase
|
||||
|
||||
def setup
|
||||
@doc = XML::Document.file(File.expand_path(File.dirname(__FILE__)) + "/xpath_test_file.xml")
|
||||
end
|
||||
|
||||
def test_simple_xpath
|
||||
mapping = {:solr_field1 => :'/root/parent/child',
|
||||
:solr_field2 => :'/root/parent/child/@attribute'}
|
||||
|
||||
mapper = Solr::Importer::XPathMapper.new(mapping)
|
||||
mapped_data = mapper.map(@doc)
|
||||
|
||||
assert_equal ['text1', 'text2'], mapped_data[:solr_field1]
|
||||
assert_equal ['attribute1', 'attribute2'], mapped_data[:solr_field2]
|
||||
end
|
||||
|
||||
end
|
|
@ -0,0 +1,6 @@
|
|||
<root>
|
||||
<parent>
|
||||
<child attribute="attribute1">text1</child>
|
||||
<child attribute="attribute2">text2</child>
|
||||
</parent>
|
||||
</root>
|
Loading…
Reference in New Issue