clarent
clarent

Reputation: 347

Field name from data values in Apache Solr

I have 2 SQL tables for indexing in Solr:

items and params

In table items - 2 fields :

id and name

In table params - 3 fields :

item_id, param_name and param_value

so I have in config:

<document>
    <entity name="item" query="select * from items" >
        <field column="id" name="id" />
         <field column="name" name="name" />

        <entity name="params"  
                query="select param_name, param_value from items where item_id ='${item.id}'">
            <field name="param_value" column="param_value" />
        </entity>

    </entity>
</document>

and I have schema:

<field name="id" type="int" indexed="true" stored="true" required="true" multiValued="false" />
<field name="name" type="string" indexed="true" stored="true" required="true" multiValued="false" />
<field name="param_value" type="string" indexed="true" stored="true" multiValued="true"/>

and result of object will be:

{  
    "id":"894",
   "name":"item_name1",
   "param_value":[  
      "param_value1",
      "param_value2",
      "param_value3",
      "param_value4"
   ]
}

but i want object to be:

{  
   "id":"894",
   "name":"item_name1",
   "param_name1":"param_value1",
   "param_name2":"param_value2",
   "param_name3":"param_value3",
   "param_name4":"param_value4"
}

How can I do this? name of field must be taken from one of result values. I can`t find any example to do this.

Upvotes: 0

Views: 816

Answers (2)

clarent
clarent

Reputation: 347

FOUND SOLUTION!! In data config add script transformer for data:

<dataConfig>
  <script><![CDATA[
              function FieldValueProcess(row)    {
              var row_id = ""+row.get('param_name');
              var row_data = ""+row.get('param_value');
               row.put(row_id, row_data);
               row.remove('param_name');
               row.remove('param_value');
               return row;
             }

 ]]></script>
<dataSource type="JdbcDataSource" 
          driver="com.mysql.jdbc.Driver"
                        url="jdbc:mysql://localhost/xxx" 
                                      user="xx" 
                                                    password="xxx"/>
<document>
<entity name="item" query="select * from items" >
     <entity name="params" transformer="script:FieldValueProcess"
            query="select param_name, param_value from items where item_id ='${item.id}'">
    </entity>

    </entity>
  </document>
</dataConfig>

in schema.xml add dynamicField:

<dynamicField name="*" type="string" indexed="true"  stored="true"/>

Upvotes: 0

Ramzy
Ramzy

Reputation: 7138

You need a dynamic filed in solr, and the config should support that. You can use this for usinig transformers in the config and then index it.

Upvotes: 1

Related Questions