Search in sources :

Example 1 with NamedAnalyzer

use of org.elasticsearch.index.analysis.NamedAnalyzer in project elasticsearch by elastic.

the class CompletionFieldMapperTests method testCompletionAnalyzerSettings.

public void testCompletionAnalyzerSettings() throws Exception {
    String mapping = jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("completion").field("type", "completion").field("analyzer", "simple").field("search_analyzer", "standard").field("preserve_separators", false).field("preserve_position_increments", true).endObject().endObject().endObject().endObject().string();
    DocumentMapper defaultMapper = createIndex("test").mapperService().documentMapperParser().parse("type1", new CompressedXContent(mapping));
    FieldMapper fieldMapper = defaultMapper.mappers().getMapper("completion");
    assertThat(fieldMapper, instanceOf(CompletionFieldMapper.class));
    MappedFieldType completionFieldType = fieldMapper.fieldType();
    NamedAnalyzer indexAnalyzer = completionFieldType.indexAnalyzer();
    assertThat(indexAnalyzer.name(), equalTo("simple"));
    assertThat(indexAnalyzer.analyzer(), instanceOf(CompletionAnalyzer.class));
    CompletionAnalyzer analyzer = (CompletionAnalyzer) indexAnalyzer.analyzer();
    assertThat(analyzer.preservePositionIncrements(), equalTo(true));
    assertThat(analyzer.preserveSep(), equalTo(false));
    NamedAnalyzer searchAnalyzer = completionFieldType.searchAnalyzer();
    assertThat(searchAnalyzer.name(), equalTo("standard"));
    assertThat(searchAnalyzer.analyzer(), instanceOf(CompletionAnalyzer.class));
    analyzer = (CompletionAnalyzer) searchAnalyzer.analyzer();
    assertThat(analyzer.preservePositionIncrements(), equalTo(true));
    assertThat(analyzer.preserveSep(), equalTo(false));
}
Also used : NamedAnalyzer(org.elasticsearch.index.analysis.NamedAnalyzer) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) CompletionAnalyzer(org.apache.lucene.search.suggest.document.CompletionAnalyzer) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 2 with NamedAnalyzer

use of org.elasticsearch.index.analysis.NamedAnalyzer in project elasticsearch by elastic.

the class ParentFieldMapperTests method testNoParentNullFieldCreatedIfNoParentSpecified.

public void testNoParentNullFieldCreatedIfNoParentSpecified() throws Exception {
    Index index = new Index("_index", "testUUID");
    IndexSettings indexSettings = IndexSettingsModule.newIndexSettings(index, Settings.EMPTY);
    NamedAnalyzer namedAnalyzer = new NamedAnalyzer("default", AnalyzerScope.INDEX, new StandardAnalyzer());
    IndexAnalyzers indexAnalyzers = new IndexAnalyzers(indexSettings, namedAnalyzer, namedAnalyzer, namedAnalyzer, Collections.emptyMap(), Collections.emptyMap());
    SimilarityService similarityService = new SimilarityService(indexSettings, Collections.emptyMap());
    MapperService mapperService = new MapperService(indexSettings, indexAnalyzers, xContentRegistry(), similarityService, new IndicesModule(emptyList()).getMapperRegistry(), () -> null);
    XContentBuilder mappingSource = jsonBuilder().startObject().startObject("some_type").startObject("properties").endObject().endObject().endObject();
    mapperService.merge("some_type", new CompressedXContent(mappingSource.string()), MergeReason.MAPPING_UPDATE, false);
    Set<String> allFields = new HashSet<>(mapperService.simpleMatchToIndexNames("*"));
    assertTrue(allFields.contains("_parent"));
    assertFalse(allFields.contains("_parent#null"));
}
Also used : IndicesModule(org.elasticsearch.indices.IndicesModule) NamedAnalyzer(org.elasticsearch.index.analysis.NamedAnalyzer) IndexSettings(org.elasticsearch.index.IndexSettings) Index(org.elasticsearch.index.Index) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) SimilarityService(org.elasticsearch.index.similarity.SimilarityService) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) IndexAnalyzers(org.elasticsearch.index.analysis.IndexAnalyzers) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) HashSet(java.util.HashSet)

Example 3 with NamedAnalyzer

use of org.elasticsearch.index.analysis.NamedAnalyzer in project elasticsearch by elastic.

the class AnalysisModuleTests method testVersionedAnalyzers.

