Java Code Examples for org.elasticsearch.common.bytes.BytesArray

The following examples show how to use org.elasticsearch.common.bytes.BytesArray. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: Elasticsearch   Source File: LocalRestRequest.java    License: Apache License 2.0 6 votes vote down vote up
public LocalRestRequest(final String uri, final Method method) {
    this.headers = new HashMap<>();
    if (uri.startsWith("\"") && uri.endsWith("\"")) {
        this.uri = uri.substring(1, uri.length()-1);
    } else {
        this.uri = uri;
    }
    this.method = method;
    this.params = new HashMap<>();
    this.content = BytesArray.EMPTY;

    int pathEndPos = this.uri.indexOf('?');
    if (pathEndPos < 0) {
        this.rawPath = this.uri;
    } else {
        this.rawPath = this.uri.substring(0, pathEndPos);
        RestUtils.decodeQueryString(this.uri, pathEndPos + 1, params);
    }
}
 
Example 2
Source Project: crate   Source File: AliasMetaData.java    License: Apache License 2.0 6 votes vote down vote up
public static void toXContent(AliasMetaData aliasMetaData, XContentBuilder builder, ToXContent.Params params) throws IOException {
    builder.startObject(aliasMetaData.alias());

    boolean binary = params.paramAsBoolean("binary", false);

    if (aliasMetaData.filter() != null) {
        if (binary) {
            builder.field("filter", aliasMetaData.filter.compressed());
        } else {
            builder.field("filter", XContentHelper.convertToMap(new BytesArray(aliasMetaData.filter().uncompressed()), true).v2());
        }
    }
    if (aliasMetaData.indexRouting() != null) {
        builder.field("index_routing", aliasMetaData.indexRouting());
    }
    if (aliasMetaData.searchRouting() != null) {
        builder.field("search_routing", aliasMetaData.searchRouting());
    }

    if (aliasMetaData.writeIndex() != null) {
        builder.field("is_write_index", aliasMetaData.writeIndex());
    }

    builder.endObject();
}
 
Example 3
Source Project: elasticsearch-minhash   Source File: MinHashPluginTest.java    License: Apache License 2.0 6 votes vote down vote up
private void test_get(final Client client, final String index,
        final String type, final String id, final byte[] hash1,
        final byte[] hash2, final byte[] hash3) {
    final GetResponse response = client.prepareGet(index, type, id)
            .setStoredFields(new String[] { "_source", "minhash_value1", "minhash_value2", "minhash_value3" }).execute()
            .actionGet();
    assertTrue(response.isExists());
    final Map<String, Object> source = response.getSourceAsMap();
    assertEquals("test " + Integer.parseInt(id) % 100, source.get("msg"));

    final DocumentField field1 = response.getField("minhash_value1");
    final BytesArray value1 = (BytesArray) field1.getValue();
    assertEquals(hash1.length, value1.length());
    Assert.assertArrayEquals(hash1, value1.array());

    final DocumentField field2 = response.getField("minhash_value2");
    final BytesArray value2 = (BytesArray) field2.getValue();
    assertEquals(hash2.length, value2.length());
    Assert.assertArrayEquals(hash2, value2.array());

    final DocumentField field3 = response.getField("minhash_value3");
    final BytesArray value3 = (BytesArray) field3.getValue();
    assertEquals(hash3.length, value3.length());
    Assert.assertArrayEquals(hash3, value3.array());
}
 
Example 4
Source Project: Elasticsearch   Source File: RemoteDigestBlob.java    License: Apache License 2.0 6 votes vote down vote up
private Status start(ChannelBuffer buffer, boolean last) {
    logger.trace("start blob upload");
    assert (transferId == null);
    StartBlobRequest request = new StartBlobRequest(
            index,
            Hex.decodeHex(digest),
            new BytesArray(buffer.array()),
            last
    );
    transferId = request.transferId();
    size += buffer.readableBytes();

    startResponse = client.execute(StartBlobAction.INSTANCE, request).actionGet();
    status = startResponse.status();
    return status;
}
 
