Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Solrj and Dynamic Fields

I'm have a solr schema with dynamic field of different types in. Eg in the schema.xml there are:

<dynamicField name="*_s" type="string" indexed="true"  stored="true"/>
<dynamicField name="*_i" type="int"    indexed="true"  stored="true"/>
<dynamicField name="*_l" type="long"   indexed="true"  stored="true"/>
<dynamicField name="*_f" type="float"  indexed="true"  stored="true"/>
<dynamicField name="*_d" type="double" indexed="true"  stored="true"/>

And I want to access these field using a SolrJ annotated POJO. I know I can have different Map references for each data type in the POJO like this:

...
@Field("*_s")
public Map<String, String> strings;

@Field("*_i")
public Map<String, Integer> integers;
...

But is it possible to have all dynamic fields stored in the same map? I was thinking something like:

...
@Field("*_s")
@Field("*_i")
public Map<String, Object> dynamicFields;
...

The only documentation I can find about SolrJ, POJOs and dynamic fields is an old feature request: https://issues.apache.org/jira/browse/SOLR-1129

like image 302
Tim P Avatar asked Jun 04 '11 17:06

Tim P


People also ask

What are dynamic fields?

Dynamic Fields are a feature that can be used to quickly pull in Contact Details, such as name or email, into your messages. Dynamic Fields can be used to add your Contact Details, instead of typing them out every time. Additionally, they can be added to Templates to personalize standard messages.

What are fields in Solr?

The field type defines how Solr should interpret data in a field and how the field can be queried. There are many field types included with Solr by default, and they can also be defined locally.

What is DocValues in Solr?

DocValues are a way of recording field values internally that is more efficient for some purposes, such as sorting and faceting, than traditional indexing.


1 Answers

I worked out the matching of the 'pattern' value in the @Field annotation doesn't have to match what's in your schema.xml. So, I defined a map in my doc class:

@Field("*DF")
private Map<String, Object> dynamicFields;

and then in the schema.xml the dynamicFields have patterns postfixed by 'DF':

<dynamicField name="*_sDF" type="string" indexed="true" stored="true"/>
<dynamicField name="*_siDF" type="sint" indexed="true" stored="true"/>
<dynamicField name="*_tDF" type="date" indexed="true" stored="true"/>

Now all the dynamicField with different value types get stored and retrieved using solrServer.addBean(doc) and solrResponse.getBeans(Doc.class). This is with Solr 3.2.0 It wasn't working with 1.4..

like image 102
Tim P Avatar answered Sep 28 '22 03:09

Tim P