Search in sources :

Example 1 with PreBuiltAnalyzers

use of org.elasticsearch.indices.analysis.PreBuiltAnalyzers in project elasticsearch by elastic.

the class PreBuiltAnalyzerTests method testThatAnalyzersAreUsedInMapping.

public void testThatAnalyzersAreUsedInMapping() throws IOException {
    int randomInt = randomInt(PreBuiltAnalyzers.values().length - 1);
    PreBuiltAnalyzers randomPreBuiltAnalyzer = PreBuiltAnalyzers.values()[randomInt];
    String analyzerName = randomPreBuiltAnalyzer.name().toLowerCase(Locale.ROOT);
    Version randomVersion = randomVersion(random());
    Settings indexSettings = Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, randomVersion).build();
    NamedAnalyzer namedAnalyzer = new PreBuiltAnalyzerProvider(analyzerName, AnalyzerScope.INDEX, randomPreBuiltAnalyzer.getAnalyzer(randomVersion)).get();
    String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties").startObject("field").field("type", "text").field("analyzer", analyzerName).endObject().endObject().endObject().endObject().string();
    DocumentMapper docMapper = createIndex("test", indexSettings).mapperService().documentMapperParser().parse("type", new CompressedXContent(mapping));
    FieldMapper fieldMapper = docMapper.mappers().getMapper("field");
    assertThat(fieldMapper.fieldType().searchAnalyzer(), instanceOf(NamedAnalyzer.class));
    NamedAnalyzer fieldMapperNamedAnalyzer = fieldMapper.fieldType().searchAnalyzer();
    assertThat(fieldMapperNamedAnalyzer.analyzer(), is(namedAnalyzer.analyzer()));
}
Also used : Version(org.elasticsearch.Version) VersionUtils.randomVersion(org.elasticsearch.test.VersionUtils.randomVersion) PreBuiltAnalyzers(org.elasticsearch.indices.analysis.PreBuiltAnalyzers) DocumentMapper(org.elasticsearch.index.mapper.DocumentMapper) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) FieldMapper(org.elasticsearch.index.mapper.FieldMapper) Settings(org.elasticsearch.common.settings.Settings)

Example 2 with PreBuiltAnalyzers

use of org.elasticsearch.indices.analysis.PreBuiltAnalyzers in project elasticsearch by elastic.

the class AnalysisRegistryTests method testBuiltInAnalyzersAreCached.

public void testBuiltInAnalyzersAreCached() throws IOException {
    Settings settings = Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()).build();
    Settings indexSettings = Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT).build();
    IndexSettings idxSettings = IndexSettingsModule.newIndexSettings("index", indexSettings);
    IndexAnalyzers indexAnalyzers = new AnalysisRegistry(new Environment(settings), emptyMap(), emptyMap(), emptyMap(), emptyMap(), emptyMap()).build(idxSettings);
    IndexAnalyzers otherIndexAnalyzers = new AnalysisRegistry(new Environment(settings), emptyMap(), emptyMap(), emptyMap(), emptyMap(), emptyMap()).build(idxSettings);
    final int numIters = randomIntBetween(5, 20);
    for (int i = 0; i < numIters; i++) {
        PreBuiltAnalyzers preBuiltAnalyzers = RandomPicks.randomFrom(random(), PreBuiltAnalyzers.values());
        assertSame(indexAnalyzers.get(preBuiltAnalyzers.name()), otherIndexAnalyzers.get(preBuiltAnalyzers.name()));
    }
}
Also used : IndexSettings(org.elasticsearch.index.IndexSettings) PreBuiltAnalyzers(org.elasticsearch.indices.analysis.PreBuiltAnalyzers) Environment(org.elasticsearch.env.Environment) Settings(org.elasticsearch.common.settings.Settings) IndexSettings(org.elasticsearch.index.IndexSettings)

Aggregations

Settings (org.elasticsearch.common.settings.Settings)2 PreBuiltAnalyzers (org.elasticsearch.indices.analysis.PreBuiltAnalyzers)2 Version (org.elasticsearch.Version)1 CompressedXContent (org.elasticsearch.common.compress.CompressedXContent)1 Environment (org.elasticsearch.env.Environment)1 IndexSettings (org.elasticsearch.index.IndexSettings)1 DocumentMapper (org.elasticsearch.index.mapper.DocumentMapper)1 FieldMapper (org.elasticsearch.index.mapper.FieldMapper)1 VersionUtils.randomVersion (org.elasticsearch.test.VersionUtils.randomVersion)1