SolrJ如何更改注释定义的字段的字段类型?

时间:2018-08-23 14:45:55

标签: solrj

我跟随Solr guide创建了一个类,并在类属性前面使用了@Field批注。

public class MyDocument {

@Field
public String fra_contents;
... // Other fields
//NO getters and setters as shown https://lucene.apache.org/solr/guide/7_2/using-solrj.html#java-object-binding

}

查看生成的“ managed-schema.xml”,表明“ fra_contents”的类型为“ text_general”:

<field name="fra_contents" type="text_general"/>

但是,我需要对该字段应用与与“ text_general”关联的过滤器不同的标记器和过滤器。因此,我以编程方式创建了一个字段类型(以下基于Solr testing code):“ fra_contents_type”:

<fieldType name="fra_contents_type" class="solr.TextField">
<analyzer type="index">
  <tokenizer class="solr.ClassicTokenizerFactory"/>
  <filter class="solr.KeywordRepeatFilterFactory"/>
  <filter class="solr.SynonymGraphFilterFactory" synonyms="lang/fra.txt"/>
  <filter class="solr.FlattenGraphFilterFactory"/>
  <filter class="solr.ASCIIFoldingFilterFactory"/>
  <filter class="solr.ElisionFilterFactory" articles="lang/contractions_fr.txt"/>
  <filter class="solr.SnowballPorterFilterFactory" language="French"/>
  <filter class="solr.RemoveDuplicatesTokenFilterFactory"/>
</analyzer>
<analyzer type="query">
  <tokenizer class="solr.ClassicTokenizerFactory"/>
  <filter class="solr.KeywordRepeatFilterFactory"/>
  <filter class="solr.ASCIIFoldingFilterFactory"/>
  <filter class="solr.ElisionFilterFactory" articles="lang/contractions_fr.txt"/>
  <filter class="solr.SnowballPorterFilterFactory" language="French"/>
  <filter class="solr.RemoveDuplicatesTokenFilterFactory"/>
</analyzer>

另外SO question解释了如何根据java变量类型设置字段类型,但没有说明如何更改此默认字段类型。

那么如何在保留批注的同时(即无需编辑“ managed-schema.xml”)以编程方式更改此字段的字段类型?

任何帮助表示赞赏,

1 个答案:

答案 0 :(得分:0)

这就是我发现使用Schema API后验而不是先验的结果。

 // First retrieves the original field attributes
    SchemaRequest.Field originalField = new SchemaRequest.Field(fieldName);
    Map<String, Object> updatedFieldAttributes = originalField.process(
            getSolrClient()).getField();

    // Modifies the original attributes
    updatedFieldAttributes.put("type",
            fieldTypeName);

    // Updates the field type of the field
    SchemaRequest.ReplaceField replaceFieldRequest = new 
    SchemaRequest.ReplaceField(updatedFieldAttributes);

    // Processes the requests
    List<SchemaRequest.Update> list = new ArrayList<>(3);
    list.add(addFieldTypeRequest);
    list.add(replaceFieldRequest);
    SchemaRequest.MultiUpdate multiUpdateRequest = new SchemaRequest.MultiUpdate(
            list);
    SchemaResponse.UpdateResponse multipleUpdatesResponse = multiUpdateRequest.process(
            getSolrClient());

可能会有更清洁的方法(又名“一个班轮” ;-))来完成此操作!