Search in sources :

Example 66 with BytesArray

use of org.elasticsearch.common.bytes.BytesArray in project elasticsearch by elastic.

the class SourceFieldMapperTests method testExcludes.

public void testExcludes() throws Exception {
    String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("_source").array("excludes", new String[] { "path1*" }).endObject().endObject().endObject().string();
    DocumentMapper documentMapper = createIndex("test").mapperService().documentMapperParser().parse("type", new CompressedXContent(mapping));
    ParsedDocument doc = documentMapper.parse("test", "type", "1", XContentFactory.jsonBuilder().startObject().startObject("path1").field("field1", "value1").endObject().startObject("path2").field("field2", "value2").endObject().endObject().bytes());
    IndexableField sourceField = doc.rootDoc().getField("_source");
    Map<String, Object> sourceAsMap;
    try (XContentParser parser = createParser(JsonXContent.jsonXContent, new BytesArray(sourceField.binaryValue()))) {
        sourceAsMap = parser.map();
    }
    assertThat(sourceAsMap.containsKey("path1"), equalTo(false));
    assertThat(sourceAsMap.containsKey("path2"), equalTo(true));
}
Also used : IndexableField(org.apache.lucene.index.IndexableField) BytesArray(org.elasticsearch.common.bytes.BytesArray) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) Matchers.containsString(org.hamcrest.Matchers.containsString) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 67 with BytesArray

use of org.elasticsearch.common.bytes.BytesArray in project elasticsearch by elastic.

the class GeoShapeQueryBuilderTests method executeGet.

@Override
protected GetResponse executeGet(GetRequest getRequest) {
    assertThat(indexedShapeToReturn, notNullValue());
    assertThat(indexedShapeId, notNullValue());
    assertThat(indexedShapeType, notNullValue());
    assertThat(getRequest.id(), equalTo(indexedShapeId));
    assertThat(getRequest.type(), equalTo(indexedShapeType));
    String expectedShapeIndex = indexedShapeIndex == null ? GeoShapeQueryBuilder.DEFAULT_SHAPE_INDEX_NAME : indexedShapeIndex;
    assertThat(getRequest.index(), equalTo(expectedShapeIndex));
    String expectedShapePath = indexedShapePath == null ? GeoShapeQueryBuilder.DEFAULT_SHAPE_FIELD_NAME : indexedShapePath;
    String json;
    try {
        XContentBuilder builder = XContentFactory.jsonBuilder().prettyPrint();
        builder.startObject();
        builder.field(expectedShapePath, indexedShapeToReturn);
        builder.endObject();
        json = builder.string();
    } catch (IOException ex) {
        throw new ElasticsearchException("boom", ex);
    }
    return new GetResponse(new GetResult(indexedShapeIndex, indexedShapeType, indexedShapeId, 0, true, new BytesArray(json), null));
}
Also used : BytesArray(org.elasticsearch.common.bytes.BytesArray) GetResult(org.elasticsearch.index.get.GetResult) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) IOException(java.io.IOException) ElasticsearchException(org.elasticsearch.ElasticsearchException) GetResponse(org.elasticsearch.action.get.GetResponse) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 68 with BytesArray

use of org.elasticsearch.common.bytes.BytesArray in project elasticsearch by elastic.

the class TermsQueryBuilderTests method executeGet.

@Override
public GetResponse executeGet(GetRequest getRequest) {
    String json;
    try {
        XContentBuilder builder = XContentFactory.jsonBuilder().prettyPrint();
        builder.startObject();
        builder.array(termsPath, randomTerms.toArray(new Object[randomTerms.size()]));
        builder.endObject();
        json = builder.string();
    } catch (IOException ex) {
        throw new ElasticsearchException("boom", ex);
    }
    return new GetResponse(new GetResult(getRequest.index(), getRequest.type(), getRequest.id(), 0, true, new BytesArray(json), null));
}
Also used : BytesArray(org.elasticsearch.common.bytes.BytesArray) GetResult(org.elasticsearch.index.get.GetResult) Matchers.containsString(org.hamcrest.Matchers.containsString) IOException(java.io.IOException) ElasticsearchException(org.elasticsearch.ElasticsearchException) GetResponse(org.elasticsearch.action.get.GetResponse) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 69 with BytesArray

use of org.elasticsearch.common.bytes.BytesArray in project elasticsearch by elastic.

the class IndexShardTests method testIndexingOperationsListeners.

