Search in sources :

Example 16 with Document

use of org.opensearch.index.mapper.ParseContext.Document in project OpenSearch by opensearch-project.

the class DynamicTemplatesTests method testSimpleWithXContentTraverse.

public void testSimpleWithXContentTraverse() throws Exception {
    String mapping = copyToStringFromClasspath("/org/opensearch/index/mapper/dynamictemplate/simple/test-mapping.json");
    MapperService mapperService = createMapperService("_doc", mapping);
    String docJson = copyToStringFromClasspath("/org/opensearch/index/mapper/dynamictemplate/simple/test-data.json");
    ParsedDocument parsedDoc = mapperService.documentMapper().parse(source(docJson));
    merge(mapperService, dynamicMapping(parsedDoc.dynamicMappingsUpdate()));
    Document doc = parsedDoc.rootDoc();
    IndexableField f = doc.getField("name");
    assertThat(f.name(), equalTo("name"));
    assertThat(f.binaryValue(), equalTo(new BytesRef("some name")));
    assertNotSame(IndexOptions.NONE, f.fieldType().indexOptions());
    assertThat(f.fieldType().tokenized(), equalTo(false));
    Mapper fieldMapper = mapperService.documentMapper().mappers().getMapper("name");
    assertNotNull(fieldMapper);
    f = doc.getField("multi1");
    assertThat(f.name(), equalTo("multi1"));
    assertThat(f.stringValue(), equalTo("multi 1"));
    assertNotSame(IndexOptions.NONE, f.fieldType().indexOptions());
    assertThat(f.fieldType().tokenized(), equalTo(true));
    fieldMapper = mapperService.documentMapper().mappers().getMapper("multi1");
    assertNotNull(fieldMapper);
    f = doc.getField("multi1.org");
    assertThat(f.name(), equalTo("multi1.org"));
    assertThat(f.binaryValue(), equalTo(new BytesRef("multi 1")));
    assertNotSame(IndexOptions.NONE, f.fieldType().indexOptions());
    assertThat(f.fieldType().tokenized(), equalTo(false));
    fieldMapper = mapperService.documentMapper().mappers().getMapper("multi1.org");
    assertNotNull(fieldMapper);
    f = doc.getField("multi2");
    assertThat(f.name(), equalTo("multi2"));
    assertThat(f.stringValue(), equalTo("multi 2"));
    assertNotSame(IndexOptions.NONE, f.fieldType().indexOptions());
    assertThat(f.fieldType().tokenized(), equalTo(true));
    fieldMapper = mapperService.documentMapper().mappers().getMapper("multi2");
    assertNotNull(fieldMapper);
    f = doc.getField("multi2.org");
    assertThat(f.name(), equalTo("multi2.org"));
    assertThat(f.binaryValue(), equalTo(new BytesRef("multi 2")));
    assertNotSame(IndexOptions.NONE, f.fieldType().indexOptions());
    assertThat(f.fieldType().tokenized(), equalTo(false));
    fieldMapper = mapperService.documentMapper().mappers().getMapper("multi2.org");
    assertNotNull(fieldMapper);
}
Also used : IndexableField(org.apache.lucene.index.IndexableField) Document(org.opensearch.index.mapper.ParseContext.Document) BytesRef(org.apache.lucene.util.BytesRef)

Example 17 with Document

use of org.opensearch.index.mapper.ParseContext.Document in project OpenSearch by opensearch-project.

the class DynamicTemplatesTests method testSimple.

