lucene 4: support doc level boost

This commit is contained in:
Shay Banon 2012-11-02 11:32:47 +01:00
parent b492320e2f
commit c60f20413b
3 changed files with 25 additions and 3 deletions

View File

@ -25,6 +25,7 @@ import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document; import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field; import org.apache.lucene.document.Field;
import org.apache.lucene.document.FieldType; import org.apache.lucene.document.FieldType;
import org.apache.lucene.index.IndexableField;
import org.apache.lucene.search.Filter; import org.apache.lucene.search.Filter;
import org.elasticsearch.common.Booleans; import org.elasticsearch.common.Booleans;
import org.elasticsearch.common.Nullable; import org.elasticsearch.common.Nullable;
@ -519,6 +520,17 @@ public class DocumentMapper implements ToXContent {
if (context.docs().size() > 1) { if (context.docs().size() > 1) {
Collections.reverse(context.docs()); 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(), 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()); context.source(), context.mappingsModified()).parent(source.parent());
// reset the context to free up memory // reset the context to free up memory

View File

@ -21,7 +21,6 @@ package org.elasticsearch.index.mapper;
import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document; import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.elasticsearch.common.Nullable; import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.lucene.all.AllEntries; import org.elasticsearch.common.lucene.all.AllEntries;
@ -80,6 +79,8 @@ public class ParseContext {
private AllEntries allEntries = new AllEntries(); private AllEntries allEntries = new AllEntries();
private float docBoost = 1.0f;
public ParseContext(String index, @Nullable Settings indexSettings, DocumentMapperParser docMapperParser, DocumentMapper docMapper, ContentPath path) { public ParseContext(String index, @Nullable Settings indexSettings, DocumentMapperParser docMapperParser, DocumentMapper docMapper, ContentPath path) {
this.index = index; this.index = index;
this.indexSettings = indexSettings; this.indexSettings = indexSettings;
@ -107,6 +108,7 @@ public class ParseContext {
this.listener = listener == null ? DocumentMapper.ParseListener.EMPTY : listener; this.listener = listener == null ? DocumentMapper.ParseListener.EMPTY : listener;
this.allEntries = new AllEntries(); this.allEntries = new AllEntries();
this.ignoredValues.clear(); this.ignoredValues.clear();
this.docBoost = 1.0f;
} }
public boolean flyweight() { public boolean flyweight() {
@ -273,6 +275,14 @@ public class ParseContext {
return externalValue; 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. * A string builder that can be used to construct complex names for example.
* Its better to reuse the. * Its better to reuse the.

View File

@ -228,7 +228,7 @@ public class BoostFieldMapper extends NumberFieldMapper<Float> implements Intern
// we override parse since we want to handle cases where it is not indexed and not stored (the default) // we override parse since we want to handle cases where it is not indexed and not stored (the default)
float value = parseFloatValue(context); float value = parseFloatValue(context);
if (!Float.isNaN(value)) { if (!Float.isNaN(value)) {
context.doc().setBoost(value); context.docBoost(value);
} }
super.parse(context); super.parse(context);
} }
@ -239,7 +239,7 @@ public class BoostFieldMapper extends NumberFieldMapper<Float> implements Intern
if (Float.isNaN(value)) { if (Float.isNaN(value)) {
return null; return null;
} }
context.doc().setBoost(value); context.docBoost(value);
return new FloatFieldMapper.CustomFloatNumericField(this, value, fieldType); return new FloatFieldMapper.CustomFloatNumericField(this, value, fieldType);
} }