public void testIndexingOperationsListeners() throws IOException {
    IndexShard shard = newStartedShard(true);
    indexDoc(shard, "test", "0", "{\"foo\" : \"bar\"}");
    AtomicInteger preIndex = new AtomicInteger();
    AtomicInteger postIndexCreate = new AtomicInteger();
    AtomicInteger postIndexUpdate = new AtomicInteger();
    AtomicInteger postIndexException = new AtomicInteger();
    AtomicInteger preDelete = new AtomicInteger();
    AtomicInteger postDelete = new AtomicInteger();
    AtomicInteger postDeleteException = new AtomicInteger();
    shard.close("simon says", true);
    shard = reinitShard(shard, new IndexingOperationListener() {

        @Override
        public Engine.Index preIndex(ShardId shardId, Engine.Index operation) {
            preIndex.incrementAndGet();
            return operation;
        }

        @Override
        public void postIndex(ShardId shardId, Engine.Index index, Engine.IndexResult result) {
            if (result.hasFailure() == false) {
                if (result.isCreated()) {
                    postIndexCreate.incrementAndGet();
                } else {
                    postIndexUpdate.incrementAndGet();
                }
            } else {
                postIndex(shardId, index, result.getFailure());
            }
        }

        @Override
        public void postIndex(ShardId shardId, Engine.Index index, Exception ex) {
            postIndexException.incrementAndGet();
        }

        @Override
        public Engine.Delete preDelete(ShardId shardId, Engine.Delete delete) {
            preDelete.incrementAndGet();
            return delete;
        }

        @Override
        public void postDelete(ShardId shardId, Engine.Delete delete, Engine.DeleteResult result) {
            if (result.hasFailure() == false) {
                postDelete.incrementAndGet();
            } else {
                postDelete(shardId, delete, result.getFailure());
            }
        }

        @Override
        public void postDelete(ShardId shardId, Engine.Delete delete, Exception ex) {
            postDeleteException.incrementAndGet();
        }
    });
    recoveryShardFromStore(shard);
    ParsedDocument doc = testParsedDocument("1", "test", null, new ParseContext.Document(), new BytesArray(new byte[] { 1 }), null);
    Engine.Index index = new Engine.Index(new Term("_uid", doc.uid()), doc);
    shard.index(index);
    assertEquals(1, preIndex.get());
    assertEquals(1, postIndexCreate.get());
    assertEquals(0, postIndexUpdate.get());
    assertEquals(0, postIndexException.get());
    assertEquals(0, preDelete.get());
    assertEquals(0, postDelete.get());
    assertEquals(0, postDeleteException.get());
    shard.index(index);
    assertEquals(2, preIndex.get());
    assertEquals(1, postIndexCreate.get());
    assertEquals(1, postIndexUpdate.get());
    assertEquals(0, postIndexException.get());
    assertEquals(0, preDelete.get());
    assertEquals(0, postDelete.get());
    assertEquals(0, postDeleteException.get());
    Engine.Delete delete = new Engine.Delete("test", "1", new Term("_uid", doc.uid()));
    shard.delete(delete);
    assertEquals(2, preIndex.get());
    assertEquals(1, postIndexCreate.get());
    assertEquals(1, postIndexUpdate.get());
    assertEquals(0, postIndexException.get());
    assertEquals(1, preDelete.get());
    assertEquals(1, postDelete.get());
    assertEquals(0, postDeleteException.get());
    shard.close("Unexpected close", true);
    // It will generate exception
    shard.state = IndexShardState.STARTED;
    try {
        shard.index(index);
        fail();
    } catch (AlreadyClosedException e) {
    }
    assertEquals(2, preIndex.get());
    assertEquals(1, postIndexCreate.get());
    assertEquals(1, postIndexUpdate.get());
    assertEquals(0, postIndexException.get());
    assertEquals(1, preDelete.get());
    assertEquals(1, postDelete.get());
    assertEquals(0, postDeleteException.get());
    try {
        shard.delete(delete);
        fail();
    } catch (AlreadyClosedException e) {
    }
    assertEquals(2, preIndex.get());
    assertEquals(1, postIndexCreate.get());
    assertEquals(1, postIndexUpdate.get());
    assertEquals(0, postIndexException.get());
    assertEquals(1, preDelete.get());
    assertEquals(1, postDelete.get());
    assertEquals(0, postDeleteException.get());
    closeShards(shard);
}
Also used : BytesArray(org.elasticsearch.common.bytes.BytesArray) Lucene.cleanLuceneIndex(org.elasticsearch.common.lucene.Lucene.cleanLuceneIndex) Term(org.apache.lucene.index.Term) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) EngineException(org.elasticsearch.index.engine.EngineException) IOException(java.io.IOException) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) ExecutionException(java.util.concurrent.ExecutionException) CorruptIndexException(org.apache.lucene.index.CorruptIndexException) ParsedDocument(org.elasticsearch.index.mapper.ParsedDocument) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ParseContext(org.elasticsearch.index.mapper.ParseContext) Engine(org.elasticsearch.index.engine.Engine)

Example 70 with BytesArray

use of org.elasticsearch.common.bytes.BytesArray in project elasticsearch by elastic.

the class IndexShardIT method testMaybeFlush.