public void testSimple() throws Exception {
    String mapping = copyToStringFromClasspath("/org/opensearch/index/mapper/dynamictemplate/simple/test-mapping.json");
    MapperService mapperService = createMapperService("_doc", mapping);
    String docJson = copyToStringFromClasspath("/org/opensearch/index/mapper/dynamictemplate/simple/test-data.json");
    ParsedDocument parsedDoc = mapperService.documentMapper().parse(source(docJson));
    merge(mapperService, dynamicMapping(parsedDoc.dynamicMappingsUpdate()));
    Document doc = parsedDoc.rootDoc();
    IndexableField f = doc.getField("name");
    assertThat(f.name(), equalTo("name"));
    assertThat(f.binaryValue(), equalTo(new BytesRef("some name")));
    assertNotSame(IndexOptions.NONE, f.fieldType().indexOptions());
    assertThat(f.fieldType().tokenized(), equalTo(false));
    Mapper fieldMapper = mapperService.documentMapper().mappers().getMapper("name");
    assertNotNull(fieldMapper);
    f = doc.getField("multi1");
    assertThat(f.name(), equalTo("multi1"));
    assertThat(f.stringValue(), equalTo("multi 1"));
    assertNotSame(IndexOptions.NONE, f.fieldType().indexOptions());
    assertThat(f.fieldType().tokenized(), equalTo(true));
    fieldMapper = mapperService.documentMapper().mappers().getMapper("multi1");
    assertNotNull(fieldMapper);
    f = doc.getField("multi1.org");
    assertThat(f.name(), equalTo("multi1.org"));
    assertThat(f.binaryValue(), equalTo(new BytesRef("multi 1")));
    assertNotSame(IndexOptions.NONE, f.fieldType().indexOptions());
    assertThat(f.fieldType().tokenized(), equalTo(false));
    fieldMapper = mapperService.documentMapper().mappers().getMapper("multi1.org");
    assertNotNull(fieldMapper);
    f = doc.getField("multi2");
    assertThat(f.name(), equalTo("multi2"));
    assertThat(f.stringValue(), equalTo("multi 2"));
    assertNotSame(IndexOptions.NONE, f.fieldType().indexOptions());
    assertThat(f.fieldType().tokenized(), equalTo(true));
    fieldMapper = mapperService.documentMapper().mappers().getMapper("multi2");
    assertNotNull(fieldMapper);
    f = doc.getField("multi2.org");
    assertThat(f.name(), equalTo("multi2.org"));
    assertThat(f.binaryValue(), equalTo(new BytesRef("multi 2")));
    assertNotSame(IndexOptions.NONE, f.fieldType().indexOptions());
    assertThat(f.fieldType().tokenized(), equalTo(false));
    fieldMapper = mapperService.documentMapper().mappers().getMapper("multi2.org");
    assertNotNull(fieldMapper);
}
Also used : IndexableField(org.apache.lucene.index.IndexableField) Document(org.opensearch.index.mapper.ParseContext.Document) BytesRef(org.apache.lucene.util.BytesRef)

Example 18 with Document

use of org.opensearch.index.mapper.ParseContext.Document in project OpenSearch by opensearch-project.

the class GenericStoreDynamicTemplateTests method testSimple.

public void testSimple() throws Exception {
    String mapping = copyToStringFromClasspath("/org/opensearch/index/mapper/dynamictemplate/genericstore/test-mapping.json");
    IndexService index = createIndex("test");
    client().admin().indices().preparePutMapping("test").setSource(mapping, XContentType.JSON).get();
    MapperService mapperService = index.mapperService();
    byte[] json = copyToBytesFromClasspath("/org/opensearch/index/mapper/dynamictemplate/genericstore/test-data.json");
    ParsedDocument parsedDoc = mapperService.documentMapper().parse(new SourceToParse("test", MapperService.SINGLE_MAPPING_NAME, "1", new BytesArray(json), XContentType.JSON));
    client().admin().indices().preparePutMapping("test").setSource(parsedDoc.dynamicMappingsUpdate().toString(), XContentType.JSON).get();
    Document doc = parsedDoc.rootDoc();
    IndexableField f = doc.getField("name");
    assertThat(f.name(), equalTo("name"));
    assertThat(f.stringValue(), equalTo("some name"));
    assertThat(f.fieldType().stored(), equalTo(true));
    assertTrue(mapperService.fieldType("name").isStored());
    boolean stored = false;
    for (IndexableField field : doc.getFields("age")) {
        stored |= field.fieldType().stored();
    }
    assertTrue(stored);
}
Also used : IndexableField(org.apache.lucene.index.IndexableField) BytesArray(org.opensearch.common.bytes.BytesArray) IndexService(org.opensearch.index.IndexService) Document(org.opensearch.index.mapper.ParseContext.Document)

Example 19 with Document

use of org.opensearch.index.mapper.ParseContext.Document in project OpenSearch by opensearch-project.

the class RefreshListenersTests method index.

private Engine.IndexResult index(String id, String testFieldValue) throws IOException {
    Document document = new Document();
    document.add(new TextField("test", testFieldValue, Field.Store.YES));
    Field idField = new Field("_id", id, IdFieldMapper.Defaults.FIELD_TYPE);
    Field versionField = new NumericDocValuesField("_version", Versions.MATCH_ANY);
    SeqNoFieldMapper.SequenceIDFields seqID = SeqNoFieldMapper.SequenceIDFields.emptySeqID();
    document.add(idField);
    document.add(versionField);
    document.add(seqID.seqNo);
    document.add(seqID.seqNoDocValue);
    document.add(seqID.primaryTerm);
    BytesReference source = new BytesArray(new byte[] { 1 });
    ParsedDocument doc = new ParsedDocument(versionField, seqID, id, "test", null, Arrays.asList(document), source, XContentType.JSON, null);
    Engine.Index index = new Engine.Index(new Term("_id", doc.id()), engine.config().getPrimaryTermSupplier().getAsLong(), doc);
    return engine.index(index);
}
Also used : SeqNoFieldMapper(org.opensearch.index.mapper.SeqNoFieldMapper) BytesReference(org.opensearch.common.bytes.BytesReference) BytesArray(org.opensearch.common.bytes.BytesArray) Index(org.opensearch.index.Index) Term(org.apache.lucene.index.Term) Document(org.opensearch.index.mapper.ParseContext.Document) ParsedDocument(org.opensearch.index.mapper.ParsedDocument) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) Field(org.apache.lucene.document.Field) TextField(org.apache.lucene.document.TextField) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) ParsedDocument(org.opensearch.index.mapper.ParsedDocument) TextField(org.apache.lucene.document.TextField) InternalEngine(org.opensearch.index.engine.InternalEngine) Engine(org.opensearch.index.engine.Engine)

