Search in sources :

Example 71 with BytesArray

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

the class RefreshListenersTests method index.

private Engine.IndexResult index(String id, String testFieldValue) throws IOException {
    String type = "test";
    String uid = type + ":" + id;
    Document document = new Document();
    document.add(new TextField("test", testFieldValue, Field.Store.YES));
    Field uidField = new Field("_uid", Uid.createUid(type, id), UidFieldMapper.Defaults.FIELD_TYPE);
    Field versionField = new NumericDocValuesField("_version", Versions.MATCH_ANY);
    SeqNoFieldMapper.SequenceID seqID = SeqNoFieldMapper.SequenceID.emptySeqID();
    document.add(uidField);
    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, type, null, Arrays.asList(document), source, XContentType.JSON, null);
    Engine.Index index = new Engine.Index(new Term("_uid", doc.uid()), doc);
    return engine.index(index);
}
Also used : SeqNoFieldMapper(org.elasticsearch.index.mapper.SeqNoFieldMapper) BytesReference(org.elasticsearch.common.bytes.BytesReference) BytesArray(org.elasticsearch.common.bytes.BytesArray) Index(org.elasticsearch.index.Index) Term(org.apache.lucene.index.Term) Document(org.elasticsearch.index.mapper.ParseContext.Document) ParsedDocument(org.elasticsearch.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.elasticsearch.index.mapper.ParsedDocument) TextField(org.apache.lucene.document.TextField) Engine(org.elasticsearch.index.engine.Engine) InternalEngine(org.elasticsearch.index.engine.InternalEngine)

Example 72 with BytesArray

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

the class TranslogTests method testTranslogOpSerialization.

public void testTranslogOpSerialization() throws Exception {
    BytesReference B_1 = new BytesArray(new byte[] { 1 });
    SeqNoFieldMapper.SequenceID seqID = SeqNoFieldMapper.SequenceID.emptySeqID();
    assert Version.CURRENT.major <= 6 : "Using UNASSIGNED_SEQ_NO can be removed in 7.0, because 6.0+ nodes have actual sequence numbers";
    long randomSeqNum = randomBoolean() ? SequenceNumbersService.UNASSIGNED_SEQ_NO : randomNonNegativeLong();
    long randomPrimaryTerm = randomBoolean() ? 0 : randomNonNegativeLong();
    seqID.seqNo.setLongValue(randomSeqNum);
    seqID.seqNoDocValue.setLongValue(randomSeqNum);
    seqID.primaryTerm.setLongValue(randomPrimaryTerm);
    Field uidField = new Field("_uid", Uid.createUid("test", "1"), UidFieldMapper.Defaults.FIELD_TYPE);
    Field versionField = new NumericDocValuesField("_version", 1);
    Document document = new Document();
    document.add(new TextField("value", "test", Field.Store.YES));
    document.add(uidField);
    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);
    Engine.IndexResult eIndexResult = new Engine.IndexResult(1, randomSeqNum, true);
    Translog.Index index = new Translog.Index(eIndex, eIndexResult);
    BytesStreamOutput out = new BytesStreamOutput();
    index.writeTo(out);
    StreamInput in = out.bytes().streamInput();
    Translog.Index serializedIndex = new Translog.Index(in);
    assertEquals(index, serializedIndex);
    Engine.Delete eDelete = new Engine.Delete(doc.type(), doc.id(), newUid(doc), randomSeqNum, randomPrimaryTerm, 2, VersionType.INTERNAL, Origin.PRIMARY, 0);
    Engine.DeleteResult eDeleteResult = new Engine.DeleteResult(2, randomSeqNum, true);
    Translog.Delete delete = new Translog.Delete(eDelete, eDeleteResult);
    out = new BytesStreamOutput();
    delete.writeTo(out);
    in = out.bytes().streamInput();
    Translog.Delete serializedDelete = new Translog.Delete(in);
    assertEquals(delete, serializedDelete);
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) SeqNoFieldMapper(org.elasticsearch.index.mapper.SeqNoFieldMapper) BytesArray(org.elasticsearch.common.bytes.BytesArray) Document(org.elasticsearch.index.mapper.ParseContext.Document) ParsedDocument(org.elasticsearch.index.mapper.ParsedDocument) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput) 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.elasticsearch.index.mapper.ParsedDocument) StreamInput(org.elasticsearch.common.io.stream.StreamInput) TextField(org.apache.lucene.document.TextField) Engine(org.elasticsearch.index.engine.Engine)

Example 73 with BytesArray

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

the class TranslogTests method testCloseIntoReader.

public void testCloseIntoReader() throws IOException {
    try (TranslogWriter writer = translog.createWriter(0)) {
        final int numOps = randomIntBetween(8, 128);
        final byte[] bytes = new byte[4];
        final ByteArrayDataOutput out = new ByteArrayDataOutput(bytes);
        for (int i = 0; i < numOps; i++) {
            out.reset(bytes);
            out.writeInt(i);
            writer.add(new BytesArray(bytes), randomNonNegativeLong());
        }
        writer.sync();
        final Checkpoint writerCheckpoint = writer.getCheckpoint();
        try (TranslogReader reader = writer.closeIntoReader()) {
            for (int i = 0; i < numOps; i++) {
                final ByteBuffer buffer = ByteBuffer.allocate(4);
                reader.readBytes(buffer, reader.getFirstOperationOffset() + 4 * i);
                buffer.flip();
                final int value = buffer.getInt();
                assertEquals(i, value);
            }
            final Checkpoint readerCheckpoint = reader.getCheckpoint();
            assertThat(readerCheckpoint, equalTo(writerCheckpoint));
        }
    }
}
Also used : BytesArray(org.elasticsearch.common.bytes.BytesArray) ByteArrayDataOutput(org.apache.lucene.store.ByteArrayDataOutput) ByteBuffer(java.nio.ByteBuffer)

