All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.hibernate.search.elasticsearch.analyzer.impl.NamedElasticsearchAnalyzerReference Maven / Gradle / Ivy

The newest version!
/*
 * Hibernate Search, full-text search for your domain model
 *
 * License: GNU Lesser General Public License (LGPL), version 2.1 or later
 * See the lgpl.txt file in the root directory or .
 */
package org.hibernate.search.elasticsearch.analyzer.impl;

import java.util.Collection;
import java.util.Map;
import java.util.TreeMap;
import java.util.function.Function;

import org.hibernate.search.elasticsearch.analyzer.definition.impl.ElasticsearchAnalysisDefinitionRegistry;
import org.hibernate.search.elasticsearch.analyzer.definition.impl.ElasticsearchAnalysisDefinitionRegistryPopulator;
import org.hibernate.search.elasticsearch.settings.impl.model.AnalyzerDefinition;
import org.hibernate.search.elasticsearch.settings.impl.model.CharFilterDefinition;
import org.hibernate.search.elasticsearch.settings.impl.model.TokenFilterDefinition;
import org.hibernate.search.elasticsearch.settings.impl.model.TokenizerDefinition;
import org.hibernate.search.elasticsearch.settings.impl.translation.ElasticsearchAnalyzerDefinitionTranslator;
import org.hibernate.search.exception.AssertionFailure;
import org.hibernate.search.util.logging.impl.Log;
import org.hibernate.search.util.logging.impl.LoggerFactory;
import java.lang.invoke.MethodHandles;

/**
 * A reference to an Elasticsearch analyzer that also provides a name for this analyzer.
 * 

* Such a reference may initially only contain the analyzer name, but in this case * it will be fully initialized with the actual analyzer later. * * @author Yoann Rodiere */ public class NamedElasticsearchAnalyzerReference extends ElasticsearchAnalyzerReference { private static final Log LOG = LoggerFactory.make( MethodHandles.lookup() ); private static final ElasticsearchAnalysisDefinitionRegistryPopulator NOOP_DEFINITION_REGISTRY_POPULATOR = (r) -> { }; protected final String name; private ElasticsearchAnalysisDefinitionRegistryPopulator definitionRegistryPopulator; public NamedElasticsearchAnalyzerReference(String name) { this.name = name; } @Override public String getAnalyzerName(String fieldName) { return name; } @Override public boolean isNormalizer(String fieldName) { return false; } @Override public void registerDefinitions(String fieldName, ElasticsearchAnalysisDefinitionRegistry definitionRegistry) { if ( definitionRegistryPopulator == null ) { throw LOG.lazyRemoteAnalyzerReferenceNotInitialized( this ); } definitionRegistryPopulator.populate( definitionRegistry ); } @Override public boolean isInitialized() { return definitionRegistryPopulator != null; } @Override public void initialize(ElasticsearchAnalysisDefinitionRegistry definitionRegistry, ElasticsearchAnalyzerDefinitionTranslator translator) { if ( this.definitionRegistryPopulator != null ) { throw new AssertionFailure( "A named analyzer reference has been initialized more than once: " + this ); } this.definitionRegistryPopulator = createRegistryPopulator( definitionRegistry ); } protected ElasticsearchAnalysisDefinitionRegistryPopulator createRegistryPopulator(ElasticsearchAnalysisDefinitionRegistry definitionRegistry) { AnalyzerDefinition analyzerDefinition = definitionRegistry.getAnalyzerDefinition( name ); if ( analyzerDefinition == null ) { return NOOP_DEFINITION_REGISTRY_POPULATOR; // No-op } String tokenizerName = analyzerDefinition.getTokenizer(); TokenizerDefinition tokenizerDefinition = definitionRegistry.getTokenizerDefinition( tokenizerName ); Map tokenFilters = collectDefinitions( definitionRegistry::getTokenFilterDefinition, analyzerDefinition.getTokenFilters() ); Map charFilters = collectDefinitions( definitionRegistry::getCharFilterDefinition, analyzerDefinition.getCharFilters() ); return new SimpleElasticsearchAnalysisDefinitionRegistryPopulator( name, analyzerDefinition, tokenizerName, tokenizerDefinition, charFilters, tokenFilters ); } protected final Map collectDefinitions(Function registry, Collection names) { Map result = new TreeMap<>(); if ( names != null ) { for ( String name : names ) { T definition = registry.apply( name ); if ( definition != null ) { // Ignore missing definitions: they may be already available on the server result.put( name, definition ); } } } return result; } @Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append( getClass().getSimpleName() ); sb.append( "<" ); sb.append( name ); if ( definitionRegistryPopulator != NOOP_DEFINITION_REGISTRY_POPULATOR ) { sb.append( "," ); sb.append( definitionRegistryPopulator ); } sb.append( ">" ); return sb.toString(); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy