diff --git a/src/main/java/org/elasticsearch/index/mapper/DocumentMapper.java b/src/main/java/org/elasticsearch/index/mapper/DocumentMapper.java index 0935ad41926..6805acf3269 100644 --- a/src/main/java/org/elasticsearch/index/mapper/DocumentMapper.java +++ b/src/main/java/org/elasticsearch/index/mapper/DocumentMapper.java @@ -25,6 +25,7 @@ import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import org.apache.lucene.document.FieldType; +import org.apache.lucene.index.IndexableField; import org.apache.lucene.search.Filter; import org.elasticsearch.common.Booleans; import org.elasticsearch.common.Nullable; @@ -519,6 +520,17 @@ public class DocumentMapper implements ToXContent { if (context.docs().size() > 1) { Collections.reverse(context.docs()); } + // apply doc boost + if (context.docBoost() != 1.0f) { + for (Document doc : context.docs()) { + for (IndexableField field : doc) { + if (field.fieldType().indexed() && !field.fieldType().omitNorms()) { + ((Field) field).setBoost(context.docBoost() * field.boost()); + } + } + } + } + ParsedDocument doc = new ParsedDocument(context.uid(), context.id(), context.type(), source.routing(), source.timestamp(), source.ttl(), context.docs(), context.analyzer(), context.source(), context.mappingsModified()).parent(source.parent()); // reset the context to free up memory diff --git a/src/main/java/org/elasticsearch/index/mapper/ParseContext.java b/src/main/java/org/elasticsearch/index/mapper/ParseContext.java index c6a70a0a880..af85557a631 100644 --- a/src/main/java/org/elasticsearch/index/mapper/ParseContext.java +++ b/src/main/java/org/elasticsearch/index/mapper/ParseContext.java @@ -21,7 +21,6 @@ package org.elasticsearch.index.mapper; import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.document.Document; -import org.apache.lucene.document.Field; import org.elasticsearch.common.Nullable; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.lucene.all.AllEntries; @@ -80,6 +79,8 @@ public class ParseContext { private AllEntries allEntries = new AllEntries(); + private float docBoost = 1.0f; + public ParseContext(String index, @Nullable Settings indexSettings, DocumentMapperParser docMapperParser, DocumentMapper docMapper, ContentPath path) { this.index = index; this.indexSettings = indexSettings; @@ -107,6 +108,7 @@ public class ParseContext { this.listener = listener == null ? DocumentMapper.ParseListener.EMPTY : listener; this.allEntries = new AllEntries(); this.ignoredValues.clear(); + this.docBoost = 1.0f; } public boolean flyweight() { @@ -273,6 +275,14 @@ public class ParseContext { return externalValue; } + public float docBoost() { + return this.docBoost; + } + + public void docBoost(float docBoost) { + this.docBoost = docBoost; + } + /** * A string builder that can be used to construct complex names for example. * Its better to reuse the. diff --git a/src/main/java/org/elasticsearch/index/mapper/internal/BoostFieldMapper.java b/src/main/java/org/elasticsearch/index/mapper/internal/BoostFieldMapper.java index f2c99d8c1aa..de41e5e7a63 100644 --- a/src/main/java/org/elasticsearch/index/mapper/internal/BoostFieldMapper.java +++ b/src/main/java/org/elasticsearch/index/mapper/internal/BoostFieldMapper.java @@ -228,7 +228,7 @@ public class BoostFieldMapper extends NumberFieldMapper implements Intern // we override parse since we want to handle cases where it is not indexed and not stored (the default) float value = parseFloatValue(context); if (!Float.isNaN(value)) { - context.doc().setBoost(value); + context.docBoost(value); } super.parse(context); } @@ -239,7 +239,7 @@ public class BoostFieldMapper extends NumberFieldMapper implements Intern if (Float.isNaN(value)) { return null; } - context.doc().setBoost(value); + context.docBoost(value); return new FloatFieldMapper.CustomFloatNumericField(this, value, fieldType); }