lucene 4: Upgraded BoostMappingTests + SimpleMapperTests

This commit is contained in:
Martijn van Groningen 2012-11-02 15:48:27 +01:00 committed by Shay Banon
parent cefe2ba870
commit 5c0ef796e8
2 changed files with 38 additions and 9 deletions

View File

@ -19,6 +19,7 @@
package org.elasticsearch.test.unit.index.mapper.boost;
import org.apache.lucene.index.IndexableField;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.mapper.DocumentMapper;
import org.elasticsearch.index.mapper.ParsedDocument;
@ -41,8 +42,16 @@ public class BoostMappingTests {
ParsedDocument doc = mapper.parse("type", "1", XContentFactory.jsonBuilder().startObject()
.field("_boost", 2.0f)
.field("field", "a")
.field("field", "b")
.endObject().bytes());
assertThat(doc.rootDoc().getBoost(), equalTo(2.0f));
assertThat(doc.rootDoc().getFields().size(), equalTo(2));
float sum = 0.0f;
for (IndexableField field : doc.rootDoc().getFields()) {
sum += field.boost();
}
assertThat(3.0f, equalTo(sum)); // 2.0 (for first field) + 1.0 (for second field)
}
@Test
@ -54,13 +63,22 @@ public class BoostMappingTests {
DocumentMapper mapper = MapperTests.newParser().parse(mapping);
ParsedDocument doc = mapper.parse("type", "1", XContentFactory.jsonBuilder().startObject()
.field("field", "a")
.field("_boost", 2.0f)
.endObject().bytes());
assertThat(doc.rootDoc().getBoost(), equalTo(1.0f));
assertThat(doc.rootDoc().getFields().size(), equalTo(1));
for (IndexableField field : doc.rootDoc().getFields()) {
assertThat(field.boost(), equalTo(1.0f));
}
doc = mapper.parse("type", "1", XContentFactory.jsonBuilder().startObject()
.field("field", "a")
.field("custom_boost", 2.0f)
.endObject().bytes());
assertThat(doc.rootDoc().getBoost(), equalTo(2.0f));
assertThat(doc.rootDoc().getFields().size(), equalTo(1));
for (IndexableField field : doc.rootDoc().getFields()) {
assertThat(field.boost(), equalTo(2.0f));
}
}
}

View File

@ -20,7 +20,7 @@
package org.elasticsearch.test.unit.index.mapper.simple;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexableField;
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.index.mapper.DocumentMapper;
@ -29,7 +29,6 @@ import org.elasticsearch.index.mapper.Uid;
import org.elasticsearch.test.unit.index.mapper.MapperTests;
import org.testng.annotations.Test;
import static org.apache.lucene.document.Field.Store.YES;
import static org.elasticsearch.common.io.Streams.copyToBytesFromClasspath;
import static org.elasticsearch.common.io.Streams.copyToStringFromClasspath;
import static org.elasticsearch.index.mapper.MapperBuilders.*;
@ -53,7 +52,10 @@ public class SimpleMapperTests {
BytesReference json = new BytesArray(copyToBytesFromClasspath("/org/elasticsearch/test/unit/index/mapper/simple/test1.json"));
Document doc = docMapper.parse("person", "1", json).rootDoc();
assertThat((double) doc.getBoost(), closeTo(3.7, 0.01));
assertThat(doc.getFields().size(), equalTo(14));
for (IndexableField field : doc.getFields()) {
assertThat((double) field.boost(), closeTo(3.7, 0.01));
}
assertThat(doc.get(docMapper.mappers().name("first").mapper().names().indexName()), equalTo("shay"));
assertThat(docMapper.mappers().name("first").mapper().names().fullName(), equalTo("name.first"));
// System.out.println("Document: " + doc);
@ -74,7 +76,10 @@ public class SimpleMapperTests {
BytesReference json = new BytesArray(copyToBytesFromClasspath("/org/elasticsearch/test/unit/index/mapper/simple/test1.json"));
Document doc = builtDocMapper.parse(json).rootDoc();
assertThat(doc.get(docMapper.uidMapper().names().indexName()), equalTo(Uid.createUid("person", "1")));
assertThat((double) doc.getBoost(), closeTo(3.7, 0.01));
assertThat(doc.getFields().size(), equalTo(14));
for (IndexableField field : doc.getFields()) {
assertThat((double) field.boost(), closeTo(3.7, 0.01));
}
assertThat(doc.get(docMapper.mappers().name("first").mapper().names().indexName()), equalTo("shay"));
// System.out.println("Document: " + doc);
// System.out.println("Json: " + docMapper.sourceMapper().value(doc));
@ -90,7 +95,10 @@ public class SimpleMapperTests {
BytesReference json = new BytesArray(copyToBytesFromClasspath("/org/elasticsearch/test/unit/index/mapper/simple/test1.json"));
Document doc = docMapper.parse(json).rootDoc();
assertThat(doc.get(docMapper.uidMapper().names().indexName()), equalTo(Uid.createUid("person", "1")));
assertThat((double) doc.getBoost(), closeTo(3.7, 0.01));
assertThat(doc.getFields().size(), equalTo(14));
for (IndexableField field : doc.getFields()) {
assertThat((double) field.boost(), closeTo(3.7, 0.01));
}
assertThat(doc.get(docMapper.mappers().name("first").mapper().names().indexName()), equalTo("shay"));
// System.out.println("Document: " + doc);
// System.out.println("Json: " + docMapper.sourceMapper().value(doc));
@ -103,7 +111,10 @@ public class SimpleMapperTests {
BytesReference json = new BytesArray(copyToBytesFromClasspath("/org/elasticsearch/test/unit/index/mapper/simple/test1-notype-noid.json"));
Document doc = docMapper.parse("person", "1", json).rootDoc();
assertThat(doc.get(docMapper.uidMapper().names().indexName()), equalTo(Uid.createUid("person", "1")));
assertThat((double) doc.getBoost(), closeTo(3.7, 0.01));
assertThat(doc.getFields().size(), equalTo(14));
for (IndexableField field : doc.getFields()) {
assertThat((double) field.boost(), closeTo(3.7, 0.01));
}
assertThat(doc.get(docMapper.mappers().name("first").mapper().names().indexName()), equalTo("shay"));
// System.out.println("Document: " + doc);
// System.out.println("Json: " + docMapper.sourceMapper().value(doc));