Over a million developers have joined DZone.

Solr Data Import Handler & XML – nested entities

DZone's Guide to

Solr Data Import Handler & XML – nested entities

· Java Zone
Free Resource

Bitbucket is for the code that takes us to Mars, decodes the human genome, or drives your next car. What will your code do? Get started with Bitbucket today, it's free.

Data Import Handler is a very nice and powerful tool. The following entry is a description of the problem (and solutions) which I met recently.

Description of the problem

I had to index some list of products, it doesn’t matter what kind of products. However, the products can be combined into groups. In addition, every successive element in the group may have some data omitted – actually the data that were present in the previous documents that appeared in the group. Here is the example structure (irrelevant information was omitted for readability):

    <name>Product 1</name>
    <name>Product 2</name>
      <name>Product 3 and 4</name>


The solution is as always a definition of the “entity” element which looked as follows:

<entity processor="XPathEntityProcessor"
    forEach="/products/product | /products/group/product">
  <field column="id" xpath="//id" />
  <field column="name" xpath="//name" commonField="true" />


With this “forEach” design the processing will take place both for products that do not belong to the group, as well as those in groups. An important attribute if the “commonField” one. It informs DIH that if the record doesn’t have a field defined DIH should fetch the field from the previous record.

The above solution has some limitations, such as the first item in the group should have defined the field “name” and it is important to order the products, but in my case, those limitations corresponded exactly with the specifications of the provided import file.


Bitbucket is the Git solution for professional teams who code with a purpose, not just as a hobby. Get started today, it's free.


Published at DZone with permission of Rafał Kuć, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}