Example 5
Source Project: Elasticsearch   Source File: BlobRecoveryHandler.java    License: Apache License 2.0 6 votes vote down vote up
private Set<BytesArray> getExistingDigestsFromTarget(byte prefix) {
    BlobStartPrefixResponse response =
        (BlobStartPrefixResponse)transportService.submitRequest(
            request.targetNode(),
            BlobRecoveryTarget.Actions.START_PREFIX,
            new BlobStartPrefixSyncRequest(request.recoveryId(), request.shardId(), prefix),
            TransportRequestOptions.EMPTY,
            new FutureTransportResponseHandler<TransportResponse>() {
                @Override
                public TransportResponse newInstance() {
                    return new BlobStartPrefixResponse();
                }
            }
        ).txGet();

    Set<BytesArray> result = new HashSet<BytesArray>();
    for (byte[] digests : response.existingDigests) {
        result.add(new BytesArray(digests));
    }
    return result;
}
 
Example 6
Source Project: Elasticsearch   Source File: TransportShardUpsertAction.java    License: Apache License 2.0 6 votes vote down vote up
@VisibleForTesting
Map<String, Object> buildMapFromSource(Reference[] insertColumns,
                                       Object[] insertValues,
                                       boolean isRawSourceInsert) {
    Map<String, Object> sourceAsMap;
    if (isRawSourceInsert) {
        BytesRef source = (BytesRef) insertValues[0];
        sourceAsMap = XContentHelper.convertToMap(new BytesArray(source), true).v2();
    } else {
        sourceAsMap = new LinkedHashMap<>(insertColumns.length);
        for (int i = 0; i < insertColumns.length; i++) {
            sourceAsMap.put(insertColumns[i].ident().columnIdent().fqn(), insertValues[i]);
        }
    }
    return sourceAsMap;
}
 
