Over a million developers have joined DZone.

Using Solr 4.9 new ChildDocTransformerFactory

· Java Zone

What every Java engineer should know about microservices: Reactive Microservices Architecture.  Brought to you in partnership with Lightbend.

Lucene & Solr 4.9 were released a couple weeks ago and introduced a new result document transformer called ChildDocTransformerFactory.

The ChildDocTransformerFactory transformer is useful when we need to get child documents that were indexed as nested documents.

There are many use cases where we want to 'join' results into a single response where this transformer can help.

For instance, lets say we have the following nested documents:
<doc>
	<field name="id">1</field>
	<field name="name">I am the parent</field>
	<field name="cat">PARENT</field>
	<doc>
		<field name="id">1.1</field>
		<field name="name">I am the 1st child</field>
		<field name="cat">CHILD</field>
	</doc>
	<doc>
		<field name="id">1.2</field>
		<field name="name">I am the 2nd child</field>
		<field name="cat">CHILD</field>
		<doc>
			<field name="id">1.2.1</field>
			<field name="name">I am a grandchildren</field>
			<field name="cat">GRANDCHILD</field>
		</doc>
	</doc>
</doc>

Now, we can use the below BlockJoinQuery to find the parent of all the documents containing the words I am child:

q={!parent which="cat:PARENT"}name:(I am +child)

The result of the above query will contain the parent document and will look like that:
{
  "responseHeader": {
    "status": 0,
    "QTime": 1,
    "params": {
      "indent": "true",
      "q": "{!parent which=\"cat:PARENT\"}name:(I am +child)",
      "_": "1403793883163",
      "wt": "json"
    }
  },
  "response": {
    "numFound": 1,
    "start": 0,
    "docs": [
      {
        "id": "1",
        "name": "I am the parent",
        "cat": [
          "PARENT"
        ],
        "_version_": 1471982731634147300
      }
    ]
  }
}


Now, lets get the parent document along with the other child document but without the grandchild.
We do that by adding a fields parameter that looks like:
fl=id,name,[child parentFilter=cat:PARENT childFilter=cat:CHILD]

The above contains regular fields request - id and name and additional ChildDocTransformer that enable Solr to return the given parent and it's nested child's.
Pay attention that we've added optional parameter - childFilter=cat:CHILD that filters the GRANDCHILDREN out of the response.

The output after adding the above 'fl' is (XML):
<response>

<lst name="responseHeader">
  <int name="status">0</int>
  <int name="QTime">1</int>
  <lst name="params">
    <str name="fl">id,name,[child parentFilter=cat:PARENT childFilter=cat:CHILD]</str>
    <str name="indent">true</str>
    <str name="q">{!parent which="cat:PARENT"}name:(I am +child)</str>
    <str name="_">1403794541806</str>
    <str name="wt">xml</str>
  </lst>
</lst>
<result name="response" numFound="1" start="0">
  <doc>
    <str name="id">1</str>
    <str name="name">I am the parent</str>
    <doc>
      <str name="id">1.1</str>
      <str name="name">I am the 1st child</str>
      <arr name="cat">
        <str>CHILD</str>
      </arr></doc>
    <doc>
      <str name="id">1.2</str>
      <str name="name">I am the 2nd child</str>
      <arr name="cat">
        <str>CHILD</str>
      </arr></doc>
</doc>
</result>
</response>


Microservices for Java, explained. Revitalize your legacy systems (and your career) with Reactive Microservices Architecture, a free O'Reilly book. Brought to you in partnership with Lightbend.

Topics:

Published at DZone with permission of Tomer Levi. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
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.
Subscribe

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

{{ parent.tldr }}

{{ parent.urlSource.name }}