public void testVersionedAnalyzers() throws Exception {
    String yaml = "/org/elasticsearch/index/analysis/test1.yml";
    Settings settings2 = Settings.builder().loadFromStream(yaml, getClass().getResourceAsStream(yaml)).put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()).put(IndexMetaData.SETTING_VERSION_CREATED, Version.V_2_0_0).build();
    AnalysisRegistry newRegistry = getNewRegistry(settings2);
    IndexAnalyzers indexAnalyzers = getIndexAnalyzers(newRegistry, settings2);
    // registry always has the current version
    assertThat(newRegistry.getAnalyzer("default"), is(instanceOf(NamedAnalyzer.class)));
    NamedAnalyzer defaultNamedAnalyzer = (NamedAnalyzer) newRegistry.getAnalyzer("default");
    assertThat(defaultNamedAnalyzer.analyzer(), is(instanceOf(StandardAnalyzer.class)));
    assertEquals(Version.CURRENT.luceneVersion, defaultNamedAnalyzer.analyzer().getVersion());
    // analysis service has the expected version
    assertThat(indexAnalyzers.get("standard").analyzer(), is(instanceOf(StandardAnalyzer.class)));
    assertEquals(Version.V_2_0_0.luceneVersion, indexAnalyzers.get("standard").analyzer().getVersion());
    assertEquals(Version.V_2_0_0.luceneVersion, indexAnalyzers.get("thai").analyzer().getVersion());
    assertThat(indexAnalyzers.get("custom7").analyzer(), is(instanceOf(StandardAnalyzer.class)));
    assertEquals(org.apache.lucene.util.Version.fromBits(3, 6, 0), indexAnalyzers.get("custom7").analyzer().getVersion());
}
Also used : AnalysisRegistry(org.elasticsearch.index.analysis.AnalysisRegistry) NamedAnalyzer(org.elasticsearch.index.analysis.NamedAnalyzer) IndexAnalyzers(org.elasticsearch.index.analysis.IndexAnalyzers) Settings(org.elasticsearch.common.settings.Settings) IndexSettings(org.elasticsearch.index.IndexSettings)

Example 4 with NamedAnalyzer

use of org.elasticsearch.index.analysis.NamedAnalyzer in project elasticsearch by elastic.

the class TypeParsers method parseAnalyzersAndTermVectors.

private static void parseAnalyzersAndTermVectors(FieldMapper.Builder builder, String name, Map<String, Object> fieldNode, Mapper.TypeParser.ParserContext parserContext) {
    NamedAnalyzer indexAnalyzer = null;
    NamedAnalyzer searchAnalyzer = null;
    NamedAnalyzer searchQuoteAnalyzer = null;
    for (Iterator<Map.Entry<String, Object>> iterator = fieldNode.entrySet().iterator(); iterator.hasNext(); ) {
        Map.Entry<String, Object> entry = iterator.next();
        final String propName = entry.getKey();
        final Object propNode = entry.getValue();
        if (propName.equals("term_vector")) {
            parseTermVector(name, propNode.toString(), builder);
            iterator.remove();
        } else if (propName.equals("store_term_vectors")) {
            builder.storeTermVectors(nodeBooleanValue(name, "store_term_vectors", propNode, parserContext));
            iterator.remove();
        } else if (propName.equals("store_term_vector_offsets")) {
            builder.storeTermVectorOffsets(nodeBooleanValue(name, "store_term_vector_offsets", propNode, parserContext));
            iterator.remove();
        } else if (propName.equals("store_term_vector_positions")) {
            builder.storeTermVectorPositions(nodeBooleanValue(name, "store_term_vector_positions", propNode, parserContext));
            iterator.remove();
        } else if (propName.equals("store_term_vector_payloads")) {
            builder.storeTermVectorPayloads(nodeBooleanValue(name, "store_term_vector_payloads", propNode, parserContext));
            iterator.remove();
        } else if (propName.equals("analyzer")) {
            NamedAnalyzer analyzer = parserContext.getIndexAnalyzers().get(propNode.toString());
            if (analyzer == null) {
                throw new MapperParsingException("analyzer [" + propNode.toString() + "] not found for field [" + name + "]");
            }
            indexAnalyzer = analyzer;
            iterator.remove();
        } else if (propName.equals("search_analyzer")) {
            NamedAnalyzer analyzer = parserContext.getIndexAnalyzers().get(propNode.toString());
            if (analyzer == null) {
                throw new MapperParsingException("analyzer [" + propNode.toString() + "] not found for field [" + name + "]");
            }
            searchAnalyzer = analyzer;
            iterator.remove();
        } else if (propName.equals("search_quote_analyzer")) {
            NamedAnalyzer analyzer = parserContext.getIndexAnalyzers().get(propNode.toString());
            if (analyzer == null) {
                throw new MapperParsingException("analyzer [" + propNode.toString() + "] not found for field [" + name + "]");
            }
            searchQuoteAnalyzer = analyzer;
            iterator.remove();
        }
    }
    if (indexAnalyzer == null && searchAnalyzer != null) {
        throw new MapperParsingException("analyzer on field [" + name + "] must be set when search_analyzer is set");
    }
    if (searchAnalyzer == null && searchQuoteAnalyzer != null) {
        throw new MapperParsingException("analyzer and search_analyzer on field [" + name + "] must be set when search_quote_analyzer is set");
    }
    if (searchAnalyzer == null) {
        searchAnalyzer = indexAnalyzer;
    }
    if (searchQuoteAnalyzer == null) {
        searchQuoteAnalyzer = searchAnalyzer;
    }
    if (indexAnalyzer != null) {
        builder.indexAnalyzer(indexAnalyzer);
    }
    if (searchAnalyzer != null) {
        builder.searchAnalyzer(searchAnalyzer);
    }
    if (searchQuoteAnalyzer != null) {
        builder.searchQuoteAnalyzer(searchQuoteAnalyzer);
    }
}
Also used : Entry(java.util.Map.Entry) NamedAnalyzer(org.elasticsearch.index.analysis.NamedAnalyzer) Map(java.util.Map)

