Static field for document in Data Import Handlerfor Solr

860 Views Asked by At

Im making an index in solr from db in the following way:

<document name="Index">  

<entity name="c" query="SELECT * FROM C">  

  <field column="Name" name="name"/>  

</entity>  

<entity name="p" query="SELECT * FROM P">  

  <field column="Name" name="name"/>  

</entity>  

</document>  

Is it possible to have a static field that is set for each row that signify what type is returned to client so that one can make a call to the right database table based on that information from the json result?

That is a field that has no column in the table

<field name="id" value="1"/> 

Or is there another way to solve this?

2

There are 2 best solutions below

0
On

You can add a column to your SQL query that contains static data like this:

<document name="Index">  
  <entity name="c" query="SELECT *, 'foo' as NameFromC FROM C">  
    <field column="NameFromC" name="name"/>
  </entity>

  <entity name="p" query="SELECT *, 'bar' as NameFromP FROM P">  
    <field column="NameFromP" name="name"/>
  </entity>  
</document>

If you try to add a field with only name and template attributes, Solr will throw an error saying Field must have a column attribute.

0
On
<document name="Index">  
<entity name="c" transformer="TemplateTransformer" query="SELECT * FROM C">  
  <field column="Name" name="name"/>  
  <field column="id" template="1"/>  
</entity>  
<entity name="p" transformer="TemplateTransformer" query="SELECT * FROM P">  
  <field column="Name" name="name"/>  
  <field column="id" template="1"/>  
</entity>  
</document>