Solrj和动态领域

时间:2011-06-04 17:09:50

标签: dynamic map field pojo solrj

我有一个包含不同类型动态字段的solr模式。例如,在schema.xml中有:

<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"/>

我想使用SolrJ注释的POJO访问这些字段。我知道我可以为POJO中的每种数据类型提供不同的Map引用,如下所示:

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

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

但是可以将所有动态字段存储在同一个地图中吗?我想的是:

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

我能找到的关于SolrJ,POJO和动态字段的唯一文档是一个旧的功能请求: https://issues.apache.org/jira/browse/SOLR-1129

1 个答案:

答案 0 :(得分:10)

我计算出@Field注释中'pattern'值的匹配不必与schema.xml中的匹配。所以,我在我的doc类中定义了一个地图:

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

然后在schema.xml中,dynamicFields的模式后缀为'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"/>

现在,使用solrServer.addBean(doc)和solrResponse.getBeans(Doc.class)存储和检索具有不同值类型的所有dynamicField。这是使用Solr 3.2.0它不适用于1.4 ..