Example 74 with BytesArray

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

the class SimpleIndexTemplateIT method testIndexTemplateWithAliasesInSource.

public void testIndexTemplateWithAliasesInSource() {
    client().admin().indices().preparePutTemplate("template_1").setSource(new BytesArray("{\n" + "    \"template\" : \"*\",\n" + "    \"aliases\" : {\n" + "        \"my_alias\" : {\n" + "            \"filter\" : {\n" + "                \"type\" : {\n" + "                    \"value\" : \"type2\"\n" + "                }\n" + "            }\n" + "        }\n" + "    }\n" + "}"), XContentType.JSON).get();
    assertAcked(prepareCreate("test_index").addMapping("type1").addMapping("type2"));
    ensureGreen();
    GetAliasesResponse getAliasesResponse = client().admin().indices().prepareGetAliases().setIndices("test_index").get();
    assertThat(getAliasesResponse.getAliases().size(), equalTo(1));
    assertThat(getAliasesResponse.getAliases().get("test_index").size(), equalTo(1));
    client().prepareIndex("test_index", "type1", "1").setSource("field", "value1").get();
    client().prepareIndex("test_index", "type2", "2").setSource("field", "value2").get();
    refresh();
    SearchResponse searchResponse = client().prepareSearch("test_index").get();
    assertHitCount(searchResponse, 2L);
    searchResponse = client().prepareSearch("my_alias").get();
    assertHitCount(searchResponse, 1L);
    assertThat(searchResponse.getHits().getAt(0).getType(), equalTo("type2"));
}
Also used : BytesArray(org.elasticsearch.common.bytes.BytesArray) GetAliasesResponse(org.elasticsearch.action.admin.indices.alias.get.GetAliasesResponse) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 75 with BytesArray

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

the class PipelineStoreTests method testDeleteUsingWildcard.

public void testDeleteUsingWildcard() {
    HashMap<String, PipelineConfiguration> pipelines = new HashMap<>();
    BytesArray definition = new BytesArray("{\"processors\": [{\"set\" : {\"field\": \"_field\", \"value\": \"_value\"}}]}");
    pipelines.put("p1", new PipelineConfiguration("p1", definition, XContentType.JSON));
    pipelines.put("p2", new PipelineConfiguration("p2", definition, XContentType.JSON));
    pipelines.put("q1", new PipelineConfiguration("q1", definition, XContentType.JSON));
    IngestMetadata ingestMetadata = new IngestMetadata(pipelines);
    ClusterState clusterState = ClusterState.builder(new ClusterName("_name")).build();
    ClusterState previousClusterState = clusterState;
    clusterState = ClusterState.builder(clusterState).metaData(MetaData.builder().putCustom(IngestMetadata.TYPE, ingestMetadata)).build();
    store.innerUpdatePipelines(previousClusterState, clusterState);
    assertThat(store.get("p1"), notNullValue());
    assertThat(store.get("p2"), notNullValue());
    assertThat(store.get("q1"), notNullValue());
    // Delete pipeline matching wildcard
    DeletePipelineRequest deleteRequest = new DeletePipelineRequest("p*");
    previousClusterState = clusterState;
    clusterState = store.innerDelete(deleteRequest, clusterState);
    store.innerUpdatePipelines(previousClusterState, clusterState);
    assertThat(store.get("p1"), nullValue());
    assertThat(store.get("p2"), nullValue());
    assertThat(store.get("q1"), notNullValue());
    // Exception if we used name which does not exist
    try {
        store.innerDelete(new DeletePipelineRequest("unknown"), clusterState);
        fail("exception expected");
    } catch (ResourceNotFoundException e) {
        assertThat(e.getMessage(), equalTo("pipeline [unknown] is missing"));
    }
    // match all wildcard works on last remaining pipeline
    DeletePipelineRequest matchAllDeleteRequest = new DeletePipelineRequest("*");
    previousClusterState = clusterState;
    clusterState = store.innerDelete(matchAllDeleteRequest, clusterState);
    store.innerUpdatePipelines(previousClusterState, clusterState);
    assertThat(store.get("p1"), nullValue());
    assertThat(store.get("p2"), nullValue());
    assertThat(store.get("q1"), nullValue());
    // match all wildcard does not throw exception if none match
    store.innerDelete(matchAllDeleteRequest, clusterState);
}
Also used : DeletePipelineRequest(org.elasticsearch.action.ingest.DeletePipelineRequest) ClusterState(org.elasticsearch.cluster.ClusterState) BytesArray(org.elasticsearch.common.bytes.BytesArray) HashMap(java.util.HashMap) ClusterName(org.elasticsearch.cluster.ClusterName) ResourceNotFoundException(org.elasticsearch.ResourceNotFoundException)

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