use of org.elasticsearch.index.mapper.ParseContext.Document in project elasticsearch by elastic.
the class InternalEngineTests method testSequenceIDs.
public void testSequenceIDs() throws Exception {
Tuple<Long, Long> seqID = getSequenceID(engine, new Engine.Get(false, newUid("1")));
// Non-existent doc returns no seqnum and no primary term
assertThat(seqID.v1(), equalTo(SequenceNumbersService.UNASSIGNED_SEQ_NO));
assertThat(seqID.v2(), equalTo(0L));
// create a document
Document document = testDocumentWithTextField();
document.add(new Field(SourceFieldMapper.NAME, BytesReference.toBytes(B_1), SourceFieldMapper.Defaults.FIELD_TYPE));
ParsedDocument doc = testParsedDocument("1", "test", null, document, B_1, null);
engine.index(indexForDoc(doc));
engine.refresh("test");
seqID = getSequenceID(engine, new Engine.Get(false, newUid(doc)));
logger.info("--> got seqID: {}", seqID);
assertThat(seqID.v1(), equalTo(0L));
assertThat(seqID.v2(), equalTo(0L));
// Index the same document again
document = testDocumentWithTextField();
document.add(new Field(SourceFieldMapper.NAME, BytesReference.toBytes(B_1), SourceFieldMapper.Defaults.FIELD_TYPE));
doc = testParsedDocument("1", "test", null, document, B_1, null);
engine.index(indexForDoc(doc));
engine.refresh("test");
seqID = getSequenceID(engine, new Engine.Get(false, newUid(doc)));
logger.info("--> got seqID: {}", seqID);
assertThat(seqID.v1(), equalTo(1L));
assertThat(seqID.v2(), equalTo(0L));
// Index the same document for the third time, this time changing the primary term
document = testDocumentWithTextField();
document.add(new Field(SourceFieldMapper.NAME, BytesReference.toBytes(B_1), SourceFieldMapper.Defaults.FIELD_TYPE));
doc = testParsedDocument("1", "test", null, document, B_1, null);
engine.index(new Engine.Index(newUid(doc), doc, SequenceNumbersService.UNASSIGNED_SEQ_NO, 1, Versions.MATCH_ANY, VersionType.INTERNAL, Engine.Operation.Origin.PRIMARY, System.nanoTime(), -1, false));
engine.refresh("test");
seqID = getSequenceID(engine, new Engine.Get(false, newUid(doc)));
logger.info("--> got seqID: {}", seqID);
assertThat(seqID.v1(), equalTo(2L));
assertThat(seqID.v2(), equalTo(1L));
// we can query by the _seq_no
Engine.Searcher searchResult = engine.acquireSearcher("test");
MatcherAssert.assertThat(searchResult, EngineSearcherTotalHitsMatcher.engineSearcherTotalHits(1));
MatcherAssert.assertThat(searchResult, EngineSearcherTotalHitsMatcher.engineSearcherTotalHits(LongPoint.newExactQuery("_seq_no", 2), 1));
searchResult.close();
}
use of org.elasticsearch.index.mapper.ParseContext.Document in project elasticsearch by elastic.
the class BooleanFieldMapperTests method testParsesPreEs6BooleansLenient.
public void testParsesPreEs6BooleansLenient() throws IOException {
String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties").startObject("field1").field("type", "boolean").endObject().startObject("field2").field("type", "boolean").endObject().endObject().endObject().endObject().string();
DocumentMapper defaultMapper = preEs6Parser.parse("type", new CompressedXContent(mapping));
String falsy = randomFrom("false", "off", "no", "0");
String truthy = randomFrom("true", "on", "yes", "1");
ParsedDocument parsedDoc = defaultMapper.parse("legacy", "type", "1", XContentFactory.jsonBuilder().startObject().field("field1", falsy).field("field2", truthy).endObject().bytes());
Document doc = parsedDoc.rootDoc();
assertEquals("F", doc.getField("field1").stringValue());
assertEquals("T", doc.getField("field2").stringValue());
List<String> expectedDeprecationWarnings = new ArrayList<>();
if (Booleans.isBoolean(falsy) == false) {
expectedDeprecationWarnings.add("Expected a boolean for property [field1] but got [" + falsy + "]");
}
if (Booleans.isBoolean(truthy) == false) {
expectedDeprecationWarnings.add("Expected a boolean for property [field2] but got [" + truthy + "]");
}
if (expectedDeprecationWarnings.isEmpty() == false) {
assertWarnings(expectedDeprecationWarnings.toArray(new String[1]));
}
}
use of org.elasticsearch.index.mapper.ParseContext.Document in project elasticsearch by elastic.
the class DocumentParserTests method testParseToJsonAndParse.
public void testParseToJsonAndParse() throws Exception {
String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/simple/test-mapping.json");
DocumentMapperParser parser = createIndex("test").mapperService().documentMapperParser();
DocumentMapper docMapper = parser.parse("person", new CompressedXContent(mapping));
String builtMapping = docMapper.mappingSource().string();
// reparse it
DocumentMapper builtDocMapper = parser.parse("person", new CompressedXContent(builtMapping));
BytesReference json = new BytesArray(copyToBytesFromClasspath("/org/elasticsearch/index/mapper/simple/test1.json"));
Document doc = builtDocMapper.parse("test", "person", "1", json).rootDoc();
assertThat(doc.get(docMapper.uidMapper().fieldType().name()), equalTo(Uid.createUid("person", "1")));
assertThat(doc.get(docMapper.mappers().getMapper("name.first").fieldType().name()), equalTo("shay"));
}
use of org.elasticsearch.index.mapper.ParseContext.Document in project elasticsearch by elastic.
the class CopyToMapperTests method testCopyToNestedField.
public void testCopyToNestedField() throws Exception {
IndexService indexService = createIndex("test");
DocumentMapperParser parser = indexService.mapperService().documentMapperParser();
XContentBuilder mapping = jsonBuilder().startObject().startObject("type").startObject("properties").startObject("target").field("type", "long").field("doc_values", false).endObject().startObject("n1").field("type", "nested").startObject("properties").startObject("target").field("type", "long").field("doc_values", false).endObject().startObject("n2").field("type", "nested").startObject("properties").startObject("target").field("type", "long").field("doc_values", false).endObject().startObject("source").field("type", "long").field("doc_values", false).startArray("copy_to").value(// should go to the root doc
"target").value(// should go to the parent doc
"n1.target").value(// should go to the current doc
"n1.n2.target").endArray().endObject().endObject().endObject().endObject().endObject().endObject().endObject().endObject();
DocumentMapper mapper = parser.parse("type", new CompressedXContent(mapping.string()));
XContentBuilder jsonDoc = XContentFactory.jsonBuilder().startObject().startArray("n1").startObject().startArray("n2").startObject().field("source", 3).endObject().startObject().field("source", 5).endObject().endArray().endObject().startObject().startArray("n2").startObject().field("source", 7).endObject().endArray().endObject().endArray().endObject();
ParsedDocument doc = mapper.parse("test", "type", "1", jsonDoc.bytes());
assertEquals(6, doc.docs().size());
Document nested = doc.docs().get(0);
assertFieldValue(nested, "n1.n2.target", 7L);
assertFieldValue(nested, "n1.target");
assertFieldValue(nested, "target");
nested = doc.docs().get(2);
assertFieldValue(nested, "n1.n2.target", 5L);
assertFieldValue(nested, "n1.target");
assertFieldValue(nested, "target");
nested = doc.docs().get(3);
assertFieldValue(nested, "n1.n2.target", 3L);
assertFieldValue(nested, "n1.target");
assertFieldValue(nested, "target");
Document parent = doc.docs().get(1);
assertFieldValue(parent, "target");
assertFieldValue(parent, "n1.target", 7L);
assertFieldValue(parent, "n1.n2.target");
parent = doc.docs().get(4);
assertFieldValue(parent, "target");
assertFieldValue(parent, "n1.target", 3L, 5L);
assertFieldValue(parent, "n1.n2.target");
Document root = doc.docs().get(5);
assertFieldValue(root, "target", 3L, 5L, 7L);
assertFieldValue(root, "n1.target");
assertFieldValue(root, "n1.n2.target");
}
use of org.elasticsearch.index.mapper.ParseContext.Document in project elasticsearch by elastic.
the class DocumentParserTests method testSimpleParser.
public void testSimpleParser() throws Exception {
String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/simple/test-mapping.json");
DocumentMapper docMapper = createIndex("test").mapperService().documentMapperParser().parse("person", new CompressedXContent(mapping));
assertThat((String) docMapper.meta().get("param1"), equalTo("value1"));
BytesReference json = new BytesArray(copyToBytesFromClasspath("/org/elasticsearch/index/mapper/simple/test1.json"));
Document doc = docMapper.parse("test", "person", "1", json).rootDoc();
assertThat(doc.get(docMapper.uidMapper().fieldType().name()), equalTo(Uid.createUid("person", "1")));
assertThat(doc.get(docMapper.mappers().getMapper("name.first").fieldType().name()), equalTo("shay"));
}
Aggregations