org.elasticsearch.index.mapper.DocumentFieldMappers 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 Elastic Search and Shay Banon under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. Elastic Search 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.index.mapper;
import org.apache.lucene.analysis.Analyzer;
import org.elasticsearch.index.analysis.FieldNameAnalyzer;
import org.elasticsearch.util.collect.ImmutableList;
import org.elasticsearch.util.collect.ImmutableMap;
import org.elasticsearch.util.collect.Iterables;
import org.elasticsearch.util.collect.UnmodifiableIterator;
import org.elasticsearch.util.concurrent.Immutable;
import java.util.Map;
import static org.elasticsearch.util.collect.Lists.*;
import static org.elasticsearch.util.collect.Maps.*;
/**
* @author kimchy (Shay Banon)
*/
@Immutable
public class DocumentFieldMappers implements Iterable {
private final ImmutableList fieldMappers;
private final Map fullNameFieldMappers;
private final Map nameFieldMappers;
private final Map indexNameFieldMappers;
private final FieldNameAnalyzer indexAnalyzer;
private final FieldNameAnalyzer searchAnalyzer;
public DocumentFieldMappers(DocumentMapper docMapper, Iterable fieldMappers) {
final Map tempNameFieldMappers = newHashMap();
final Map tempIndexNameFieldMappers = newHashMap();
final Map tempFullNameFieldMappers = newHashMap();
final Map indexAnalyzers = newHashMap();
final Map searchAnalyzers = newHashMap();
for (FieldMapper fieldMapper : fieldMappers) {
FieldMappers mappers = tempNameFieldMappers.get(fieldMapper.names().name());
if (mappers == null) {
mappers = new FieldMappers(fieldMapper);
} else {
mappers = mappers.concat(fieldMapper);
}
tempNameFieldMappers.put(fieldMapper.names().name(), mappers);
mappers = tempIndexNameFieldMappers.get(fieldMapper.names().indexName());
if (mappers == null) {
mappers = new FieldMappers(fieldMapper);
} else {
mappers = mappers.concat(fieldMapper);
}
tempIndexNameFieldMappers.put(fieldMapper.names().indexName(), mappers);
mappers = tempFullNameFieldMappers.get(fieldMapper.names().fullName());
if (mappers == null) {
mappers = new FieldMappers(fieldMapper);
} else {
mappers = mappers.concat(fieldMapper);
}
tempFullNameFieldMappers.put(fieldMapper.names().fullName(), mappers);
if (fieldMapper.indexAnalyzer() != null) {
indexAnalyzers.put(fieldMapper.names().indexName(), fieldMapper.indexAnalyzer());
}
if (fieldMapper.searchAnalyzer() != null) {
searchAnalyzers.put(fieldMapper.names().indexName(), fieldMapper.searchAnalyzer());
}
}
this.fieldMappers = ImmutableList.copyOf(fieldMappers);
this.nameFieldMappers = ImmutableMap.copyOf(tempNameFieldMappers);
this.indexNameFieldMappers = ImmutableMap.copyOf(tempIndexNameFieldMappers);
this.fullNameFieldMappers = ImmutableMap.copyOf(tempFullNameFieldMappers);
this.indexAnalyzer = new FieldNameAnalyzer(indexAnalyzers, docMapper.indexAnalyzer());
this.searchAnalyzer = new FieldNameAnalyzer(searchAnalyzers, docMapper.searchAnalyzer());
}
@Override public UnmodifiableIterator iterator() {
return fieldMappers.iterator();
}
public FieldMappers name(String name) {
return nameFieldMappers.get(name);
}
public FieldMappers indexName(String indexName) {
return indexNameFieldMappers.get(indexName);
}
public FieldMappers fullName(String fullName) {
return fullNameFieldMappers.get(fullName);
}
/**
* Tries to find first based on {@link #fullName(String)}, then by {@link #indexName(String)}.
*/
public FieldMappers smartName(String name) {
FieldMappers fieldMappers = fullName(name);
if (fieldMappers != null) {
return fieldMappers;
}
return indexName(name);
}
public FieldMapper smartNameFieldMapper(String name) {
FieldMappers fieldMappers = smartName(name);
if (fieldMappers == null) {
return null;
}
return fieldMappers.mapper();
}
/**
* A smart analyzer used for indexing that takes into account specific analyzers configured
* per {@link FieldMapper}.
*/
public Analyzer indexAnalyzer() {
return this.indexAnalyzer;
}
/**
* A smart analyzer used for searching that takes into account specific analyzers configured
* per {@link FieldMapper}.
*/
public Analyzer searchAnalyzer() {
return this.searchAnalyzer;
}
public DocumentFieldMappers concat(DocumentMapper docMapper, FieldMapper... fieldMappers) {
return concat(docMapper, newArrayList(fieldMappers));
}
public DocumentFieldMappers concat(DocumentMapper docMapper, Iterable fieldMappers) {
return new DocumentFieldMappers(docMapper, Iterables.concat(this.fieldMappers, fieldMappers));
}
}