use of org.opensearch.index.mapper.ParseContext.Document in project OpenSearch by opensearch-project.
the class TranslogTests method testTranslogOpSerialization.
public void testTranslogOpSerialization() throws Exception {
BytesReference B_1 = new BytesArray(new byte[] { 1 });
SeqNoFieldMapper.SequenceIDFields seqID = SeqNoFieldMapper.SequenceIDFields.emptySeqID();
long randomSeqNum = randomNonNegativeLong();
long randomPrimaryTerm = randomBoolean() ? 0 : randomNonNegativeLong();
seqID.seqNo.setLongValue(randomSeqNum);
seqID.seqNoDocValue.setLongValue(randomSeqNum);
seqID.primaryTerm.setLongValue(randomPrimaryTerm);
Field idField = new Field("_id", Uid.encodeId("1"), IdFieldMapper.Defaults.FIELD_TYPE);
Field versionField = new NumericDocValuesField("_version", 1);
Document document = new Document();
document.add(new TextField("value", "test", Field.Store.YES));
document.add(idField);
document.add(versionField);
document.add(seqID.seqNo);
document.add(seqID.seqNoDocValue);
document.add(seqID.primaryTerm);
ParsedDocument doc = new ParsedDocument(versionField, seqID, "1", "type", null, Arrays.asList(document), B_1, XContentType.JSON, null);
Engine.Index eIndex = new Engine.Index(newUid(doc), doc, randomSeqNum, randomPrimaryTerm, 1, VersionType.INTERNAL, Origin.PRIMARY, 0, 0, false, SequenceNumbers.UNASSIGNED_SEQ_NO, 0);
Engine.IndexResult eIndexResult = new Engine.IndexResult(1, randomPrimaryTerm, randomSeqNum, true);
Translog.Index index = new Translog.Index(eIndex, eIndexResult);
Version wireVersion = VersionUtils.randomVersionBetween(random(), Version.CURRENT.minimumCompatibilityVersion(), Version.CURRENT);
BytesStreamOutput out = new BytesStreamOutput();
out.setVersion(wireVersion);
Translog.Operation.writeOperation(out, index);
StreamInput in = out.bytes().streamInput();
in.setVersion(wireVersion);
Translog.Index serializedIndex = (Translog.Index) Translog.Operation.readOperation(in);
assertEquals(index, serializedIndex);
Engine.Delete eDelete = new Engine.Delete(doc.type(), doc.id(), newUid(doc), randomSeqNum, randomPrimaryTerm, 2, VersionType.INTERNAL, Origin.PRIMARY, 0, SequenceNumbers.UNASSIGNED_SEQ_NO, 0);
Engine.DeleteResult eDeleteResult = new Engine.DeleteResult(2, randomPrimaryTerm, randomSeqNum, true);
Translog.Delete delete = new Translog.Delete(eDelete, eDeleteResult);
out = new BytesStreamOutput();
out.setVersion(wireVersion);
Translog.Operation.writeOperation(out, delete);
in = out.bytes().streamInput();
in.setVersion(wireVersion);
Translog.Delete serializedDelete = (Translog.Delete) Translog.Operation.readOperation(in);
assertEquals(delete, serializedDelete);
}
use of org.opensearch.index.mapper.ParseContext.Document in project OpenSearch by opensearch-project.
the class SeqNoFieldMapper method postParse.
@Override
public void postParse(ParseContext context) throws IOException {
// In the case of nested docs, let's fill nested docs with the original
// so that Lucene doesn't write a Bitset for documents that
// don't have the field. This is consistent with the default value
// for efficiency.
// we share the parent docs fields to ensure good compression
SequenceIDFields seqID = context.seqID();
assert seqID != null;
for (Document doc : context.nonRootDocuments()) {
doc.add(seqID.seqNo);
doc.add(seqID.seqNoDocValue);
}
}
use of org.opensearch.index.mapper.ParseContext.Document in project OpenSearch by opensearch-project.
the class DataStreamFieldMapper method postParse.
@Override
public void postParse(ParseContext context) throws IOException {
// If _data_stream_timestamp metadata mapping is disabled, then skip all the remaining checks.
if (enabled == false) {
return;
}
// It is expected that the timestamp field will be parsed by the DateFieldMapper during the parseCreateField step.
// The parsed field will be added to the document as:
// 1. LongPoint (indexed = true; an indexed long field to allow fast range filters on the timestamp field value)
// 2. SortedNumericDocValuesField (hasDocValues = true; allows sorting, aggregations and access to the timestamp field value)
Document document = context.doc();
IndexableField[] fields = document.getFields(timestampField.getName());
// Documents must contain exactly one value for the timestamp field.
long numTimestampValues = Arrays.stream(fields).filter(field -> field.fieldType().docValuesType() == DocValuesType.SORTED_NUMERIC).count();
if (numTimestampValues != 1) {
throw new IllegalArgumentException("documents must contain a single-valued timestamp field '" + timestampField.getName() + "' of date type");
}
}
use of org.opensearch.index.mapper.ParseContext.Document in project OpenSearch by opensearch-project.
the class BooleanFieldMapperTests method testDocValues.
public void testDocValues() throws Exception {
DocumentMapper defaultMapper = createDocumentMapper(mapping(b -> {
b.startObject("bool1").field("type", "boolean").endObject();
b.startObject("bool2");
{
b.field("type", "boolean");
b.field("index", false);
}
b.endObject();
b.startObject("bool3");
{
b.field("type", "boolean");
b.field("index", true);
}
b.endObject();
}));
ParsedDocument parsedDoc = defaultMapper.parse(source(b -> {
b.field("bool1", true);
b.field("bool2", true);
b.field("bool3", true);
}));
Document doc = parsedDoc.rootDoc();
IndexableField[] fields = doc.getFields("bool1");
assertEquals(2, fields.length);
assertEquals(DocValuesType.NONE, fields[0].fieldType().docValuesType());
assertEquals(DocValuesType.SORTED_NUMERIC, fields[1].fieldType().docValuesType());
fields = doc.getFields("bool2");
assertEquals(1, fields.length);
assertEquals(DocValuesType.SORTED_NUMERIC, fields[0].fieldType().docValuesType());
fields = doc.getFields("bool3");
assertEquals(DocValuesType.NONE, fields[0].fieldType().docValuesType());
assertEquals(DocValuesType.SORTED_NUMERIC, fields[1].fieldType().docValuesType());
}
use of org.opensearch.index.mapper.ParseContext.Document in project OpenSearch by opensearch-project.
the class DateFieldTypeTests method isFieldWithinRangeTestCase.
public void isFieldWithinRangeTestCase(DateFieldType ft) throws IOException {
Directory dir = newDirectory();
IndexWriter w = new IndexWriter(dir, new IndexWriterConfig(null));
Document doc = new Document();
LongPoint field = new LongPoint("my_date", ft.parse("2015-10-12"));
doc.add(field);
w.addDocument(doc);
field.setLongValue(ft.parse("2016-04-03"));
w.addDocument(doc);
DirectoryReader reader = DirectoryReader.open(w);
DateMathParser alternateFormat = DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.toDateMathParser();
doTestIsFieldWithinQuery(ft, reader, null, null);
doTestIsFieldWithinQuery(ft, reader, null, alternateFormat);
doTestIsFieldWithinQuery(ft, reader, DateTimeZone.UTC, null);
doTestIsFieldWithinQuery(ft, reader, DateTimeZone.UTC, alternateFormat);
QueryRewriteContext context = new QueryRewriteContext(xContentRegistry(), writableRegistry(), null, () -> nowInMillis);
// Fields with no value indexed.
DateFieldType ft2 = new DateFieldType("my_date2");
assertEquals(Relation.DISJOINT, ft2.isFieldWithinQuery(reader, "2015-10-09", "2016-01-02", false, false, null, null, context));
IOUtils.close(reader, w, dir);
}
Aggregations