Mapping: Document's Field level boosting, closes #920.

This commit is contained in:
kimchy 2011-05-10 23:27:38 +03:00
parent 15d8f0b1ac
commit 8c1171a6ef
3 changed files with 109 additions and 8 deletions

View File

@ -290,7 +290,9 @@ public abstract class AbstractFieldMapper<T> implements FieldMapper<T>, XContent
}
field.setOmitNorms(omitNorms);
field.setOmitTermFreqAndPositions(omitTermFreqAndPositions);
field.setBoost(boost);
if (!customBoost()) {
field.setBoost(boost);
}
if (context.listener().beforeFieldAdded(this, field, context)) {
context.doc().add(field);
}
@ -301,6 +303,13 @@ public abstract class AbstractFieldMapper<T> implements FieldMapper<T>, XContent
protected abstract Fieldable parseCreateField(ParseContext context) throws IOException;
/**
* Derived classes can override it to specify that boost value is set by derived classes.
*/
protected boolean customBoost() {
return false;
}
@Override public void traverse(FieldMapperListener fieldMapperListener) {
fieldMapperListener.fieldMapper(this);
}

View File

@ -132,18 +132,35 @@ public class StringFieldMapper extends AbstractFieldMapper<String> implements In
return value;
}
@Override protected boolean customBoost() {
return true;
}
@Override protected Field parseCreateField(ParseContext context) throws IOException {
String value;
String value = nullValue;
float boost = this.boost;
if (context.externalValueSet()) {
value = (String) context.externalValue();
if (value == null) {
value = nullValue;
}
} else {
if (context.parser().currentToken() == XContentParser.Token.VALUE_NULL) {
XContentParser parser = context.parser();
if (parser.currentToken() == XContentParser.Token.VALUE_NULL) {
value = nullValue;
} else if (parser.currentToken() == XContentParser.Token.START_OBJECT) {
XContentParser.Token token;
String currentFieldName = null;
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) {
currentFieldName = parser.currentName();
} else {
if ("value".equals(currentFieldName) || "_value".equals(currentFieldName)) {
value = parser.textOrNull();
} else if ("boost".equals(currentFieldName) || "_boost".equals(currentFieldName)) {
boost = parser.floatValue();
}
}
}
} else {
value = context.parser().text();
value = parser.textOrNull();
}
}
if (value == null) {
@ -156,7 +173,9 @@ public class StringFieldMapper extends AbstractFieldMapper<String> implements In
context.ignoredValue(names.indexName(), value);
return null;
}
return new Field(names.indexName(), false, value, store, index, termVector);
Field field = new Field(names.indexName(), false, value, store, index, termVector);
field.setBoost(boost);
return field;
}
@Override protected String contentType() {

View File

@ -0,0 +1,73 @@
/*
* Licensed to Elastic Search and Shay Banon under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. Elastic Search 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.mapper.xcontent.compound;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.mapper.ParsedDocument;
import org.elasticsearch.index.mapper.xcontent.MapperTests;
import org.elasticsearch.index.mapper.xcontent.XContentDocumentMapper;
import org.testng.annotations.Test;
import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;
@Test
public class CompoundTypesTests {
@Test public void testStringType() throws Exception {
String mapping = XContentFactory.jsonBuilder().startObject().startObject("type")
.startObject("properties")
.startObject("field1").field("type", "string").endObject()
.endObject()
.endObject().endObject().string();
XContentDocumentMapper defaultMapper = MapperTests.newParser().parse(mapping);
ParsedDocument doc = defaultMapper.parse("type", "1", XContentFactory.jsonBuilder()
.startObject()
.field("field1", "value1")
.field("field2", "value2")
.copiedBytes());
assertThat(doc.doc().get("field1"), equalTo("value1"));
assertThat((double) doc.doc().getFieldable("field1").getBoost(), closeTo(1.0d, 0.000001d));
assertThat(doc.doc().get("field2"), equalTo("value2"));
doc = defaultMapper.parse("type", "1", XContentFactory.jsonBuilder()
.startObject()
.startObject("field1").field("value", "value1").field("boost", 2.0f).endObject()
.field("field2", "value2")
.copiedBytes());
assertThat(doc.doc().get("field1"), equalTo("value1"));
assertThat((double) doc.doc().getFieldable("field1").getBoost(), closeTo(2.0d, 0.000001d));
assertThat(doc.doc().get("field2"), equalTo("value2"));
doc = defaultMapper.parse("type", "1", XContentFactory.jsonBuilder()
.startObject()
.field("field1", "value1")
.field("field2", "value2")
.copiedBytes());
assertThat(doc.doc().get("field1"), equalTo("value1"));
assertThat((double) doc.doc().getFieldable("field1").getBoost(), closeTo(1.0d, 0.000001d));
assertThat(doc.doc().get("field2"), equalTo("value2"));
}
}