Example 7
Source Project: Elasticsearch   Source File: ChecksumBlobStoreFormat.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Reads blob with specified name without resolving the blobName using using {@link #blobName} method.
 *
 * @param blobContainer blob container
 * @param blobName blob name
 */
public T readBlob(BlobContainer blobContainer, String blobName) throws IOException {
    try (InputStream inputStream = blobContainer.readBlob(blobName)) {
        byte[] bytes = ByteStreams.toByteArray(inputStream);
        final String resourceDesc = "ChecksumBlobStoreFormat.readBlob(blob=\"" + blobName + "\")";
        try (ByteArrayIndexInput indexInput = new ByteArrayIndexInput(resourceDesc, bytes)) {
            CodecUtil.checksumEntireFile(indexInput);
            CodecUtil.checkHeader(indexInput, codec, VERSION, VERSION);
            long filePointer = indexInput.getFilePointer();
            long contentSize = indexInput.length() - CodecUtil.footerLength() - filePointer;
            BytesReference bytesReference = new BytesArray(bytes, (int) filePointer, (int) contentSize);
            return read(bytesReference);
        } catch (CorruptIndexException | IndexFormatTooOldException | IndexFormatTooNewException ex) {
            // we trick this into a dedicated exception with the original stacktrace
            throw new CorruptStateException(ex);
        }
    }
}
 
Example 8
Source Project: Elasticsearch   Source File: BlobStoreRepository.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Reads snapshot index file
 * <p>
 * This file can be used by read-only repositories that are unable to list files in the repository
 *
 * @return list of snapshots in the repository
 * @throws IOException I/O errors
 */
protected List<SnapshotId> readSnapshotList() throws IOException {
    try (InputStream blob = snapshotsBlobContainer.readBlob(SNAPSHOTS_FILE)) {
        final byte[] data = ByteStreams.toByteArray(blob);
        ArrayList<SnapshotId> snapshots = new ArrayList<>();
        try (XContentParser parser = XContentHelper.createParser(new BytesArray(data))) {
            if (parser.nextToken() == XContentParser.Token.START_OBJECT) {
                if (parser.nextToken() == XContentParser.Token.FIELD_NAME) {
                    String currentFieldName = parser.currentName();
                    if ("snapshots".equals(currentFieldName)) {
                        if (parser.nextToken() == XContentParser.Token.START_ARRAY) {
                            while (parser.nextToken() != XContentParser.Token.END_ARRAY) {
                                snapshots.add(new SnapshotId(repositoryName, parser.text()));
                            }
                        }
                    }
                }
            }
        }
        return Collections.unmodifiableList(snapshots);
    }
}
 
Example 9
Source Project: crate   Source File: SerializationTests.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testPutChunkReplicaRequestSerialization() throws Exception {
    UUID transferId = UUID.randomUUID();

    PutChunkReplicaRequest requestOut = new PutChunkReplicaRequest(
        new ShardId("foo", UUIDs.randomBase64UUID(), 1),
        "nodeId",
        transferId,
        0,
        new BytesArray(new byte[]{0x65, 0x66}),
        false
    );
    requestOut.index("foo");
    BytesStreamOutput outputStream = new BytesStreamOutput();
    requestOut.writeTo(outputStream);
    StreamInput inputStream = outputStream.bytes().streamInput();

    PutChunkReplicaRequest requestIn = new PutChunkReplicaRequest(inputStream);

    assertEquals(requestOut.currentPos, requestIn.currentPos);
    assertEquals(requestOut.isLast, requestIn.isLast);
    assertEquals(requestOut.content, requestIn.content);
    assertEquals(requestOut.transferId, requestIn.transferId);
    assertEquals(requestOut.index(), requestIn.index());
}
 
Example 10
@Override
public void verify(String seed) {
    BlobContainer testBlobContainer = blobStore.blobContainer(basePath.add(testBlobPrefix(seed)));
    DiscoveryNode localNode = clusterService.localNode();
    if (testBlobContainer.blobExists("master.dat")) {
        try  {
            testBlobContainer.writeBlob("data-" + localNode.getId() + ".dat", new BytesArray(seed));
        } catch (IOException exp) {
            throw new RepositoryVerificationException(repositoryName, "store location [" + blobStore + "] is not accessible on the node [" + localNode + "]", exp);
        }
    } else {
        throw new RepositoryVerificationException(repositoryName, "a file written by master to the store [" + blobStore + "] cannot be accessed on the node [" + localNode + "]. "
                + "This might indicate that the store [" + blobStore + "] is not shared between this node and the master node or "
                + "that permissions on the store don't allow reading files written by the master node");
    }
}
 
Example 11
Source Project: core-ng-project   Source File: ElasticSearchImpl.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void putIndex(String index, String source) {
    var watch = new StopWatch();
    try {
        IndicesClient client = client().indices();
        CreateIndexRequest request = new CreateIndexRequest(index).source(new BytesArray(source), XContentType.JSON);
        boolean exists = client.exists(new GetIndexRequest(index), RequestOptions.DEFAULT);
        if (!exists) {
            client.create(request, RequestOptions.DEFAULT);
        } else {
            // only try to update mappings, as for settings it generally requires to close index first then open after update
            logger.info("index already exists, update mapping, index={}", index);
            client.putMapping(new PutMappingRequest(index).source(request.mappings(), XContentType.JSON), RequestOptions.DEFAULT);
        }
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    } finally {
        logger.info("put index, index={}, source={}, elapsed={}", index, source, watch.elapsed());
    }
}
 
Example 12
Source Project: crate   Source File: GeneratedColsFromRawInsertSource.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public Map<String, Object> generateSourceAndCheckConstraints(Object[] values) {
    String rawSource = (String) values[0];
    Map<String, Object> source = XContentHelper.toMap(new BytesArray(rawSource), XContentType.JSON);
    mixinDefaults(source, defaults);
    for (int i = 0; i < expressions.size(); i++) {
        expressions.get(i).setNextRow(source);
    }
    for (Map.Entry<Reference, Input<?>> entry : generatedCols.entrySet()) {
        var reference = entry.getKey();
        var value = entry.getValue().value();
        var valueForInsert = reference
            .valueType()
            .valueForInsert(value);
        source.putIfAbsent(reference.column().fqn(), valueForInsert);
    }
    return source;
}
 
Example 13
Source Project: Elasticsearch   Source File: XContentHelper.java    License: Apache License 2.0 6 votes vote down vote up
public static String convertToJson(BytesReference bytes, boolean reformatJson, boolean prettyPrint) throws IOException {
    if (bytes.hasArray()) {
        return convertToJson(bytes.array(), bytes.arrayOffset(), bytes.length(), reformatJson, prettyPrint);
    }
    XContentType xContentType = XContentFactory.xContentType(bytes);
    if (xContentType == XContentType.JSON && !reformatJson) {
        BytesArray bytesArray = bytes.toBytesArray();
        return new String(bytesArray.array(), bytesArray.arrayOffset(), bytesArray.length(), Charsets.UTF_8);
    }
    try (XContentParser parser = XContentFactory.xContent(xContentType).createParser(bytes.streamInput())) {
        parser.nextToken();
        XContentBuilder builder = XContentFactory.jsonBuilder();
        if (prettyPrint) {
            builder.prettyPrint();
        }
        builder.copyCurrentStructure(parser);
        return builder.string();
    }
}
 
Example 14
@Test
public void testSeserialization() throws Exception {
    List<RolesMapping> mappings = new RolesMappingBuilder()
        .addUser("foo", "user_of_foo")
        .expire("12345")
        .build();
    SearchGuardRolesMapping sgMapping = new SearchGuardRolesMapping();
    sgMapping.addAll(mappings);
    final String out = XContentHelper.toString(sgMapping);
    Map<String, Object> in = XContentHelper.convertToMap(new BytesArray(out), true, XContentType.JSON).v2();
    SearchGuardRolesMapping inMapping = new SearchGuardRolesMapping().load(in);
    assertEquals("Exp serialization to equal derialization", out, XContentHelper.toString(inMapping));
}
 
Example 15
@Override
public void binaryField(final FieldInfo fieldInfo, final byte[] value) throws IOException {

    if (fieldInfo.name.equals("_source")) {
        final BytesReference bytesRef = new BytesArray(value);
        final Tuple<XContentType, Map<String, Object>> bytesRefTuple = XContentHelper.convertToMap(bytesRef, false, XContentType.JSON);
        Map<String, Object> filteredSource = bytesRefTuple.v2();
        MapUtils.deepTraverseMap(filteredSource, HASH_CB);
        final XContentBuilder xBuilder = XContentBuilder.builder(bytesRefTuple.v1().xContent()).map(filteredSource);
        delegate.binaryField(fieldInfo, BytesReference.toBytes(BytesReference.bytes(xBuilder)));
    } else {
        delegate.binaryField(fieldInfo, value);
    }
}
 
Example 16
Source Project: spring-content   Source File: ElasticsearchIndexer.java    License: Apache License 2.0 5 votes vote down vote up
void ensureAttachmentPipeline() throws IOException {
	GetPipelineRequest getRequest = new GetPipelineRequest(SPRING_CONTENT_ATTACHMENT);
	GetPipelineResponse res = client.ingest().getPipeline(getRequest, RequestOptions.DEFAULT);
	if (!res.isFound()) {
		String source = "{\"description\":\"Extract attachment information encoded in Base64 with UTF-8 charset\"," +
				"\"processors\":[{\"attachment\":{\"field\":\"data\"}}]}";
		PutPipelineRequest put = new PutPipelineRequest(SPRING_CONTENT_ATTACHMENT,
				new BytesArray(source.getBytes(StandardCharsets.UTF_8)),
				XContentType.JSON);
		WritePipelineResponse wpr = client.ingest().putPipeline(put, RequestOptions.DEFAULT);
		Assert.isTrue(wpr.isAcknowledged(), "Attachment pipeline not acknowledged by server");
	}
}
 
Example 17
private SearchResponse mockSearchResponse(String searchIndex) {
    SearchResponse searchResponse = mock(SearchResponse.class);
    Map<String, DocumentField> fields = new HashMap<>();
    SearchHit searchHit = new SearchHit(101, "xxx", null, fields);
    BytesReference source = new BytesArray("{\"uuid\":\"13456565\",\"createdBy\":\"user\",\"lastUpdateTimestamp\":\"2019-05-20 12:00:00\",\"enabled\":true,\"trusted\":true,\"detectorConfig\":{\"hyperparams\":{\"alpha\":0.5,\"beta\":0.6},\"trainingMetaData\":{\"alpha\":0.5},\"params\":{\"upperWeak\":123}}}}");
    searchHit.sourceRef(source);
    SearchHit[] bunchOfSearchHits = new SearchHit[1];
    bunchOfSearchHits[0] = searchHit;
    SearchHits searchHits = new SearchHits(bunchOfSearchHits, 1, 1);
    when(searchResponse.getHits()).thenReturn(searchHits);
    return searchResponse;
}
 
Example 18
private SearchResponse mockSearchResponse(String searchIndex) {
    SearchResponse searchResponse = mock(SearchResponse.class);
    Map<String, DocumentField> fields = new HashMap<>();
    SearchHit searchHit = new SearchHit(101, "xxx", null, fields);
    BytesReference source = new BytesArray("{}");
    searchHit.sourceRef(source);
    SearchHit[] bunchOfSearchHits = new SearchHit[1];
    bunchOfSearchHits[0] = searchHit;
    SearchHits searchHits = new SearchHits(bunchOfSearchHits, 1, 1);
    when(searchResponse.getHits()).thenReturn(searchHits);
    return searchResponse;
}
 
Example 19
Source Project: elasticsearch-xml   Source File: XmlXContentBuilder.java    License: Apache License 2.0 5 votes vote down vote up
public XmlXContentBuilder field(XContentBuilderString name, Text value) throws IOException {
    field(name);
    if (value.hasBytes() && value.bytes().hasArray()) {
        generator.writeUTF8String(value.bytes().array(), value.bytes().arrayOffset(), value.bytes().length());
        return this;
    }
    if (value.hasString()) {
        generator.writeString(value.string());
        return this;
    }
    // TODO: TextBytesOptimization we can use a buffer here to convert it? maybe add a request to jackson to support InputStream as well?
    BytesArray bytesArray = value.bytes().toBytesArray();
    generator.writeUTF8String(bytesArray.array(), bytesArray.arrayOffset(), bytesArray.length());
    return this;
}
 
Example 20
@Test
public void toStr() throws Exception {
  UpdateRequest request = new UpdateRequest("test", "type1", "1")
      .script(mockInlineScript("ctx._source.body = \"foo\""));
  Assert.assertEquals(ElasticsearchChannel.toString(request),
      "update {[test][type1][1], script[Script{type=inline, lang='mock', idOrCode='ctx._source.body = \"foo\"', options={}, params={}}], detect_noop[true]}");
  request = new UpdateRequest("test", "type1", "1").fromXContent(
      createParser(JsonXContent.jsonXContent, new BytesArray("{\"doc\": {\"body\": \"bar\"}}")));
  Assert.assertEquals(ElasticsearchChannel.toString(request),
      "update {[test][type1][1], doc[index {[null][null][null], source[{\"body\":\"bar\"}]}], detect_noop[true]}");
}
 
Example 21
@Test
public void testGetDefaultIndexPatternWhenConfigIsCurrentAndDefaultIsNotSet() {
    // given current config doesnt exist
    Map<String, BytesReference> docs = new HashMap<>();
    docs.put(ConfigurationSettings.DEFAULT_KIBANA_VERSION, new BytesArray("{\"somekey\":\"avalue\"}"));
    givenSearchResultForDocuments(".kibana.foo", docs);
    String defaultPattern = utils.getDefaultIndexPattern(".kibana.foo", "test-index-pattern");
    // assert we return empty to do nothing
    assertEquals("test-index-pattern", defaultPattern);
}
 
Example 22
Source Project: Elasticsearch   Source File: LineContext.java    License: Apache License 2.0 5 votes vote down vote up
public Map<String, Object> sourceAsMap() {
    if (parsedSource == null) {
        try {
            parsedSource = XContentHelper.convertToMap(new BytesArray(rawSource), false).v2();
        } catch (NullPointerException e) {
            return null;
        }
    }
    return parsedSource;
}
 
Example 23
@Before
public void setUp() {
    KibanaUtils utils = new KibanaUtils(settings, pluginClient);
    seeder = new KibanaSeed(settings, loader, pluginClient, utils);
    context = new OpenshiftRequestContextFactory.OpenshiftRequestContext(USER, TOKEN, true, 
            new HashSet<Project>(), ".kibana_123", KibanaIndexMode.SHARED_OPS, Collections.emptyList());
    when(loader.getOperationsMappingsTemplate()).thenReturn("{\"foo\":\"bar\"");
    when(pluginClient.updateDocument(anyString(), anyString(), anyString(), anyString())).thenReturn(mock(UpdateResponse.class));
   
    GetResponse response = new GetResponse(new GetResult(context.getKibanaIndex(), 
            "config", ConfigurationSettings.DEFAULT_KIBANA_VERSION, 1L, false, 
            new BytesArray("{\"defaultIndex\":\"\"}"), null));
    when(pluginClient.getDocument(eq(context.getKibanaIndex()), eq("config"),
            eq(ConfigurationSettings.DEFAULT_KIBANA_VERSION))).thenReturn(response);
}
 
Example 24
/**
 * Ensures that that the cache id is taking into consideration the index changes. See #73.
 */
@Test
public void testCiapiOutput() throws IOException, RestException, ExecutionException, InterruptedException {
  assertAcked(prepareCreate("email").addMapping("email", "id", "type=string", "content_md5", "type=string"));
  assertAcked(prepareCreate("ciapioutput").addMapping("ciapioutput", "id", "type=string", "content_md5", "type=string"));

  ensureGreen();

  indexRandom(true,
          client().prepareIndex("email", "email", "1").setSource("id", "1", "content_md5", "595b6b645b7d9548c8c462e889267477"),
          client().prepareIndex("email", "email", "2").setSource("id", "2", "content_md5", "6aa71033aef3f529926fe3840c6c0a7e"),

          client().prepareIndex("ciapioutput", "ciapioutput", "1").setSource("id", "1", "content_md5", "595b6b645b7d9548c8c462e889267477") );

  // Retrieves all the md5 from ciapioutput that does not appear in email
  String q = boolQuery().must(
               matchAllQuery()
             ).mustNot(
                filterJoin("content_md5").indices("ciapioutput").types("ciapioutput").path("content_md5").query(
                  matchAllQuery()
                )
             ).toString();
  String body = "{ \"query\" : " + q + "}";

  HttpResponse response = httpClient().method("GET").path("/email/_coordinate_search").body(body).execute();
  assertThat(response.getStatusCode(), equalTo(RestStatus.OK.getStatus()));
  Map<String, Object> map = XContentHelper.convertToMap(new BytesArray(response.getBody().getBytes("UTF-8")), false).v2();
  assertThat((Integer) ((Map) map.get("hits")).get("total"), equalTo(1));

  // Add missing md5 in ciapioutput
  indexRandom(true,
          client().prepareIndex("ciapioutput", "ciapioutput", "2").setSource("id", "2", "content_md5", "6aa71033aef3f529926fe3840c6c0a7e") );

  // It should now return an empty result set
  response = httpClient().method("GET").path("/email/_coordinate_search").body(body).execute();
  assertThat(response.getStatusCode(), equalTo(RestStatus.OK.getStatus()));
  map = XContentHelper.convertToMap(new BytesArray(response.getBody().getBytes("UTF-8")), false).v2();
  assertThat((Integer) ((Map) map.get("hits")).get("total"), equalTo(0));
}
 
Example 25
@Test
public void testOrderByApi() throws IOException, RestException, ExecutionException, InterruptedException {
  assertAcked(prepareCreate("index1").addMapping("type", "id", "type=string", "foreign_key", "type=string"));

  // Enforce one single shard for index2
  Map<String, Object> indexSettings = new HashMap<>();
  indexSettings.put("number_of_shards", 1);
  assertAcked(prepareCreate("index2").setSettings(indexSettings).addMapping("type", "id", "type=string", "tag", "type=string"));

  ensureGreen();

  indexRandom(true,
    client().prepareIndex("index1", "type", "1").setSource("id", "1", "foreign_key", new String[]{"1", "3"}),
    client().prepareIndex("index1", "type", "2").setSource("id", "2"),
    client().prepareIndex("index1", "type", "3").setSource("id", "3", "foreign_key", new String[]{"2"}),
    client().prepareIndex("index1", "type", "4").setSource("id", "4", "foreign_key", new String[]{"4"}),

    client().prepareIndex("index2", "type", "1").setSource("id", "1", "tag", "aaa"),
    client().prepareIndex("index2", "type", "2").setSource("id", "2", "tag", "aaa aaa"),
    client().prepareIndex("index2", "type", "3").setSource("id", "3", "tag", "aaa"),
    client().prepareIndex("index2", "type", "4").setSource("id", "4", "tag", "aaa aaa") );

  // Order by doc score, and take only the first element of the index2 shard
  // It should therefore pick only odd document ids (with tag:aaa) due to scoring.
  String q = boolQuery().filter(
              filterJoin("foreign_key").indices("index2").types("type").path("id").query(
                  boolQuery().filter(termQuery("tag", "aaa"))
              ).orderBy(TermsByQueryRequest.Ordering.DOC_SCORE).maxTermsPerShard(1)).toString();
  String body = "{ \"query\" : " + q + "}";

  HttpResponse response = httpClient().method("GET").path("/_coordinate_search").body(body).execute();
  assertThat(response.getStatusCode(), equalTo(RestStatus.OK.getStatus()));
  Map<String, Object> map = XContentHelper.convertToMap(new BytesArray(response.getBody().getBytes("UTF-8")), false).v2();
  // Only one document contains a odd document id as foreign key.
  assertThat((Integer) ((Map) map.get("hits")).get("total"), equalTo(1));
}
 
Example 26
Source Project: Elasticsearch   Source File: BlobRecoveryHandler.java    License: Apache License 2.0 5 votes vote down vote up
private void deleteFilesRequest(BytesArray[] digests) {
    transportService.submitRequest(
        request.targetNode(),
        BlobRecoveryTarget.Actions.DELETE_FILE,
        new BlobRecoveryDeleteRequest(request.recoveryId(), digests),
        TransportRequestOptions.EMPTY,
        EmptyTransportResponseHandler.INSTANCE_SAME
    ).txGet();
}
 
Example 27
Source Project: crate   Source File: StreamInput.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Reads a bytes reference from this stream, might hold an actual reference to the underlying
 * bytes of the stream.
 */
public BytesReference readBytesReference(int length) throws IOException {
    if (length == 0) {
        return BytesArray.EMPTY;
    }
    byte[] bytes = new byte[length];
    readBytes(bytes, 0, length);
    return new BytesArray(bytes, 0, length);
}
 
Example 28
Source Project: crate   Source File: ESBlobStoreContainerTestCase.java    License: Apache License 2.0 5 votes vote down vote up
public void testVerifyOverwriteFails() throws IOException {
    try (BlobStore store = newBlobStore()) {
        final String blobName = "foobar";
        final BlobContainer container = store.blobContainer(new BlobPath());
        byte[] data = randomBytes(randomIntBetween(10, scaledRandomIntBetween(1024, 1 << 16)));
        final BytesArray bytesArray = new BytesArray(data);
        writeBlob(container, blobName, bytesArray, true);
        // should not be able to overwrite existing blob
        expectThrows(FileAlreadyExistsException.class, () -> writeBlob(container, blobName, bytesArray, true));
        container.deleteBlob(blobName);
        writeBlob(container, blobName, bytesArray, true); // after deleting the previous blob, we should be able to write to it again
    }
}
 
Example 29
Source Project: crate   Source File: FieldsVisitor.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void binaryField(FieldInfo fieldInfo, byte[] value) throws IOException {
    if (sourceFieldName.equals(fieldInfo.name)) {
        source = new BytesArray(value);
    } else if (IdFieldMapper.NAME.equals(fieldInfo.name)) {
        id = Uid.decodeId(value);
    } else {
        addValue(fieldInfo.name, new BytesRef(value));
    }
}
 
Example 30
Source Project: crate   Source File: ChecksumBlobStoreFormat.java    License: Apache License 2.0 5 votes vote down vote up
private void writeTo(final T obj, final String blobName, final CheckedConsumer<BytesArray, IOException> consumer) throws IOException {
    final BytesReference bytes;
    try (BytesStreamOutput bytesStreamOutput = new BytesStreamOutput()) {
        if (compress) {
            try (StreamOutput compressedStreamOutput = CompressorFactory.COMPRESSOR.streamOutput(bytesStreamOutput)) {
                write(obj, compressedStreamOutput);
            }
        } else {
            write(obj, bytesStreamOutput);
        }
        bytes = bytesStreamOutput.bytes();
    }
    try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
        final String resourceDesc = "ChecksumBlobStoreFormat.writeBlob(blob=\"" + blobName + "\")";
        try (OutputStreamIndexOutput indexOutput = new OutputStreamIndexOutput(resourceDesc, blobName, outputStream, BUFFER_SIZE)) {
            CodecUtil.writeHeader(indexOutput, codec, VERSION);
            try (OutputStream indexOutputOutputStream = new IndexOutputOutputStream(indexOutput) {
                @Override
                public void close() {
                    // this is important since some of the XContentBuilders write bytes on close.
                    // in order to write the footer we need to prevent closing the actual index input.
                }
            }) {
                bytes.writeTo(indexOutputOutputStream);
            }
            CodecUtil.writeFooter(indexOutput);
        }
        consumer.accept(new BytesArray(outputStream.toByteArray()));
    }
}