Cannot find the overridden analyzer when using overridesForField

Ok, so you are using the Elasticsearch integration. Then this behavior is, unfortunately, expected.

How to work it around

You can add another field with that analyzer, it will work and you can rely on that to work.

It will, however, create an unnecessary field in your schema, which may increase the storage use of your Elasticsearch noticeably depending on how much data is contained in this field. To mitigate that, you could just use the analyzer on a “dummy” field that will never have any content, like that:

/**
 * A dummy bridge that allows to define a field without adding any data to documents.
 * Allows to work around query analyzer issues, in particular.
 * See https://discourse.hibernate.org/t/cannot-find-the-second-analyzer-when-using/1043
 */
public class DummyStringBridge implements MetadataProvidingFieldBridge {
	@Override
	public void set(String name, Object value, Document document, LuceneOptions luceneOptions) {
		// No-op: this bridge does not index anything
	}

	@Override
	public void configureFieldMetadata(String name, FieldMetadataBuilder builder) {
		builder.field( name, FieldType.STRING );
	}
}

// Then in your entity class:
 
public class MyEntity {

    // ... 

    @Fields({
            @Field(name = "firstName", index = Index.YES, analyze = Analyze.YES, analyzer = @Analyzer(definition = "nameAnalyzer")),
            @Field(name = "dummyFieldToAddQueryAnalyzer", index = Index.YES, analyze = Analyze.YES, analyzer = @Analyzer(definition = "nameAnalyzer_query"), bridge = @FieldBridge(impl = DummyStringBridge.class)
    })
    private String firstName;

    // ... 
}

Then the dummy field will still be in your schema, but at least documents will never have any data for the dummy field.

Note I didn’t test this; if you do, I’d appreciate your feedback.

Another solution would be for you to use templates or alter the settings of the Elasticsearch indexes after Hibernate Search created them, but that’s much more complex. You’ll need to learn how to add templates or edit the settings of an index (caution: you may need to close the index before updating the settings) and how to configure an index to add analyzers.

Why is this happening?

The problem is that analyzers in Elasticsearch are defined per index, meaning we have to determine what analyzers are needed for each index in order to produce the configuration of each index. We can’t just send all analyzers to a global configuration and say “hey Elasticsearch, make these analyzers available on every index”.

In order to determine which analyzers are needed on each index, the only data available to Hibernate Search is the mapping. So if an analyzer is not used in the mapping (like your query-only analyzer, then it will not be considered as needed for the index, and you will get this error.

There is a ticket to try to improve that situation, but we will probably only be able to address it in the next major version of Hibernate Search, version 6: https://hibernate.atlassian.net/browse/HSEARCH-2534