allowCreation = Boolean.parseBoolean(allowCreationString);
}
//This are all fields actually present in the index (distinguished with
//those defined in the schema). This also includes actual instances of
//dynamic field definition in the schema.
FieldInfos fieldInfos = indexReader.getFieldInfos(); //we need this twice
//(1) in case the fstConfig uses a wildcard we need to search for
// languages present in the SolrIndex. For that we use the indexReader
// to get the FieldInfos and match them against FST files in the FST
// directory and FieldType definitions in the schema of the SolrCore
//NOTE: this needs only do be done if wildcards are enabled in the fstConfig
if(fstConfig.useWildcard()){
//(1.a) search for present FST files in the FST directory
Map<String,File> presentFstFiles = new HashMap<String,File>();
WildcardFileFilter fstFilter = new WildcardFileFilter(
fstName+".*.fst");
@SuppressWarnings("unchecked")
Iterator<File> fstFiles = FileUtils.iterateFiles(fstDirectory, fstFilter, null);
while(fstFiles.hasNext()){
File fstFile = fstFiles.next();
String fstFileName = fstFile.getName();
//files are named such as "{name}.{lang}.fst"
String language = FilenameUtils.getExtension(
FilenameUtils.getBaseName(fstFileName));
presentFstFiles.put(language, fstFile);
}
//(1.b) iterate over the fields in the Solr index and search for
// matches against the configured indexField name
String fieldWildcard = FieldEncodingEnum.encodeLanguage(indexField,
fieldEncoding, "*");
for(FieldInfo fieldInfo : fieldInfos){
//try to match the field names against the wildcard
if(FilenameUtils.wildcardMatch(fieldInfo.name, fieldWildcard)){
//for matches parse the language from the field name
String language = FieldEncodingEnum.parseLanguage(
fieldInfo.name, fieldEncoding, indexField);
if(language != null && //successfully parsed language
//is current language is enabled?
fstConfig.isLanguage(language) &&
//is there no explicit configuration for this language?
!fstConfig.getExplicitlyIncluded().contains(language)){
//generate the FST file name
StringBuilder fstFileName = new StringBuilder(fstName);
if(!language.isEmpty()){
fstFileName.append('.').append(language);
}
fstFileName.append(".fst");
File fstFile = new File(fstDirectory,fstFileName.toString());
//get the FieldType of the field from the Solr schema
FieldType fieldType = schema.getFieldTypeNoEx(fieldInfo.name);
if(fieldType != null){ //if the fieldType is present
if(allowCreation || fstFile.isFile()){ //and FST is present or can be created
//we need also to check if the stored field with
//the labels is present
//get the stored Field and check if it is present!
String storeFieldName;
if(storeField == null){ //storeField == indexField
storeFieldName = fieldInfo.name;
} else { // check that the storeField is present in the index
storeFieldName = FieldEncodingEnum.encodeLanguage(
storeField, fieldEncoding, language);
FieldInfo storedFieldInfos = fieldInfos.fieldInfo(storeFieldName);
if(storedFieldInfos == null){
log.warn(" ... ignore language {} because Stored Field {} "
+ "for IndexField {} does not exist! ", new Object[]{
language,storeFieldName,fieldInfo.name});
storeFieldName = null;
}
}
if(storeFieldName != null){ // == valid configuration
CorpusInfo fstInfo = new CorpusInfo(language,
fieldInfo.name, storeFieldName,
fieldType, fstFile, allowCreation);
log.debug(" ... init {} ", fstInfo);
addCorpus(fstInfo);
foundCorpus = true;
}
} else {
log.warn(" ... ignore language {} (field: {}) because "
+ "FST file '{}' does not exist and runtime creation "
+ "is deactivated!",new Object[]{ language,
fieldInfo.name, fstFile.getAbsolutePath()});
}
} else {
log.warn(" ... ignore language {} becuase unknown fieldtype "
+ "for SolrFied {}",language,fieldInfo.name);
}
} //else the field matched the wildcard, but has not passed the
//encoding test.
} //Solr field does not match the field definition in the config
} // end iterate over all fields in the SolrIndex
} //else Wildcard not enabled in the fstConfig
//(2) process explicit configuration for configured languages
for(String language : fstConfig.getExplicitlyIncluded()){
//(2.a) get the language specific config (with fallback to default)
Map<String,String> config = fstConfig.getLanguageParams(language);
String langIndexField = config.get(IndexConfiguration.PARAM_FIELD);
String langStoreField = config.get(IndexConfiguration.PARAM_STORE_FIELD);
String langFstFileName = config.get(IndexConfiguration.PARAM_FST);
final boolean langAllowCreation;
final String langAllowCreationString = config.get(IndexConfiguration.PARAM_RUNTIME_GENERATION);
if(langIndexField != null){
//also consider explicit field names as default for the fst name
if(langFstFileName == null){
StringBuilder fileName = new StringBuilder(
getDefaultFstFileName(langIndexField));
if(!language.isEmpty()){
fileName.append('.').append(language);
}
fileName.append(".fst");
langFstFileName = fileName.toString();
}
} else {
langIndexField = indexField;
}
if(langStoreField == null){ //fallbacks
if(storeField != null){ //first to default store field
langStoreField = storeField;
} else { //else to the lang index field
langStoreField = langIndexField;
}
}
if(langFstFileName == null){ //no fstFileName config
// ... use the default
langFstFileName = new StringBuilder(fstName).append('.')
.append(language).append(".fst").toString();
}
if(langAllowCreationString != null){
langAllowCreation = Boolean.parseBoolean(langAllowCreationString);
} else {
langAllowCreation = allowCreation;
}
//(2.b) check if the Solr field is present
String encodedLangIndexField = FieldEncodingEnum.encodeLanguage(
langIndexField, fieldEncoding, language);
String encodedLangStoreField = FieldEncodingEnum.encodeLanguage(
langStoreField, fieldEncoding, language);
FieldInfo langIndexFieldInfo = fieldInfos.fieldInfo(encodedLangIndexField);
if(langIndexFieldInfo != null){
FieldInfo langStoreFieldInfo = fieldInfos.fieldInfo(encodedLangStoreField);
if(langStoreFieldInfo != null){
FieldType fieldType = schema.getFieldTypeNoEx(langIndexFieldInfo.name);
if(fieldType != null){
//(2.c) check the FST file
File langFstFile = new File(fstDirectory,langFstFileName);