Allow the `_boost` field to be indexed and stored.

Closes #3752
This commit is contained in:
Brusic 2013-09-20 18:58:57 +02:00 committed by Martijn van Groningen
parent a1185a93d0
commit ab05f929fe
3 changed files with 34 additions and 1 deletions

View File

@ -412,6 +412,10 @@ public class DocumentMapper implements ToXContent {
return rootMapper(SizeFieldMapper.class);
}
public BoostFieldMapper boostFieldMapper() {
return rootMapper(BoostFieldMapper.class);
}
public Analyzer indexAnalyzer() {
return this.indexAnalyzer;
}

View File

@ -277,7 +277,9 @@ public class BoostFieldMapper extends NumberFieldMapper<Float> implements Intern
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
// all are defaults, don't write it at all
if (name().equals(Defaults.NAME) && nullValue == null) {
if (name().equals(Defaults.NAME) && nullValue == null &&
fieldType.indexed() == Defaults.FIELD_TYPE.indexed() &&
fieldType.stored() == Defaults.FIELD_TYPE.stored()) {
return builder;
}
builder.startObject(contentType());
@ -286,6 +288,12 @@ public class BoostFieldMapper extends NumberFieldMapper<Float> implements Intern
}
if (nullValue != null) {
builder.field("null_value", nullValue);
}
if (fieldType.indexed() != Defaults.FIELD_TYPE.indexed()) {
builder.field("index", fieldType.indexed());
}
if (fieldType.stored() != Defaults.FIELD_TYPE.stored()) {
builder.field("store", fieldType.stored());
}
builder.endObject();
return builder;

View File

@ -23,6 +23,7 @@ import org.apache.lucene.index.IndexableField;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.mapper.DocumentMapper;
import org.elasticsearch.index.mapper.ParsedDocument;
import org.elasticsearch.index.mapper.internal.BoostFieldMapper;
import org.elasticsearch.index.mapper.MapperTestUtils;
import org.junit.Test;
@ -71,4 +72,24 @@ public class BoostMappingTests {
.endObject().bytes());
assertThat(doc.rootDoc().getField("field").boost(), equalTo(2.0f));
}
@Test
public void testDefaultValues() throws Exception {
String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").endObject().string();
DocumentMapper docMapper = MapperTestUtils.newParser().parse(mapping);
assertThat(docMapper.boostFieldMapper().fieldType().stored(), equalTo(BoostFieldMapper.Defaults.FIELD_TYPE.stored()));
assertThat(docMapper.boostFieldMapper().fieldType().indexed(), equalTo(BoostFieldMapper.Defaults.FIELD_TYPE.indexed()));
}
@Test
public void testSetValues() throws Exception {
String mapping = XContentFactory.jsonBuilder().startObject().startObject("type")
.startObject("_boost")
.field("store", "yes").field("index", "not_analyzed")
.endObject()
.endObject().endObject().string();
DocumentMapper docMapper = MapperTestUtils.newParser().parse(mapping);
assertThat(docMapper.boostFieldMapper().fieldType().stored(), equalTo(true));
assertThat(docMapper.boostFieldMapper().fieldType().indexed(), equalTo(true));
}
}