org.elasticsearch.indices.analysis.PreBuiltAnalyzers Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of elasticsearch Show documentation
Show all versions of elasticsearch Show documentation
Elasticsearch subproject :server
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.indices.analysis;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.CharArraySet;
import org.apache.lucene.analysis.core.KeywordAnalyzer;
import org.apache.lucene.analysis.core.SimpleAnalyzer;
import org.apache.lucene.analysis.core.StopAnalyzer;
import org.apache.lucene.analysis.core.WhitespaceAnalyzer;
import org.apache.lucene.analysis.en.EnglishAnalyzer;
import org.apache.lucene.analysis.standard.ClassicAnalyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.elasticsearch.Version;
import org.elasticsearch.indices.analysis.PreBuiltCacheFactory.CachingStrategy;
import java.util.Locale;
public enum PreBuiltAnalyzers {
STANDARD(CachingStrategy.ELASTICSEARCH) {
@Override
protected Analyzer create(Version version) {
final Analyzer a = new StandardAnalyzer(CharArraySet.EMPTY_SET);
a.setVersion(version.luceneVersion);
return a;
}
},
DEFAULT(CachingStrategy.ELASTICSEARCH){
@Override
protected Analyzer create(Version version) {
// by calling get analyzer we are ensuring reuse of the same STANDARD analyzer for DEFAULT!
// this call does not create a new instance
return STANDARD.getAnalyzer(version);
}
},
KEYWORD(CachingStrategy.ONE) {
@Override
protected Analyzer create(Version version) {
return new KeywordAnalyzer();
}
},
STOP {
@Override
protected Analyzer create(Version version) {
Analyzer a = new StopAnalyzer(EnglishAnalyzer.ENGLISH_STOP_WORDS_SET);
a.setVersion(version.luceneVersion);
return a;
}
},
WHITESPACE {
@Override
protected Analyzer create(Version version) {
Analyzer a = new WhitespaceAnalyzer();
a.setVersion(version.luceneVersion);
return a;
}
},
SIMPLE {
@Override
protected Analyzer create(Version version) {
Analyzer a = new SimpleAnalyzer();
a.setVersion(version.luceneVersion);
return a;
}
},
CLASSIC {
@Override
protected Analyzer create(Version version) {
Analyzer a = new ClassicAnalyzer();
a.setVersion(version.luceneVersion);
return a;
}
};
protected abstract Analyzer create(Version version);
protected final PreBuiltCacheFactory.PreBuiltCache cache;
PreBuiltAnalyzers() {
this(PreBuiltCacheFactory.CachingStrategy.LUCENE);
}
PreBuiltAnalyzers(PreBuiltCacheFactory.CachingStrategy cachingStrategy) {
cache = PreBuiltCacheFactory.getCache(cachingStrategy);
}
public PreBuiltCacheFactory.PreBuiltCache getCache() {
return cache;
}
public synchronized Analyzer getAnalyzer(Version version) {
Analyzer analyzer = cache.get(version);
if (analyzer == null) {
analyzer = this.create(version);
cache.put(version, analyzer);
}
return analyzer;
}
/**
* Get a pre built Analyzer by its name or fallback to the default one
* @param name Analyzer name
* @param defaultAnalyzer default Analyzer if name not found
*/
public static PreBuiltAnalyzers getOrDefault(String name, PreBuiltAnalyzers defaultAnalyzer) {
try {
return valueOf(name.toUpperCase(Locale.ROOT));
} catch (IllegalArgumentException e) {
return defaultAnalyzer;
}
}
}