Search in sources :

Example 1 with Document

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();
}
Also used : Searcher(org.elasticsearch.index.engine.Engine.Searcher) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) TextField(org.apache.lucene.document.TextField) IndexableField(org.apache.lucene.index.IndexableField) StoredField(org.apache.lucene.document.StoredField) Field(org.apache.lucene.document.Field) ParsedDocument(org.elasticsearch.index.mapper.ParsedDocument) AtomicLong(java.util.concurrent.atomic.AtomicLong) ParsedDocument(org.elasticsearch.index.mapper.ParsedDocument) Document(org.elasticsearch.index.mapper.ParseContext.Document)

Example 2 with Document

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]));
    }
}
Also used : CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) ArrayList(java.util.ArrayList) Matchers.containsString(org.hamcrest.Matchers.containsString) Document(org.elasticsearch.index.mapper.ParseContext.Document)

Example 3 with Document

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"));
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) BytesArray(org.elasticsearch.common.bytes.BytesArray) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) Matchers.containsString(org.hamcrest.Matchers.containsString) Document(org.elasticsearch.index.mapper.ParseContext.Document)

Example 4 with Document

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");
}
Also used : IndexService(org.elasticsearch.index.IndexService) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) Document(org.elasticsearch.index.mapper.ParseContext.Document) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 5 with Document

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"));
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) BytesArray(org.elasticsearch.common.bytes.BytesArray) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) Matchers.containsString(org.hamcrest.Matchers.containsString) Document(org.elasticsearch.index.mapper.ParseContext.Document)

Aggregations

Document (org.elasticsearch.index.mapper.ParseContext.Document)25 IndexableField (org.apache.lucene.index.IndexableField)12 BytesArray (org.elasticsearch.common.bytes.BytesArray)12 ParsedDocument (org.elasticsearch.index.mapper.ParsedDocument)11 BytesReference (org.elasticsearch.common.bytes.BytesReference)10 CompressedXContent (org.elasticsearch.common.compress.CompressedXContent)10 DocumentMapper (org.elasticsearch.index.mapper.DocumentMapper)8 NumericDocValuesField (org.apache.lucene.document.NumericDocValuesField)7 TextField (org.apache.lucene.document.TextField)7 IndexService (org.elasticsearch.index.IndexService)7 Matchers.containsString (org.hamcrest.Matchers.containsString)7 Field (org.apache.lucene.document.Field)5 FieldMapper (org.elasticsearch.index.mapper.FieldMapper)4 LongPoint (org.apache.lucene.document.LongPoint)3 StoredField (org.apache.lucene.document.StoredField)3 Term (org.apache.lucene.index.Term)3 BytesRef (org.apache.lucene.util.BytesRef)3 Index (org.elasticsearch.index.Index)3 ArrayList (java.util.ArrayList)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2