Skip to content

Commit

Permalink
Remove support for boost in copy_to field
Browse files Browse the repository at this point in the history
Currently, boosting on `copy_to` is misleading and does not work as originally specified in #4520. Instead of boosting just the terms from the origin field, it boosts the whole destination field.  If two fields copy_to a third field, one with a boost of 2 and another with a boost of 3, all the terms in the third field end up with a boost of 6.  This was not the intention.

  The alternative: to store the boost in a payload for every term, results in poor performance and inflexibility. Instead, users should either (1) query the common field AND the field that requires boosting, or (2) the multi_match query will soon be able to perform term-centric cross-field matching that will allow per-field boosting at query time (coming in 1.1).
  • Loading branch information
imotov committed Jan 31, 2014
1 parent 767c2e9 commit ee65026
Show file tree
Hide file tree
Showing 16 changed files with 44 additions and 142 deletions.
23 changes: 2 additions & 21 deletions docs/reference/mapping/types/core-types.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -637,33 +637,14 @@ the parameter. In the following example all values from fields `title` and `abst
}
--------------------------------------------------

Multiple fields with optional boost factors are also supported:
Multiple fields are also supported:

[source,js]
--------------------------------------------------
{
"book" : {
"properties" : {
"title" : { "type" : "string", "copy_to" : ["meta_data", "article_info^10.0"] },
}
}
--------------------------------------------------

The same mapping can be also defined using extended syntax:

[source,js]
--------------------------------------------------
{
"book" : {
"properties" : {
"title" : {
"type" : "string",
"copy_to" : [{
"field": "meta_data"
}, {
"field": "article_info"
"boost": 10.0
}
"title" : { "type" : "string", "copy_to" : ["meta_data", "article_info"] },
}
}
--------------------------------------------------
Expand Down
14 changes: 1 addition & 13 deletions src/main/java/org/elasticsearch/index/mapper/ParseContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,6 @@ public BytesRef getBinaryValue(String name) {

private float docBoost = 1.0f;

private float copyToBoost = 1.0f;

public ParseContext(String index, @Nullable Settings indexSettings, DocumentMapperParser docMapperParser, DocumentMapper docMapper, ContentPath path) {
this.index = index;
this.indexSettings = indexSettings;
Expand Down Expand Up @@ -227,14 +225,12 @@ public boolean isWithinNewMapper() {
return withinNewMapper;
}

public void setWithinCopyTo(float copyToBoost) {
public void setWithinCopyTo() {
this.withinCopyTo = true;
this.copyToBoost = copyToBoost;
}

public void clearWithinCopyTo() {
this.withinCopyTo = false;
this.copyToBoost = 1.0f;
}

public boolean isWithinCopyTo() {
Expand Down Expand Up @@ -400,14 +396,6 @@ public Object externalValue() {
return externalValue;
}

public float fieldBoost(AbstractFieldMapper mapper) {
if (withinCopyTo) {
return copyToBoost;
} else {
return mapper.boost();
}
}

public float docBoost() {
return this.docBoost;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ public T addMultiField(Mapper.Builder mapperBuilder) {
multiFieldsBuilder.add(mapperBuilder);
return builder;
}

public T copyTo(CopyTo copyTo) {
this.copyTo = copyTo;
return builder;
Expand Down Expand Up @@ -283,7 +284,7 @@ protected AbstractFieldMapper(Names names, float boost, FieldType fieldType, Boo
this(names, boost, fieldType, docValues, indexAnalyzer, searchAnalyzer, postingsFormat, docValuesFormat, similarity,
normsLoading, fieldDataSettings, indexSettings, MultiFields.empty(), null);
}

protected AbstractFieldMapper(Names names, float boost, FieldType fieldType, Boolean docValues, NamedAnalyzer indexAnalyzer,
NamedAnalyzer searchAnalyzer, PostingsFormatProvider postingsFormat,
DocValuesFormatProvider docValuesFormat, SimilarityProvider similarity,
Expand Down Expand Up @@ -996,9 +997,9 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
*/
public static class CopyTo {

private final ImmutableList<CopyToField> copyToFields;
private final ImmutableList<String> copyToFields;

private CopyTo(ImmutableList<CopyToField> copyToFields) {
private CopyTo(ImmutableList<String> copyToFields) {
this.copyToFields = copyToFields;
}

Expand All @@ -1007,28 +1008,28 @@ private CopyTo(ImmutableList<CopyToField> copyToFields) {
*/
public void parse(ParseContext context) throws IOException {
if (!context.isWithinCopyTo()) {
for (CopyToField field : copyToFields) {
field.parse(context);
for (String field : copyToFields) {
parse(field, context);
}
}
}

public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
if (!copyToFields.isEmpty()) {
builder.startArray("copy_to");
for (CopyToField field : copyToFields) {
field.toXContent(builder, params);
for (String field : copyToFields) {
builder.value(field);
}
builder.endArray();
}
return builder;
}

public static class Builder {
private final ImmutableList.Builder<CopyToField> copyToBuilders = ImmutableList.builder();
private final ImmutableList.Builder<String> copyToBuilders = ImmutableList.builder();

public Builder add(String field, float boost) {
copyToBuilders.add(new CopyToField(field, boost));
public Builder add(String field) {
copyToBuilders.add(field);
return this;
}

Expand All @@ -1037,28 +1038,15 @@ public CopyTo build() {
}
}

public ImmutableList<CopyToField> copyToFields() {
public ImmutableList<String> copyToFields() {
return copyToFields;
}

}

public static class CopyToField {

private final String field;

protected final float boost;

public CopyToField(String field, float boost) {
this.field = field;
this.boost = boost;
}

/**
* Creates an copy of the current field with given field name and boost
*/
public void parse(ParseContext context) throws IOException {
context.setWithinCopyTo(boost);
public void parse(String field, ParseContext context) throws IOException {
context.setWithinCopyTo();
FieldMappers mappers = context.docMapper().mappers().indexName(field);
if (mappers != null && !mappers.isEmpty()) {
mappers.mapper().parse(context);
Expand Down Expand Up @@ -1115,23 +1103,7 @@ public void parse(ParseContext context) throws IOException {
context.clearWithinCopyTo();
}

public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
builder.field("field", field);
if (boost != 1.0 || params.paramAsBoolean("include_defaults", false)) {
builder.field("boost", boost);
}
builder.endObject();
return builder;
}

public String field() {
return field;
}

public float boost() {
return boost;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ protected boolean customBoost() {
@Override
protected void innerParseCreateField(ParseContext context, List<Field> fields) throws IOException {
byte value;
float boost = context.fieldBoost(this);
float boost = this.boost;
if (context.externalValueSet()) {
Object externalValue = context.externalValue();
if (externalValue == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ protected boolean customBoost() {
protected void innerParseCreateField(ParseContext context, List<Field> fields) throws IOException {
String dateAsString = null;
Long value = null;
float boost = context.fieldBoost(this);
float boost = this.boost;
if (context.externalValueSet()) {
Object externalValue = context.externalValue();
if (externalValue instanceof Number) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ protected boolean customBoost() {
@Override
protected void innerParseCreateField(ParseContext context, List<Field> fields) throws IOException {
double value;
float boost = context.fieldBoost(this);
float boost = this.boost;
if (context.externalValueSet()) {
Object externalValue = context.externalValue();
if (externalValue == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ protected boolean customBoost() {
@Override
protected void innerParseCreateField(ParseContext context, List<Field> fields) throws IOException {
float value;
float boost = context.fieldBoost(this);
float boost = this.boost;
if (context.externalValueSet()) {
Object externalValue = context.externalValue();
if (externalValue == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ protected boolean customBoost() {
@Override
protected void innerParseCreateField(ParseContext context, List<Field> fields) throws IOException {
int value;
float boost = context.fieldBoost(this);
float boost = this.boost;
if (context.externalValueSet()) {
Object externalValue = context.externalValue();
if (externalValue == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ protected boolean customBoost() {
@Override
protected void innerParseCreateField(ParseContext context, List<Field> fields) throws IOException {
long value;
float boost = context.fieldBoost(this);
float boost = this.boost;
if (context.externalValueSet()) {
Object externalValue = context.externalValue();
if (externalValue == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ protected boolean customBoost() {
@Override
protected void innerParseCreateField(ParseContext context, List<Field> fields) throws IOException {
short value;
float boost = context.fieldBoost(this);
float boost = this.boost;
if (context.externalValueSet()) {
Object externalValue = context.externalValue();
if (externalValue == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ public Filter nullValueFilter() {

@Override
protected void parseCreateField(ParseContext context, List<Field> fields) throws IOException {
ValueAndBoost valueAndBoost = parseCreateFieldForString(context, nullValue, context.fieldBoost(this));
ValueAndBoost valueAndBoost = parseCreateFieldForString(context, nullValue, boost);
if (valueAndBoost.value() == null) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ protected TokenCountFieldMapper(Names names, int precisionStep, float boost, Fie

@Override
protected void parseCreateField(ParseContext context, List<Field> fields) throws IOException {
ValueAndBoost valueAndBoost = StringFieldMapper.parseCreateFieldForString(context, null /* Out null value is an int so we convert*/, context.fieldBoost(this));
ValueAndBoost valueAndBoost = StringFieldMapper.parseCreateFieldForString(context, null /* Out null value is an int so we convert*/, boost);
if (valueAndBoost.value() == null && nullValue() == null) {
return;
}
Expand Down
37 changes: 5 additions & 32 deletions src/main/java/org/elasticsearch/index/mapper/core/TypeParsers.java
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ public static void parseField(AbstractFieldMapper.Builder builder, String name,
final Settings settings = ImmutableSettings.builder().put(SettingsLoader.Helper.loadNestedFromMap(nodeMapValue(propNode, "fielddata"))).build();
builder.fieldDataSettings(settings);
} else if (propName.equals("copy_to")) {
parseCopyFields(name, propNode, builder);
parseCopyFields(propNode, builder);
}
}
}
Expand Down Expand Up @@ -367,43 +367,16 @@ public static ContentPath.Type parsePathType(String name, String path) throws Ma
}

@SuppressWarnings("unchecked")
public static void parseCopyFields(String fieldName, Object propNode, AbstractFieldMapper.Builder builder) {
public static void parseCopyFields(Object propNode, AbstractFieldMapper.Builder builder) {
AbstractFieldMapper.CopyTo.Builder copyToBuilder = new AbstractFieldMapper.CopyTo.Builder();
if (isObject(propNode)) {
parseCopyToField(fieldName, propNode, copyToBuilder);
} else if (isArray(propNode)) {
if (isArray(propNode)) {
for(Object node : (List<Object>) propNode) {
if (isObject(node)) {
parseCopyToField(fieldName, node, copyToBuilder);
} else {
parseCopyToField(nodeStringValue(node, null), copyToBuilder);
}
copyToBuilder.add(nodeStringValue(node, null));
}
} else {
parseCopyToField(nodeStringValue(propNode, null), copyToBuilder);
copyToBuilder.add(nodeStringValue(propNode, null));
}
builder.copyTo(copyToBuilder.build());
}

public static void parseCopyToField(String fieldValue, AbstractFieldMapper.CopyTo.Builder builder) {
int carrotPos = fieldValue.indexOf('^');
if (carrotPos > 0) {
builder.add(fieldValue.substring(0, carrotPos), Float.parseFloat(fieldValue.substring(carrotPos + 1)));
} else {
builder.add(fieldValue, 1.0f);
}
}

public static void parseCopyToField(String fieldName, Object propNode, AbstractFieldMapper.CopyTo.Builder builder) {
Map<String, Object> copyToNode = (Map<String, Object>) propNode;
String field = nodeStringValue(copyToNode.get("field"), null);
if (field == null) {
throw new MapperParsingException("No type specified for property [" + fieldName + "]");
}
float boost = nodeFloatValue(copyToNode.get("boost"), 1.0f);
builder.add(field, boost);


}

}
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ public void parse(ParseContext context) throws IOException {
}
for (Field field : fields) {
if (!customBoost()) {
field.setBoost(context.fieldBoost(this));
field.setBoost(boost);
}
if (context.listener().beforeFieldAdded(this, field, context)) {
context.doc().add(field);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ protected void innerParseCreateField(ParseContext context, List<Field> fields) t
final long value = ipToLong(ipAsString);
if (fieldType.indexed() || fieldType.stored()) {
CustomLongNumericField field = new CustomLongNumericField(this, value, fieldType);
field.setBoost(context.fieldBoost(this));
field.setBoost(boost);
fields.add(field);
}
if (hasDocValues()) {
Expand Down

0 comments on commit ee65026

Please sign in to comment.