org.hibernate.search.elasticsearch.analyzer.impl.ScopedElasticsearchAnalyzerReference Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hibernate-search-elasticsearch Show documentation
Show all versions of hibernate-search-elasticsearch Show documentation
Hibernate Search backend which has indexing operations forwarded to Elasticsearch
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.Collections;
import java.util.HashMap;
import java.util.Map;
import org.hibernate.search.analyzer.spi.AnalyzerReference;
import org.hibernate.search.analyzer.spi.ScopedAnalyzerReference;
import org.hibernate.search.elasticsearch.analyzer.definition.impl.ElasticsearchAnalysisDefinitionRegistry;
import org.hibernate.search.elasticsearch.settings.impl.translation.ElasticsearchAnalyzerDefinitionTranslator;
import org.hibernate.search.util.logging.impl.Log;
import org.hibernate.search.util.logging.impl.LoggerFactory;
import java.lang.invoke.MethodHandles;
/**
* @author Yoann Rodiere
*/
public class ScopedElasticsearchAnalyzerReference extends ElasticsearchAnalyzerReference implements ScopedAnalyzerReference {
private static final Log LOG = LoggerFactory.make( MethodHandles.lookup() );
private final ElasticsearchAnalyzerReference globalAnalyzerReference;
private final Map scopedAnalyzerReferences;
public ScopedElasticsearchAnalyzerReference(Builder builder) {
this.globalAnalyzerReference = builder.globalAnalyzerReference;
this.scopedAnalyzerReferences = Collections.unmodifiableMap( new HashMap<>( builder.scopedAnalyzerReferences ) );
}
@Override
public String getAnalyzerName(String fieldName) {
return getDelegate( fieldName ).getAnalyzerName( fieldName );
}
@Override
public boolean isNormalizer(String fieldName) {
return getDelegate( fieldName ).isNormalizer( fieldName );
}
@Override
public void registerDefinitions(String fieldName, ElasticsearchAnalysisDefinitionRegistry definitionRegistry) {
getDelegate( fieldName ).registerDefinitions( fieldName, definitionRegistry );
}
@Override
public boolean isInitialized() {
return true;
}
@Override
public void initialize(ElasticsearchAnalysisDefinitionRegistry definitionRegistry, ElasticsearchAnalyzerDefinitionTranslator translator) {
// Nothing to do
}
@Override
public CopyBuilder startCopy() {
return new Builder( globalAnalyzerReference, scopedAnalyzerReferences );
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append( getClass().getSimpleName() );
sb.append( "<" );
sb.append( "global=" );
sb.append( globalAnalyzerReference );
sb.append( "," );
sb.append( "scoped=" );
sb.append( scopedAnalyzerReferences );
sb.append( ">" );
return sb.toString();
}
private ElasticsearchAnalyzerReference getDelegate(String fieldName) {
ElasticsearchAnalyzerReference analyzerReference = scopedAnalyzerReferences.get( fieldName );
if ( analyzerReference == null ) {
analyzerReference = globalAnalyzerReference;
}
return analyzerReference;
}
public static class Builder implements ScopedAnalyzerReference.Builder, ScopedAnalyzerReference.CopyBuilder {
private ElasticsearchAnalyzerReference globalAnalyzerReference;
private final Map scopedAnalyzerReferences = new HashMap<>();
public Builder(ElasticsearchAnalyzerReference globalAnalyzerReference,
Map scopedAnalyzers) {
this.globalAnalyzerReference = globalAnalyzerReference;
this.scopedAnalyzerReferences.putAll( scopedAnalyzers );
}
@Override
public ElasticsearchAnalyzerReference getGlobalAnalyzerReference() {
return globalAnalyzerReference;
}
@Override
public void setGlobalAnalyzerReference(AnalyzerReference globalAnalyzerReference) {
this.globalAnalyzerReference = getElasticsearchAnalyzerReference( globalAnalyzerReference );
}
@Override
public void addAnalyzerReference(String scope, AnalyzerReference analyzerReference) {
scopedAnalyzerReferences.put( scope, getElasticsearchAnalyzerReference( analyzerReference ) );
}
@Override
public ScopedElasticsearchAnalyzerReference build() {
return new ScopedElasticsearchAnalyzerReference( this );
}
}
private static ElasticsearchAnalyzerReference getElasticsearchAnalyzerReference(AnalyzerReference analyzerReference) {
if ( !analyzerReference.is( ElasticsearchAnalyzerReference.class ) ) {
throw LOG.analyzerReferenceIsNotRemote( analyzerReference );
}
return analyzerReference.unwrap( ElasticsearchAnalyzerReference.class );
}
}