Skip to content

Commit

Permalink
Merge pull request #59 from johnmbw/shared-index-schema-schema-simila…
Browse files Browse the repository at this point in the history
…rity-npe-8_11_2

Cache IndexSchema on SchemaSimilarityFactory to avoid NPE with shared schemas
  • Loading branch information
johnmbw authored Nov 28, 2022
2 parents 00c3b05 + 0db7376 commit 98cba7d
Showing 1 changed file with 15 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.apache.solr.common.params.SolrParams;
import org.apache.solr.core.SolrCore;
import org.apache.solr.schema.FieldType;
import org.apache.solr.schema.IndexSchema;
import org.apache.solr.schema.SimilarityFactory;
import org.apache.solr.util.plugin.SolrCoreAware;

Expand Down Expand Up @@ -137,14 +138,16 @@ public Similarity getSimilarity() {

private class SchemaSimilarity extends PerFieldSimilarityWrapper {
private Similarity defaultSimilarity;
private volatile IndexSchema schema;

public SchemaSimilarity(Similarity defaultSimilarity) {
this.defaultSimilarity = defaultSimilarity;
schema = core.getLatestSchema();
}

@Override
public Similarity get(String name) {
FieldType fieldType = core.getLatestSchema().getFieldTypeNoEx(name);
FieldType fieldType = getSchema().getFieldTypeNoEx(name);
if (fieldType == null) {
return defaultSimilarity;
} else {
Expand All @@ -153,6 +156,17 @@ public Similarity get(String name) {
}
}

private IndexSchema getSchema() {
// if we're using shareSchema in our config we might actually have an uninitialised
// SolrCore, so we cache the old schema to bridge the gap, but otherwise
// defer to the core itself (in case the schema changes)
IndexSchema latestSchema = core.getLatestSchema();
if (latestSchema != null && schema != latestSchema) {
schema = latestSchema;
}
return schema;
}

@Override
public String toString() {
return "SchemaSimilarity. Default: " + ((get("") == null) ? "null" : get("").toString());
Expand Down

0 comments on commit 98cba7d

Please sign in to comment.