0

I need configure nested entities which are imported via DIH. Between user and address is one to many cardinality (user has many addresses).

This is our import definition in data-config.xml

<document>
    <entity name="user" query="...">
           <field column="id" name="id" />
           <field column="code" name="code" />
        // next fields of user

        <entity name="address" child="true" query="..." where="user_id=user.id">
           <field column="id" name="id" />
           <field column="city" name="city" />
           // ... next fields of address
        </entity>
    </entity>
</document>

and schema.xml configuration like this:

// user fields
<field name="id" type="long" indexed="true" stored="true" />
<field name="name" type="string" indexed="true" stored="true" />
<field name="code" type="string" indexed="true" stored="true" />
// ...

// address fields
<field name="address" type="string" indexed="true" multiValued="true"  stored="true" />
<field name="address.id" type="long" indexed="true" stored="true" />
<field name="address.city" type="string" indexed="false" stored="true" />
// ...

This solution cause that no address object is imported. Thank you for any advice.

EDIT: I also found many warning logs Error creating document : SolrInputDocument(fields: [user_id=122, ... _version_=1671840418228076544,&#8203; _root_=00924553002],&#8203; children: [SolrInputDocument(fields: [address_id=1,&#8203; _root_=00924553002,&#8203; _version_=1671840418228076544]),&#8203; SolrInputDocument(fields: [address_id=20,&#8203; _root_=00924553002,&#8203; _version_=1671840418228076544])])

EDIT 2: Default logs in application hide error. I checked server logs on machine, and I found this error: org.apache.solr.common.SolrException: [doc=null] missing required field: code at org.apache.solr.update.DocumentBuilder.toDocument(DocumentBuilder.java:245)

In schema.xml I have this field set as identifier of user, but children doesn't have this field. Then I tried add code field as alias for ID, and I found another error which tell there is missing value in required fields which are used in parent object (user). This conditions were applied also on nested objects. So I tried also remove these condition from these fields. After this import ran, but when I execute select, all objects are on same level. Solr imported it as flat.

This is expected select result:

{
  "reponse": [
    {
      "id": 1,
      "code": "tsdx242-234",
      "first_name": "Michael",
      "last_name": "Sprox",
      "addresses": [
        {
          "id": 44,
          "city": "Paris",
          "street": "Champs-Elysees"
        },
        {
          "id": 24,
          "city": "Budapest",
          "street": "Akácfa utca"
        }
      ]
    },
    {
      "id": 2,
      "code": "xx45982-114",
      "first_name": "Petra",
      "last_name": "Jurka",
      "addresses": [
        {
          "id": 31,
          "city": "Vienna",
          "street": "Karlsplatz"
        },
        {
          "id": 44,
          "city": "Paris",
          "street": "Champs-Elysees"
        }
      ]
    }
  ]
}

But it gives this, mixed users and addresses in one lovel:

{
  "response": [
    {
      "id": 44,
      "city": "Paris",
      "street": "Champs-Elysees"
    },
    {
      "id": 24,
      "city": "Budapest",
      "street": "Akácfa utca"
    },
    {
      "id": 31,
      "city": "Vienna",
      "street": "Karlsplatz"
    },
    {
      "id": 1,
      "code": "tsdx242-234",
      "first_name": "Michael",
      "last_name": "Sprox"
    },
    {
      "id": 2,
      "code": "xx45982-114",
      "first_name": "Petra",
      "last_name": "Jurka"
    }
  ]
}
Denis Stephanov
  • 4,563
  • 24
  • 78
  • 174

1 Answers1

0

Try to rewrite it to someting like this

<document>
        <entity name="user" query="...">
               <field column="id" name="id" />
            // next fields of user
    
            <entity name="second_entity_name" pk="user_id" query="SELECT ... FROM where user_id = '${user.id}'" >
               <field column="id" name="id" />
               <field column="city" name="city" />
               // ... next fields of address
            </entity>
        </entity>
    </document>
Oyeme
  • 11,088
  • 4
  • 42
  • 65