Example 5 with NamedAnalyzer

use of org.elasticsearch.index.analysis.NamedAnalyzer in project elasticsearch by elastic.

the class FragmentBuilderHelper method containsBrokenAnalysis.

private static boolean containsBrokenAnalysis(Analyzer analyzer) {
    // TODO maybe we need a getter on Namedanalyzer that tells if this uses broken Analysis
    if (analyzer instanceof NamedAnalyzer) {
        analyzer = ((NamedAnalyzer) analyzer).analyzer();
    }
    if (analyzer instanceof CustomAnalyzer) {
        final CustomAnalyzer a = (CustomAnalyzer) analyzer;
        TokenFilterFactory[] tokenFilters = a.tokenFilters();
        for (TokenFilterFactory tokenFilterFactory : tokenFilters) {
            if (tokenFilterFactory instanceof WordDelimiterTokenFilterFactory || tokenFilterFactory instanceof EdgeNGramTokenFilterFactory) {
                return true;
            }
        }
    }
    return false;
}
Also used : WordDelimiterTokenFilterFactory(org.elasticsearch.index.analysis.WordDelimiterTokenFilterFactory) EdgeNGramTokenFilterFactory(org.elasticsearch.index.analysis.EdgeNGramTokenFilterFactory) NamedAnalyzer(org.elasticsearch.index.analysis.NamedAnalyzer) CustomAnalyzer(org.elasticsearch.index.analysis.CustomAnalyzer) NGramTokenFilterFactory(org.elasticsearch.index.analysis.NGramTokenFilterFactory) WordDelimiterTokenFilterFactory(org.elasticsearch.index.analysis.WordDelimiterTokenFilterFactory) EdgeNGramTokenFilterFactory(org.elasticsearch.index.analysis.EdgeNGramTokenFilterFactory) TokenFilterFactory(org.elasticsearch.index.analysis.TokenFilterFactory)

Aggregations

NamedAnalyzer (org.elasticsearch.index.analysis.NamedAnalyzer)15 Analyzer (org.apache.lucene.analysis.Analyzer)4 TokenStream (org.apache.lucene.analysis.TokenStream)3 StandardAnalyzer (org.apache.lucene.analysis.standard.StandardAnalyzer)3 CompressedXContent (org.elasticsearch.common.compress.CompressedXContent)3 IndexSettings (org.elasticsearch.index.IndexSettings)3 HashSet (java.util.HashSet)2 Map (java.util.Map)2 Tokenizer (org.apache.lucene.analysis.Tokenizer)2 Field (org.apache.lucene.document.Field)2 Term (org.apache.lucene.index.Term)2 TermQuery (org.apache.lucene.search.TermQuery)2 CompletionAnalyzer (org.apache.lucene.search.suggest.document.CompletionAnalyzer)2 CustomAnalyzer (org.elasticsearch.index.analysis.CustomAnalyzer)2 IndexAnalyzers (org.elasticsearch.index.analysis.IndexAnalyzers)2 TokenFilterFactory (org.elasticsearch.index.analysis.TokenFilterFactory)2 Reader (java.io.Reader)1 AbstractMap (java.util.AbstractMap)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1