Skip to content

Commit

Permalink
Mapping: Allow to configure position_offset_gap for string mapping, c…
Browse files Browse the repository at this point in the history
…loses elastic#1813.
  • Loading branch information
kimchy committed Mar 24, 2012
1 parent 8ae3d78 commit 8a2fbe0
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
* Named analyzer is an analyzer wrapper around an actual analyzer ({@link #analyzer} that is associated
* with a name ({@link #name()}.
*/
public final class NamedAnalyzer extends Analyzer {
public class NamedAnalyzer extends Analyzer {

private final String name;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Licensed to ElasticSearch and Shay Banon 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.index.analysis;

/**
*/
public class NamedCustomAnalyzer extends NamedAnalyzer {

private final int positionOffsetGap;

public NamedCustomAnalyzer(NamedAnalyzer analyzer, int positionOffsetGap) {
super(analyzer.name(), analyzer.scope(), analyzer.analyzer());
this.positionOffsetGap = positionOffsetGap;
}

@Override
public int getPositionIncrementGap(String fieldName) {
return positionOffsetGap;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -476,14 +476,14 @@ protected void doXContentBody(XContentBuilder builder) throws IOException {
if (boost != 1.0f) {
builder.field("boost", boost);
}
if (indexAnalyzer != null && searchAnalyzer != null && indexAnalyzer.name().equals(searchAnalyzer.name()) && !indexAnalyzer.name().startsWith("_")) {
if (indexAnalyzer != null && searchAnalyzer != null && indexAnalyzer.name().equals(searchAnalyzer.name()) && !indexAnalyzer.name().startsWith("_") && !indexAnalyzer.name().equals("default")) {
// same analyzers, output it once
builder.field("analyzer", indexAnalyzer.name());
} else {
if (indexAnalyzer != null && !indexAnalyzer.name().startsWith("_")) {
if (indexAnalyzer != null && !indexAnalyzer.name().startsWith("_") && !indexAnalyzer.name().equals("default")) {
builder.field("index_analyzer", indexAnalyzer.name());
}
if (searchAnalyzer != null && !searchAnalyzer.name().startsWith("_")) {
if (searchAnalyzer != null && !searchAnalyzer.name().startsWith("_") && !searchAnalyzer.name().equals("default")) {
builder.field("search_analyzer", searchAnalyzer.name());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.support.XContentMapValues;
import org.elasticsearch.index.analysis.NamedAnalyzer;
import org.elasticsearch.index.analysis.NamedCustomAnalyzer;
import org.elasticsearch.index.mapper.*;
import org.elasticsearch.index.mapper.internal.AllFieldMapper;

Expand All @@ -44,12 +46,15 @@ public class StringFieldMapper extends AbstractFieldMapper<String> implements Al
public static class Defaults extends AbstractFieldMapper.Defaults {
// NOTE, when adding defaults here, make sure you add them in the builder
public static final String NULL_VALUE = null;
public static final int POSITION_OFFSET_GAP = 0;
}

public static class Builder extends AbstractFieldMapper.OpenBuilder<Builder, StringFieldMapper> {

protected String nullValue = Defaults.NULL_VALUE;

protected int positionOffsetGap = Defaults.POSITION_OFFSET_GAP;

public Builder(String name) {
super(name);
builder = this;
Expand All @@ -66,11 +71,20 @@ public Builder includeInAll(Boolean includeInAll) {
return this;
}

public Builder positionOffsetGap(int positionOffsetGap) {
this.positionOffsetGap = positionOffsetGap;
return this;
}

@Override
public StringFieldMapper build(BuilderContext context) {
if (positionOffsetGap > 0) {
indexAnalyzer = new NamedCustomAnalyzer(indexAnalyzer, positionOffsetGap);
searchAnalyzer = new NamedCustomAnalyzer(searchAnalyzer, positionOffsetGap);
}
StringFieldMapper fieldMapper = new StringFieldMapper(buildNames(context),
index, store, termVector, boost, omitNorms, omitTermFreqAndPositions, nullValue,
indexAnalyzer, searchAnalyzer);
indexAnalyzer, searchAnalyzer, positionOffsetGap);
fieldMapper.includeInAll(includeInAll);
return fieldMapper;
}
Expand All @@ -86,6 +100,16 @@ public Mapper.Builder parse(String name, Map<String, Object> node, ParserContext
Object propNode = entry.getValue();
if (propName.equals("null_value")) {
builder.nullValue(propNode.toString());
} else if (propName.equals("position_offset_gap")) {
builder.positionOffsetGap(XContentMapValues.nodeIntegerValue(propNode, -1));
// we need to update to actual analyzers if they are not set in this case...
// so we can inject the position offset gap...
if (builder.indexAnalyzer == null) {
builder.indexAnalyzer = parserContext.analysisService().defaultIndexAnalyzer();
}
if (builder.searchAnalyzer == null) {
builder.searchAnalyzer = parserContext.analysisService().defaultSearchAnalyzer();
}
}
}
return builder;
Expand All @@ -96,11 +120,20 @@ public Mapper.Builder parse(String name, Map<String, Object> node, ParserContext

private Boolean includeInAll;

private int positionOffsetGap;

protected StringFieldMapper(Names names, Field.Index index, Field.Store store, Field.TermVector termVector,
float boost, boolean omitNorms, boolean omitTermFreqAndPositions,
String nullValue, NamedAnalyzer indexAnalyzer, NamedAnalyzer searchAnalyzer) {
this(names, index, store, termVector, boost, omitNorms, omitTermFreqAndPositions, nullValue, indexAnalyzer, searchAnalyzer, Defaults.POSITION_OFFSET_GAP);
}

protected StringFieldMapper(Names names, Field.Index index, Field.Store store, Field.TermVector termVector,
float boost, boolean omitNorms, boolean omitTermFreqAndPositions,
String nullValue, NamedAnalyzer indexAnalyzer, NamedAnalyzer searchAnalyzer, int positionOffsetGap) {
super(names, index, store, termVector, boost, omitNorms, omitTermFreqAndPositions, indexAnalyzer, searchAnalyzer);
this.nullValue = nullValue;
this.positionOffsetGap = positionOffsetGap;
}

@Override
Expand Down Expand Up @@ -142,6 +175,10 @@ protected boolean customBoost() {
return true;
}

public int getPositionOffsetGap() {
return this.positionOffsetGap;
}

@Override
protected Field parseCreateField(ParseContext context) throws IOException {
String value = nullValue;
Expand Down Expand Up @@ -226,5 +263,8 @@ protected void doXContentBody(XContentBuilder builder) throws IOException {
if (includeInAll != null) {
builder.field("include_in_all", includeInAll);
}
if (positionOffsetGap != Defaults.POSITION_OFFSET_GAP) {
builder.field("position_offset_gap", positionOffsetGap);
}
}
}

0 comments on commit 8a2fbe0

Please sign in to comment.