|
@@ -22,10 +22,8 @@ package org.elasticsearch.index.mapper;
|
|
|
import org.apache.lucene.analysis.Analyzer;
|
|
|
import org.apache.lucene.analysis.TokenStream;
|
|
|
import org.apache.lucene.analysis.tokenattributes.PositionIncrementAttribute;
|
|
|
-import org.apache.lucene.document.Field;
|
|
|
import org.apache.lucene.index.IndexOptions;
|
|
|
import org.apache.lucene.index.IndexableField;
|
|
|
-import org.elasticsearch.Version;
|
|
|
import org.elasticsearch.common.settings.Settings;
|
|
|
import org.elasticsearch.common.xcontent.XContentBuilder;
|
|
|
import org.elasticsearch.index.analysis.NamedAnalyzer;
|
|
@@ -36,6 +34,7 @@ import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
|
import static org.elasticsearch.common.xcontent.support.XContentMapValues.nodeIntegerValue;
|
|
|
+import static org.elasticsearch.common.xcontent.support.XContentMapValues.nodeBooleanValue;
|
|
|
import static org.elasticsearch.index.mapper.TypeParsers.parseField;
|
|
|
|
|
|
/**
|
|
@@ -47,10 +46,12 @@ public class TokenCountFieldMapper extends FieldMapper {
|
|
|
|
|
|
public static class Defaults {
|
|
|
public static final MappedFieldType FIELD_TYPE = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER);
|
|
|
+ public static final boolean DEFAULT_POSITION_INCREMENTS = true;
|
|
|
}
|
|
|
|
|
|
public static class Builder extends FieldMapper.Builder<Builder, TokenCountFieldMapper> {
|
|
|
private NamedAnalyzer analyzer;
|
|
|
+ private boolean enablePositionIncrements = Defaults.DEFAULT_POSITION_INCREMENTS;
|
|
|
|
|
|
public Builder(String name) {
|
|
|
super(name, Defaults.FIELD_TYPE, Defaults.FIELD_TYPE);
|
|
@@ -66,18 +67,26 @@ public class TokenCountFieldMapper extends FieldMapper {
|
|
|
return analyzer;
|
|
|
}
|
|
|
|
|
|
+ public Builder enablePositionIncrements(boolean enablePositionIncrements) {
|
|
|
+ this.enablePositionIncrements = enablePositionIncrements;
|
|
|
+ return this;
|
|
|
+ }
|
|
|
+
|
|
|
+ public boolean enablePositionIncrements() {
|
|
|
+ return enablePositionIncrements;
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
public TokenCountFieldMapper build(BuilderContext context) {
|
|
|
setupFieldType(context);
|
|
|
return new TokenCountFieldMapper(name, fieldType, defaultFieldType,
|
|
|
- context.indexSettings(), analyzer, multiFieldsBuilder.build(this, context), copyTo);
|
|
|
+ context.indexSettings(), analyzer, enablePositionIncrements, multiFieldsBuilder.build(this, context), copyTo);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public static class TypeParser implements Mapper.TypeParser {
|
|
|
@Override
|
|
|
- @SuppressWarnings("unchecked")
|
|
|
- public Mapper.Builder parse(String name, Map<String, Object> node, ParserContext parserContext) throws MapperParsingException {
|
|
|
+ public Mapper.Builder<?,?> parse(String name, Map<String, Object> node, ParserContext parserContext) throws MapperParsingException {
|
|
|
TokenCountFieldMapper.Builder builder = new TokenCountFieldMapper.Builder(name);
|
|
|
for (Iterator<Map.Entry<String, Object>> iterator = node.entrySet().iterator(); iterator.hasNext();) {
|
|
|
Map.Entry<String, Object> entry = iterator.next();
|
|
@@ -93,6 +102,9 @@ public class TokenCountFieldMapper extends FieldMapper {
|
|
|
}
|
|
|
builder.analyzer(analyzer);
|
|
|
iterator.remove();
|
|
|
+ } else if (propName.equals("enable_position_increments")) {
|
|
|
+ builder.enablePositionIncrements(nodeBooleanValue(propNode));
|
|
|
+ iterator.remove();
|
|
|
}
|
|
|
}
|
|
|
parseField(builder, name, node, parserContext);
|
|
@@ -104,11 +116,13 @@ public class TokenCountFieldMapper extends FieldMapper {
|
|
|
}
|
|
|
|
|
|
private NamedAnalyzer analyzer;
|
|
|
+ private boolean enablePositionIncrements;
|
|
|
|
|
|
protected TokenCountFieldMapper(String simpleName, MappedFieldType fieldType, MappedFieldType defaultFieldType,
|
|
|
- Settings indexSettings, NamedAnalyzer analyzer, MultiFields multiFields, CopyTo copyTo) {
|
|
|
+ Settings indexSettings, NamedAnalyzer analyzer, boolean enablePositionIncrements, MultiFields multiFields, CopyTo copyTo) {
|
|
|
super(simpleName, fieldType, defaultFieldType, indexSettings, multiFields, copyTo);
|
|
|
this.analyzer = analyzer;
|
|
|
+ this.enablePositionIncrements = enablePositionIncrements;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -124,7 +138,7 @@ public class TokenCountFieldMapper extends FieldMapper {
|
|
|
if (value == null) {
|
|
|
tokenCount = (Integer) fieldType().nullValue();
|
|
|
} else {
|
|
|
- tokenCount = countPositions(analyzer, name(), value);
|
|
|
+ tokenCount = countPositions(analyzer, name(), value, enablePositionIncrements);
|
|
|
}
|
|
|
|
|
|
boolean indexed = fieldType().indexOptions() != IndexOptions.NONE;
|
|
@@ -138,19 +152,26 @@ public class TokenCountFieldMapper extends FieldMapper {
|
|
|
* @param analyzer analyzer to create token stream
|
|
|
* @param fieldName field name to pass to analyzer
|
|
|
* @param fieldValue field value to pass to analyzer
|
|
|
+ * @param enablePositionIncrements should we count position increments ?
|
|
|
* @return number of position increments in a token stream
|
|
|
* @throws IOException if tokenStream throws it
|
|
|
*/
|
|
|
- static int countPositions(Analyzer analyzer, String fieldName, String fieldValue) throws IOException {
|
|
|
+ static int countPositions(Analyzer analyzer, String fieldName, String fieldValue, boolean enablePositionIncrements) throws IOException {
|
|
|
try (TokenStream tokenStream = analyzer.tokenStream(fieldName, fieldValue)) {
|
|
|
int count = 0;
|
|
|
PositionIncrementAttribute position = tokenStream.addAttribute(PositionIncrementAttribute.class);
|
|
|
tokenStream.reset();
|
|
|
while (tokenStream.incrementToken()) {
|
|
|
- count += position.getPositionIncrement();
|
|
|
+ if (enablePositionIncrements) {
|
|
|
+ count += position.getPositionIncrement();
|
|
|
+ } else {
|
|
|
+ count += Math.min(1, position.getPositionIncrement());
|
|
|
+ }
|
|
|
}
|
|
|
tokenStream.end();
|
|
|
- count += position.getPositionIncrement();
|
|
|
+ if (enablePositionIncrements) {
|
|
|
+ count += position.getPositionIncrement();
|
|
|
+ }
|
|
|
return count;
|
|
|
}
|
|
|
}
|
|
@@ -163,6 +184,14 @@ public class TokenCountFieldMapper extends FieldMapper {
|
|
|
return analyzer.name();
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Indicates if position increments are counted.
|
|
|
+ * @return <code>true</code> if position increments are counted
|
|
|
+ */
|
|
|
+ public boolean enablePositionIncrements() {
|
|
|
+ return enablePositionIncrements;
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
protected String contentType() {
|
|
|
return CONTENT_TYPE;
|
|
@@ -172,12 +201,16 @@ public class TokenCountFieldMapper extends FieldMapper {
|
|
|
protected void doMerge(Mapper mergeWith, boolean updateAllTypes) {
|
|
|
super.doMerge(mergeWith, updateAllTypes);
|
|
|
this.analyzer = ((TokenCountFieldMapper) mergeWith).analyzer;
|
|
|
+ this.enablePositionIncrements = ((TokenCountFieldMapper) mergeWith).enablePositionIncrements;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
protected void doXContentBody(XContentBuilder builder, boolean includeDefaults, Params params) throws IOException {
|
|
|
super.doXContentBody(builder, includeDefaults, params);
|
|
|
builder.field("analyzer", analyzer());
|
|
|
+ if (includeDefaults || enablePositionIncrements() != Defaults.DEFAULT_POSITION_INCREMENTS) {
|
|
|
+ builder.field("enable_position_increments", enablePositionIncrements());
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
}
|