Example 20 with Document

use of org.opensearch.index.mapper.ParseContext.Document in project OpenSearch by opensearch-project.

the class RefreshListenersTests method testLotsOfThreads.

/**
 * Uses a bunch of threads to index, wait for refresh, and non-realtime get documents to validate that they are visible after waiting
 * regardless of what crazy sequence of events causes the refresh listener to fire.
 */
public void testLotsOfThreads() throws Exception {
    int threadCount = between(3, 10);
    maxListeners = between(1, threadCount * 2);
    // This thread just refreshes every once in a while to cause trouble.
    Cancellable refresher = threadPool.scheduleWithFixedDelay(() -> engine.refresh("because test"), timeValueMillis(100), Names.SAME);
    // These threads add and block until the refresh makes the change visible and then do a non-realtime get.
    Thread[] indexers = new Thread[threadCount];
    for (int thread = 0; thread < threadCount; thread++) {
        final String threadId = String.format(Locale.ROOT, "%04d", thread);
        indexers[thread] = new Thread(() -> {
            for (int iteration = 1; iteration <= 50; iteration++) {
                try {
                    String testFieldValue = String.format(Locale.ROOT, "%s%04d", threadId, iteration);
                    Engine.IndexResult index = index(threadId, testFieldValue);
                    assertEquals(iteration, index.getVersion());
                    DummyRefreshListener listener = new DummyRefreshListener();
                    listeners.addOrNotify(index.getTranslogLocation(), listener);
                    assertBusy(() -> assertNotNull("listener never called", listener.forcedRefresh.get()), 1, TimeUnit.MINUTES);
                    if (threadCount < maxListeners) {
                        assertFalse(listener.forcedRefresh.get());
                    }
                    listener.assertNoError();
                    Engine.Get get = new Engine.Get(false, false, threadId, new Term(IdFieldMapper.NAME, threadId));
                    try (Engine.GetResult getResult = engine.get(get, engine::acquireSearcher)) {
                        assertTrue("document not found", getResult.exists());
                        assertEquals(iteration, getResult.version());
                        org.apache.lucene.document.Document document = getResult.docIdAndVersion().reader.document(getResult.docIdAndVersion().docId);
                        assertThat(document.getValues("test"), arrayContaining(testFieldValue));
                    }
                } catch (Exception t) {
                    throw new RuntimeException("failure on the [" + iteration + "] iteration of thread [" + threadId + "]", t);
                }
            }
        });
        indexers[thread].start();
    }
    for (Thread indexer : indexers) {
        indexer.join();
    }
    refresher.cancel();
}
Also used : Cancellable(org.opensearch.threadpool.Scheduler.Cancellable) Term(org.apache.lucene.index.Term) Document(org.opensearch.index.mapper.ParseContext.Document) ParsedDocument(org.opensearch.index.mapper.ParsedDocument) IOException(java.io.IOException) InternalEngine(org.opensearch.index.engine.InternalEngine) Engine(org.opensearch.index.engine.Engine)

Aggregations

Document (org.opensearch.index.mapper.ParseContext.Document)32 IndexableField (org.apache.lucene.index.IndexableField)22 BytesArray (org.opensearch.common.bytes.BytesArray)16 BytesReference (org.opensearch.common.bytes.BytesReference)16 Matchers.containsString (org.hamcrest.Matchers.containsString)15 IOException (java.io.IOException)11 NumericDocValuesField (org.apache.lucene.document.NumericDocValuesField)11 TextField (org.apache.lucene.document.TextField)11 BytesRef (org.apache.lucene.util.BytesRef)11 Field (org.apache.lucene.document.Field)10 ParsedDocument (org.opensearch.index.mapper.ParsedDocument)10 List (java.util.List)9 Version (org.opensearch.Version)9 IndexSettings (org.opensearch.index.IndexSettings)9 ArrayList (java.util.ArrayList)8 Collections (java.util.Collections)8 StoredField (org.apache.lucene.document.StoredField)8 SortedSetSortField (org.apache.lucene.search.SortedSetSortField)8 Matchers.equalTo (org.hamcrest.Matchers.equalTo)8 IndexMetadata (org.opensearch.cluster.metadata.IndexMetadata)8