Many-to-one mapping within Apache Solr

2019-06-24 07:33发布

问题:

I am using Solr to index my database of reports. Reports can have text, submitter information, etc. This currently works and looks like this:

"docs": [
      {
        "Text": "Some Report Text"
        "ReportId": "1",
        "Date": "2013-08-09T14:59:28.147Z",
        "SubmitterId": "11111",
        "FirstName": "John",
        "LastName": "Doe",
        "_version_": 1444554112206110700
      }
 ]

The other thing a report can have is viewers (which is a one-to-many relationship between a single report and the viewers.) I want to be able to capture those viewers like this in my JSON output:

"docs": [
      {
        "Text": "Some Report Text"
        "ReportId": "1",
        "Date": "2013-08-09T14:59:28.147Z",
        "SubmitterId": "11111",
        "FirstName": "John",
        "LastName": "Doe",
        "Viewers": [
            { ViewerId: "22222" },
            { ViewerId: "33333" }
         ]
        "_version_": 1444554112206110700
      }
 ]

I cannot seem to get that to happen, however. Here is my data-config.xml (parts removed that aren't necessary to the question):

<entity name="Report" query="select * from Reports">
   <field column="Text" />
   <field column="ReportId" />
   <!-- Get Submitter Information as another entity. -->
   <entity name="Viewers" query="select * from ReportViewers where Id='${Report.ReportId}'">
     <field column="Id" name="ViewerId" />
   </entity>
</entity>

And the schema.xml:

<field name="Text" type="text_en" indexed="true" stored="true" />
<field name="ReportId" type="string" indexed="true" stored="true" />
<field name="Viewers" type="string" indexed="true" stored="true" multiValued="true" />
<field name="ViewerId" type="string" indexed="true" stored="true" />

When I do the data import, I just don't see anything. No errors, nothing apparently wrong, but I'm pretty sure my data-config and/or my schema are not correct. What am I doing wrong?

回答1:

Unfortunately Solr does not allow nesting (see http://lucene.472066.n3.nabble.com/Possible-to-have-Solr-documents-with-deeply-nested-data-structures-i-e-hashes-within-hashes-td4004285.html). You need to flatten your data!

So

"Viewers": [
            { ViewerId: "22222" },
            { ViewerId: "33333" }
         ]

is not possible. Instead flatten it and have a ViewerIds array:

"ViewerIds": ["22222", "33333" ]

In your schema, you will have:

<field name="ViewerIds" type="string" indexed="true" stored="true" multiValued="true" />

and modify your data-config accordingly.



标签: solr