0

I followed Solr guide created a class and used @Field annotation in front of the class attributes.

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

}

Looking at the generated "managed-schema.xml" shows that "fra_contents" is of type "text_general" :

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

Yet I need to apply a different tokenizer, and different filters to this field than the ones associated with "text_general". So I created a fieldtype programmatically (following based on Solr testing code) called "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>

This other SO question explains how the fieldtype is set based on the java variable type, but does not tell how to change this defauld fieldtype.

So how can I change the fieldtype of this field programmatically while keeping the annotation (ie without editing the "managed-schema.xml") ?

Any help appreciated,

HelloWorld
  • 2,275
  • 18
  • 29

1 Answers1

0

So here is what I found which works a posteriori and not a priori using the 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());

There may be a cleaner way (aka "one liner" ;-) ) to do it!

HelloWorld
  • 2,275
  • 18
  • 29