public void testMaybeFlush() throws Exception {
    createIndex("test", Settings.builder().put(IndexSettings.INDEX_TRANSLOG_DURABILITY_SETTING.getKey(), Translog.Durability.REQUEST).build());
    ensureGreen();
    IndicesService indicesService = getInstanceFromNode(IndicesService.class);
    IndexService test = indicesService.indexService(resolveIndex("test"));
    IndexShard shard = test.getShardOrNull(0);
    assertFalse(shard.shouldFlush());
    client().admin().indices().prepareUpdateSettings("test").setSettings(Settings.builder().put(IndexSettings.INDEX_TRANSLOG_FLUSH_THRESHOLD_SIZE_SETTING.getKey(), new ByteSizeValue(117, /* size of the operation + header&footer*/
    ByteSizeUnit.BYTES)).build()).get();
    client().prepareIndex("test", "test", "0").setSource("{}", XContentType.JSON).setRefreshPolicy(randomBoolean() ? IMMEDIATE : NONE).get();
    assertFalse(shard.shouldFlush());
    ParsedDocument doc = testParsedDocument("1", "test", null, SequenceNumbersService.UNASSIGNED_SEQ_NO, new ParseContext.Document(), new BytesArray(new byte[] { 1 }), XContentType.JSON, null);
    Engine.Index index = new Engine.Index(new Term("_uid", doc.uid()), doc);
    shard.index(index);
    assertTrue(shard.shouldFlush());
    assertEquals(2, shard.getEngine().getTranslog().totalOperations());
    client().prepareIndex("test", "test", "2").setSource("{}", XContentType.JSON).setRefreshPolicy(randomBoolean() ? IMMEDIATE : NONE).get();
    assertBusy(() -> {
        // this is async
        assertFalse(shard.shouldFlush());
    });
    assertEquals(0, shard.getEngine().getTranslog().totalOperations());
    shard.getEngine().getTranslog().sync();
    long size = shard.getEngine().getTranslog().sizeInBytes();
    logger.info("--> current translog size: [{}] num_ops [{}] generation [{}]", shard.getEngine().getTranslog().sizeInBytes(), shard.getEngine().getTranslog().totalOperations(), shard.getEngine().getTranslog().getGeneration());
    client().admin().indices().prepareUpdateSettings("test").setSettings(Settings.builder().put(IndexSettings.INDEX_TRANSLOG_FLUSH_THRESHOLD_SIZE_SETTING.getKey(), new ByteSizeValue(size, ByteSizeUnit.BYTES)).build()).get();
    client().prepareDelete("test", "test", "2").get();
    logger.info("--> translog size after delete: [{}] num_ops [{}] generation [{}]", shard.getEngine().getTranslog().sizeInBytes(), shard.getEngine().getTranslog().totalOperations(), shard.getEngine().getTranslog().getGeneration());
    assertBusy(() -> {
        // this is async
        logger.info("--> translog size on iter  : [{}] num_ops [{}] generation [{}]", shard.getEngine().getTranslog().sizeInBytes(), shard.getEngine().getTranslog().totalOperations(), shard.getEngine().getTranslog().getGeneration());
        assertFalse(shard.shouldFlush());
    });
    assertEquals(0, shard.getEngine().getTranslog().totalOperations());
}
Also used : BytesArray(org.elasticsearch.common.bytes.BytesArray) IndexService(org.elasticsearch.index.IndexService) ByteSizeValue(org.elasticsearch.common.unit.ByteSizeValue) IndicesService(org.elasticsearch.indices.IndicesService) Index(org.elasticsearch.index.Index) Term(org.apache.lucene.index.Term) ParsedDocument(org.elasticsearch.index.mapper.ParsedDocument) ParseContext(org.elasticsearch.index.mapper.ParseContext) Engine(org.elasticsearch.index.engine.Engine)

Aggregations

BytesArray (org.elasticsearch.common.bytes.BytesArray)203 BytesReference (org.elasticsearch.common.bytes.BytesReference)36 Matchers.containsString (org.hamcrest.Matchers.containsString)31 IOException (java.io.IOException)29 StreamInput (org.elasticsearch.common.io.stream.StreamInput)24 ParsedDocument (org.elasticsearch.index.mapper.ParsedDocument)24 BytesStreamOutput (org.elasticsearch.common.io.stream.BytesStreamOutput)21 HashMap (java.util.HashMap)17 BytesRef (org.apache.lucene.util.BytesRef)17 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)14 CompressedXContent (org.elasticsearch.common.compress.CompressedXContent)14 FakeRestRequest (org.elasticsearch.test.rest.FakeRestRequest)13 ArrayList (java.util.ArrayList)12 TopDocs (org.apache.lucene.search.TopDocs)12 SearchResponse (org.elasticsearch.action.search.SearchResponse)12 Document (org.elasticsearch.index.mapper.ParseContext.Document)12 Index (org.elasticsearch.index.Index)11 Map (java.util.Map)10 IndexableField (org.apache.lucene.index.IndexableField)10 IndexService (org.elasticsearch.index.IndexService)10