Using:
Hibernate Search 7.2.2
OpenSearch 2.15
Spring Boot 3.3.4
I have no idea why the custom analyzer is not being taken into consideration, as in my opinion the configuration in my code looks fine.
P.S: index does not exist; I want it created once the app starts (just the first time) so I kind of want my index settings to be performed when the app starts instead of doing it “manually” before.
Anyone has any idea?
<dependencyManagement> <dependencies> <dependency> <groupId>org.hibernate.search</groupId> <artifactId>hibernate-search-bom</artifactId> <version>7.2.2.Final</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependency> <groupId>org.hibernate.orm</groupId> <artifactId>hibernate-core</artifactId> <version>6.6.4.Final</version> </dependency> <dependency> <groupId>org.hibernate.search</groupId> <artifactId>hibernate-search-engine</artifactId> </dependency> <dependency> <groupId>org.hibernate.search</groupId> <artifactId>hibernate-search-backend-elasticsearch</artifactId> </dependency> <dependency> <groupId>org.hibernate.search</groupId> <artifactId>hibernate-search-mapper-orm</artifactId> </dependency> <dependency> <groupId>org.jboss.logging</groupId> <artifactId>jboss-logging</artifactId> <version>3.6.0.Final</version> </dependency>
import org.hibernate.search.backend.elasticsearch.analysis.ElasticsearchAnalysisConfigurationContext; import org.hibernate.search.backend.elasticsearch.analysis.ElasticsearchAnalysisConfigurer; public class CustomAnalysisConfigurer implements ElasticsearchAnalysisConfigurer { private static final String CUSTOM_MAPPING = "custom_mapping"; private static final String STREET_ANALYZER = "street_analyzer"; private static final String KEYWORD_NORMALIZER = "custom_normalizer"; private static final String PHONE_NUMBER_CLEANER = "phone_number_cleaner"; private static final String PHONE_NUMBER_NORMALIZER = "phone_number_normalizer"; @Override public void configure(ElasticsearchAnalysisConfigurationContext context) { List<String> mappings = loadMappingsFromFile(); context.tokenFilter(CUSTOM_MAPPING) .type("mapping") .param("mappings", mappings.toArray(new String[0])); context.analyzer(STREET_ANALYZER) .custom() .tokenizer("standard") .tokenFilters( "lowercase", // Convert to lowercase "trim", // Remove leading/trailing whitespace "stop", // Optional: remove common words if needed CUSTOM_MAPPING ); // Define a normalizer for KeywordFields context.normalizer(KEYWORD_NORMALIZER).custom() .tokenFilters( "lowercase", CUSTOM_MAPPING ); context.tokenFilter(PHONE_NUMBER_CLEANER) .type("pattern_replace") .param("pattern", "[^0-9]") .param("replacement", ""); context.normalizer(PHONE_NUMBER_NORMALIZER) .custom() .tokenFilters( PHONE_NUMBER_CLEANER, "trim" ); }
props:
jpa: properties: hibernate: ddl-auto: create search: backend: type: elasticsearch hosts: http://localhost:9200 index_management: strategy: create analysis: configurer: com.package.CustomAnalysisConfigurer show-sql: true
used like this:
@FullTextField( name = "street", analyzer = "street_analyzer", projectable = Projectable.YES, searchable = Searchable.YES) private String street;
Error :
Response: 400 'Bad Request' from 'http://localhost:9200' with body { "error": { "root_cause": [ { "type": "mapper_parsing_exception", "reason": "Failed to parse mapping [_doc]: analyzer [street_analyzer] has not been configured in mappings" } ], "type": "mapper_parsing_exception", "reason": "Failed to parse mapping [_doc]: analyzer [street_analyzer] has not been configured in mappings", "caused_by": { "type": "illegal_argument_exception", "reason": "analyzer [street_analyzer] has not been configured in mappings" } }, "status": 400