org.elasticsearch.common.xcontent.XContentHelper Java Examples

The following examples show how to use org.elasticsearch.common.xcontent.XContentHelper. 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 File: MappingMetaData.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
public MappingMetaData(CompressedXContent mapping) throws IOException {
    Map<String, Object> mappingMap;
    try (XContentParser parser = XContentHelper.createParser(mapping.compressedReference())) {
        mappingMap = parser.mapOrdered();
    }
    if (mappingMap.containsKey(MAPPING_VERSION)) {
        this.mappingVersion = (int)mappingMap.get(MAPPING_VERSION);
        mappingMap.remove(MAPPING_VERSION);
    } else {
        this.mappingVersion = 1;
    }
    XContentBuilder mappingBuilder = XContentFactory.jsonBuilder().map(mappingMap);
    this.source = new CompressedXContent(mappingBuilder.bytes());
    if (mappingMap.size() != 1) {
        throw new IllegalStateException("Can't derive type from mapping, no root type: " + mapping.string());
    }
    this.type = mappingMap.keySet().iterator().next();
    initMappers((Map<String, Object>) mappingMap.get(this.type));
}
 
Example #2
Source File: XContentTestUtilsTests.java    From crate with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
public void testInsertIntoXContent() throws IOException {
    XContentBuilder builder = JsonXContent.contentBuilder();
    builder.startObject();
    builder.endObject();
    builder = XContentTestUtils.insertIntoXContent(XContentType.JSON.xContent(), BytesReference.bytes(builder),
            Collections.singletonList(""), () -> "inn.er1", () -> new HashMap<>());
    builder = XContentTestUtils.insertIntoXContent(XContentType.JSON.xContent(), BytesReference.bytes(builder),
            Collections.singletonList(""), () -> "field1", () -> "value1");
    builder = XContentTestUtils.insertIntoXContent(XContentType.JSON.xContent(), BytesReference.bytes(builder),
            Collections.singletonList("inn\\.er1"), () -> "inner2", () -> new HashMap<>());
    builder = XContentTestUtils.insertIntoXContent(XContentType.JSON.xContent(), BytesReference.bytes(builder),
            Collections.singletonList("inn\\.er1"), () -> "field2", () -> "value2");
    try (XContentParser parser = XContentHelper.createParser(NamedXContentRegistry.EMPTY,
        DeprecationHandler.THROW_UNSUPPORTED_OPERATION, BytesReference.bytes(builder), builder.contentType())) {
        Map<String, Object> map = parser.map();
        assertEquals(2, map.size());
        assertEquals("value1", map.get("field1"));
        assertThat(map.get("inn.er1"), instanceOf(Map.class));
        Map<String, Object> innerMap = (Map<String, Object>) map.get("inn.er1");
        assertEquals(2, innerMap.size());
        assertEquals("value2", innerMap.get("field2"));
        assertThat(innerMap.get("inner2"), instanceOf(Map.class));
        assertEquals(0, ((Map<String, Object>) innerMap.get("inner2")).size());
    }
}
 
Example #3
Source File: AliasMetaData.java    From crate with 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 #4
Source File: TransportBulkCreateIndicesAction.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
private void addMappings(Map<String, Map<String, Object>> mappings, File mappingsDir) {
    File[] mappingsFiles = mappingsDir.listFiles();
    for (File mappingFile : mappingsFiles) {
        if (mappingFile.isHidden()) {
            continue;
        }
        int lastDotIndex = mappingFile.getName().lastIndexOf('.');
        String mappingType = lastDotIndex != -1 ? mappingFile.getName().substring(0, lastDotIndex) : mappingFile.getName();
        try {
            String mappingSource = Streams.copyToString(new InputStreamReader(new FileInputStream(mappingFile), Charsets.UTF_8));
            if (mappings.containsKey(mappingType)) {
                XContentHelper.mergeDefaults(mappings.get(mappingType), parseMapping(mappingSource));
            } else {
                mappings.put(mappingType, parseMapping(mappingSource));
            }
        } catch (Exception e) {
            logger.warn("failed to read / parse mapping [" + mappingType + "] from location [" + mappingFile + "], ignoring...", e);
        }
    }
}
 
Example #5
Source File: SampleIndexTestCase.java    From elasticsearch-carrot2 with Apache License 2.0 6 votes vote down vote up
protected static Map<String, Object> checkHttpResponse(HttpResponse response) throws IOException {
   byte[] responseBytes = response.getEntity().getContent().readAllBytes();
   String responseString = new String(responseBytes, StandardCharsets.UTF_8);

   String responseDescription =
       "HTTP response status: " + response.getStatusLine().toString() + ", " +
           "HTTP body: " + responseString;

   Assertions.assertThat(response.getStatusLine().getStatusCode())
       .describedAs(responseDescription)
       .isEqualTo(HttpStatus.SC_OK);

   try (XContentParser parser = XContentHelper.createParser(
       NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION,
       new BytesArray(responseBytes),
       XContentType.fromMediaTypeOrFormat(response.getFirstHeader(HttpHeaders.CONTENT_TYPE).getValue()))) {
      Map<String, Object> map = parser.map();
      Assertions.assertThat(map)
          .describedAs(responseDescription)
          .doesNotContainKey("error");
      return map;
   }
}
 
Example #6
Source File: WriterProjector.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public void write(Row row) {
    for (CollectExpression<Row, ?> collectExpression : collectExpressions) {
        collectExpression.setNextRow(row);
    }
    Map doc = (Map) row.get(0);
    XContentHelper.update(doc, overwrites, false);
    try {
        builder.map(doc);
        builder.flush();
        outputStream.write(NEW_LINE);
    } catch (IOException e) {
        throw new UnhandledServerException("Failed to write row to output", e);
    }
}
 
Example #7
Source File: ChecksumBlobStoreFormat.java    From crate with 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 {
    final BytesReference bytes = Streams.readFully(blobContainer.readBlob(blobName));
    final String resourceDesc = "ChecksumBlobStoreFormat.readBlob(blob=\"" + blobName + "\")";
    try (ByteArrayIndexInput indexInput =
             new ByteArrayIndexInput(resourceDesc, BytesReference.toBytes(bytes))) {
        CodecUtil.checksumEntireFile(indexInput);
        CodecUtil.checkHeader(indexInput, codec, VERSION, VERSION);
        long filePointer = indexInput.getFilePointer();
        long contentSize = indexInput.length() - CodecUtil.footerLength() - filePointer;
        try (XContentParser parser = XContentHelper.createParser(namedXContentRegistry, LoggingDeprecationHandler.INSTANCE,
                                                                 bytes.slice((int) filePointer, (int) contentSize), XContentType.SMILE)) {
            return reader.apply(parser);
        }
    } catch (CorruptIndexException | IndexFormatTooOldException | IndexFormatTooNewException ex) {
        // we trick this into a dedicated exception with the original stacktrace
        throw new CorruptStateException(ex);
    }
}
 
Example #8
Source File: TransportShardUpsertAction.java    From Elasticsearch with 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 #9
Source File: BlobStoreRepository.java    From Elasticsearch with 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 #10
Source File: BaseTransportCoordinateSearchAction.java    From siren-join with GNU Affero General Public License v3.0 6 votes vote down vote up
protected Tuple<XContentType, Map<String, Object>> parseSource(BytesReference source) {
  // nothing to parse...
  if (source == null || source.length() == 0) {
    return null;
  }

  try {
    Tuple<XContentType, Map<String, Object>> parsedSource = XContentHelper.convertToMap(source, false);
    logger.debug("{}: Parsed source: {}", Thread.currentThread().getName(), parsedSource);
    return parsedSource;
  }
  catch (Throwable e) {
      String sSource = "_na_";
      try {
          sSource = XContentHelper.convertToJson(source, false);
      }
      catch (Throwable e1) { /* ignore  */ }
      throw new ElasticsearchParseException("Failed to parse source [" + sSource + "]", e);
  }
}
 
Example #11
Source File: RestClearScrollAction.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
public static void buildFromContent(BytesReference content, ClearScrollRequest clearScrollRequest) {
    try (XContentParser parser = XContentHelper.createParser(content)) {
        if (parser.nextToken() != XContentParser.Token.START_OBJECT) {
            throw new IllegalArgumentException("Malformed content, must start with an object");
        } else {
            XContentParser.Token token;
            String currentFieldName = null;
            while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
                if (token == XContentParser.Token.FIELD_NAME) {
                    currentFieldName = parser.currentName();
                } else if ("scroll_id".equals(currentFieldName) && token == XContentParser.Token.START_ARRAY) {
                    while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
                        if (token.isValue() == false) {
                            throw new IllegalArgumentException("scroll_id array element should only contain scroll_id");
                        }
                        clearScrollRequest.addScrollId(parser.text());
                    }
                } else {
                    throw new IllegalArgumentException("Unknown parameter [" + currentFieldName + "] in request body or parameter is of the wrong type[" + token + "] ");
                }
            }
        }
    } catch (IOException e) {
        throw new IllegalArgumentException("Failed to parse request body", e);
    }
}
 
Example #12
Source File: RestSearchScrollAction.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
public static void buildFromContent(BytesReference content, SearchScrollRequest searchScrollRequest) {
    try (XContentParser parser = XContentHelper.createParser(content)) {
        if (parser.nextToken() != XContentParser.Token.START_OBJECT) {
            throw new IllegalArgumentException("Malforrmed content, must start with an object");
        } else {
            XContentParser.Token token;
            String currentFieldName = null;
            while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
                if (token == XContentParser.Token.FIELD_NAME) {
                    currentFieldName = parser.currentName();
                } else if ("scroll_id".equals(currentFieldName) && token == XContentParser.Token.VALUE_STRING) {
                    searchScrollRequest.scrollId(parser.text());
                } else if ("scroll".equals(currentFieldName) && token == XContentParser.Token.VALUE_STRING) {
                    searchScrollRequest.scroll(new Scroll(TimeValue.parseTimeValue(parser.text(), null, "scroll")));
                } else {
                    throw new IllegalArgumentException("Unknown parameter [" + currentFieldName + "] in request body or parameter is of the wrong type[" + token + "] ");
                }
            }
        }
    } catch (IOException e) {
        throw new IllegalArgumentException("Failed to parse request body", e);
    }
}
 
Example #13
Source File: MetaDataCreateIndexService.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
private void addMappings(Map<String, Map<String, Object>> mappings, Path mappingsDir) throws IOException {
    try (DirectoryStream<Path> stream = Files.newDirectoryStream(mappingsDir)) {
        for (Path mappingFile : stream) {
            final String fileName = mappingFile.getFileName().toString();
            if (FileSystemUtils.isHidden(mappingFile)) {
                continue;
            }
            int lastDotIndex = fileName.lastIndexOf('.');
            String mappingType = lastDotIndex != -1 ? mappingFile.getFileName().toString().substring(0, lastDotIndex) : mappingFile.getFileName().toString();
            try (BufferedReader reader = Files.newBufferedReader(mappingFile, Charsets.UTF_8)) {
                String mappingSource = Streams.copyToString(reader);
                if (mappings.containsKey(mappingType)) {
                    XContentHelper.mergeDefaults(mappings.get(mappingType), parseMapping(mappingSource));
                } else {
                    mappings.put(mappingType, parseMapping(mappingSource));
                }
            } catch (Exception e) {
                logger.warn("failed to read / parse mapping [" + mappingType + "] from location [" + mappingFile + "], ignoring...", e);
            }
        }
    }
}
 
Example #14
Source File: ExportParser.java    From elasticsearch-inout-plugin with Apache License 2.0 5 votes vote down vote up
/**
 * Main method of this class to parse given payload of _export action
 *
 * @param context
 * @param source
 * @throws SearchParseException
 */
public void parseSource(ExportContext context, BytesReference source) throws SearchParseException {
    XContentParser parser = null;
    try {
        if (source != null && source.length() != 0) {
            parser = XContentFactory.xContent(source).createParser(source);
            XContentParser.Token token;
            while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
                if (token == XContentParser.Token.FIELD_NAME) {
                    String fieldName = parser.currentName();
                    parser.nextToken();
                    SearchParseElement element = elementParsers.get(fieldName);
                    if (element == null) {
                        throw new SearchParseException(context, "No parser for element [" + fieldName + "]");
                    }
                    element.parse(parser, context);
                } else if (token == null) {
                    break;
                }
            }
        }
        validate(context);
    } catch (Exception e) {
        String sSource = "_na_";
        try {
            sSource = XContentHelper.convertToJson(source, false);
        } catch (Throwable e1) {
            // ignore
        }
        throw new SearchParseException(context, "Failed to parse source [" + sSource + "]", e);
    } finally {
        if (parser != null) {
            parser.close();
        }
    }
}
 
Example #15
Source File: RestApiTest.java    From siren-join with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
public void testCoordinateSearchApi() throws IOException, RestException, ExecutionException, InterruptedException {
  assertAcked(prepareCreate("index1").addMapping("type", "id", "type=string", "foreign_key", "type=string"));
  assertAcked(prepareCreate("index2").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[]{"1", "4"}),

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

  // Check body search query with filter join
  String q = boolQuery().filter(
              filterJoin("foreign_key").indices("index2").types("type").path("id").query(
                  boolQuery().filter(termQuery("tag", "aaa"))
          )).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();
  assertThat((Integer) ((Map) map.get("hits")).get("total"), equalTo(3));

  // Check uri search
  response = httpClient().method("GET").path("/_coordinate_search").addParam("q", "tag:aaa").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(2));
}
 
Example #16
Source File: RestFilterJoinCacheTest.java    From siren-join with GNU Affero General Public License v3.0 5 votes vote down vote up
private void runQuery() throws IOException {
  // Check body search query with filter join
  String q = boolQuery().filter(
          filterJoin("foreign_key").indices("index2").types("type").path("id").query(
                  boolQuery().filter(termQuery("tag", "aaa"))
          )).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();
  assertThat((Integer) ((Map) map.get("hits")).get("total"), equalTo(3));
}
 
Example #17
Source File: RestApiTest.java    From siren-join with GNU Affero General Public License v3.0 5 votes vote down vote up
@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 #18
Source File: MappingMetaData.java    From crate with Apache License 2.0 5 votes vote down vote up
public MappingMetaData(CompressedXContent mapping) throws IOException {
    this.source = mapping;
    Map<String, Object> mappingMap = XContentHelper.convertToMap(mapping.compressedReference(), true).v2();
    if (mappingMap.size() != 1) {
        throw new IllegalStateException("Can't derive type from mapping, no root type: " + mapping.string());
    }
    this.type = mappingMap.keySet().iterator().next();
    initMappers((Map<String, Object>) mappingMap.get(this.type));
}
 
Example #19
Source File: AbstractXContentTestCase.java    From crate with Apache License 2.0 5 votes vote down vote up
public static <T extends ToXContent> XContentTester<T> xContentTester(
        CheckedBiFunction<XContent, BytesReference, XContentParser, IOException> createParser,
        Supplier<T> instanceSupplier,
        ToXContent.Params toXContentParams,
        CheckedFunction<XContentParser, T, IOException> fromXContent) {
    return new XContentTester<T>(
            createParser,
            instanceSupplier,
            (testInstance, xContentType) ->
                    XContentHelper.toXContent(testInstance, xContentType, toXContentParams, false),
            fromXContent);
}
 
Example #20
Source File: ConfigurationLoader.java    From openshift-elasticsearch-plugin with Apache License 2.0 5 votes vote down vote up
private Settings toSettings(final BytesReference ref, final String type) {
    if (ref == null || ref.length() == 0) {
        return null;
    }
    
    XContentParser parser = null;

    try {
        parser = XContentHelper.createParser(NamedXContentRegistry.EMPTY, ref, XContentType.JSON);
        parser.nextToken();
        parser.nextToken();
     
        if(!type.equals((parser.currentName()))) {
            return null;
        }
        
        parser.nextToken();
        
        return Settings.builder().put(new JsonSettingsLoader(true).load(parser.binaryValue())).build();
    } catch (final IOException e) {
        throw ExceptionsHelper.convertToElastic(e);
    } finally {
        if(parser != null) {
            parser.close();
        }
    }
}
 
Example #21
Source File: ACLDocumentManager.java    From openshift-elasticsearch-plugin with Apache License 2.0 5 votes vote down vote up
@Override
public BulkRequest buildRequest(Client client, BulkRequestBuilder builder, Collection<SearchGuardACLDocument> docs) throws IOException{
    for (SearchGuardACLDocument doc : docs) {
        logContent("Expired doc {} to be: {}", doc.getType(), doc);
        Map<String, Object> content = new HashMap<>();
        content.put(doc.getType(), new BytesArray(XContentHelper.toString(doc)));
        IndexRequestBuilder indexBuilder = client
                .prepareIndex(searchGuardIndex, doc.getType(), SEARCHGUARD_CONFIG_ID)
                .setOpType(OpType.INDEX)
                .setVersion(doc.getVersion())
                .setSource(content);
        builder.add(indexBuilder.request());
    }
    return builder.request();
}
 
Example #22
Source File: PKLookupOperation.java    From crate with Apache License 2.0 5 votes vote down vote up
@Nullable
public static Doc lookupDoc(IndexShard shard, String id, long version, VersionType versionType, long seqNo, long primaryTerm) {
    Term uidTerm = new Term(IdFieldMapper.NAME, Uid.encodeId(id));
    Engine.Get get = new Engine.Get(id, uidTerm)
        .version(version)
        .versionType(versionType)
        .setIfSeqNo(seqNo)
        .setIfPrimaryTerm(primaryTerm);

    try (Engine.GetResult getResult = shard.get(get)) {
        var docIdAndVersion = getResult.docIdAndVersion();
        if (docIdAndVersion == null) {
            return null;
        }
        SourceFieldVisitor visitor = new SourceFieldVisitor();
        try {
            docIdAndVersion.reader.document(docIdAndVersion.docId, visitor);
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
        return new Doc(
            docIdAndVersion.docId,
            shard.shardId().getIndexName(),
            id,
            docIdAndVersion.version,
            docIdAndVersion.seqNo,
            docIdAndVersion.primaryTerm,
            XContentHelper.toMap(visitor.source(), XContentType.JSON),
            () -> visitor.source().utf8ToString()
        );
    }
}
 
Example #23
Source File: DfsOnlyRequest.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public String toString() {
    String sSource = "_na_";
    try {
        sSource = XContentHelper.convertToJson(searchRequest.source(), false);
    } catch (IOException e) {
        // ignore
    }
    return "[" + Arrays.toString(indices) + "]" + Arrays.toString(types()) + ", source[" + sSource + "]";
}
 
Example #24
Source File: SearchIntoRequest.java    From elasticsearch-inout-plugin with Apache License 2.0 5 votes vote down vote up
@Override
public String toString() {
    String sSource = "_na_";
    try {
        sSource = XContentHelper.convertToJson(source, false);
    } catch (Exception e) {
        // ignore
    }
    return "[" + Arrays.toString(indices) + "]" + Arrays.toString(
            types) + ", querySource[" + sSource + "]";
}
 
Example #25
Source File: ColumnPolicyIntegrationTest.java    From crate with Apache License 2.0 5 votes vote down vote up
@Test
public void testStrictPartitionedTableUpdate() throws Exception {
    execute("create table numbers (" +
            "  num int, " +
            "  odd boolean," +
            "  prime boolean" +
            ") partitioned by (odd) with (column_policy='strict', number_of_replicas=0)");
    ensureYellow();

    GetIndexTemplatesResponse response = client().admin().indices()
        .prepareGetTemplates(PartitionName.templateName(sqlExecutor.getCurrentSchema(), "numbers"))
        .execute().actionGet();
    assertThat(response.getIndexTemplates().size(), is(1));
    IndexTemplateMetaData template = response.getIndexTemplates().get(0);
    CompressedXContent mappingStr = template.mappings().get(Constants.DEFAULT_MAPPING_TYPE);
    assertThat(mappingStr, is(notNullValue()));
    Tuple<XContentType, Map<String, Object>> typeAndMap = XContentHelper.convertToMap(mappingStr.compressedReference(), false);
    @SuppressWarnings("unchecked")
    Map<String, Object> mapping = (Map<String, Object>) typeAndMap.v2().get(Constants.DEFAULT_MAPPING_TYPE);
    assertThat(decodeMappingValue(mapping.get("dynamic")), is(ColumnPolicy.STRICT));

    execute("insert into numbers (num, odd, prime) values (?, ?, ?)",
        new Object[]{6, true, false});
    execute("refresh table numbers");

    Map<String, Object> sourceMap = getSourceMap(
        new PartitionName(new RelationName("doc", "numbers"), Arrays.asList("true")).asIndexName());
    assertThat(decodeMappingValue(sourceMap.get("dynamic")), is(ColumnPolicy.STRICT));

    expectedException.expect(SQLActionException.class);
    expectedException.expectMessage("Column perfect unknown");
    execute("update numbers set num=?, perfect=? where num=6",
        new Object[]{28, true});
}
 
Example #26
Source File: PutIndexTemplateRequest.java    From crate with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the aliases that will be associated with the index when it gets created
 */
public PutIndexTemplateRequest aliases(BytesReference source) {
    // EMPTY is safe here because we never call namedObject
    try (XContentParser parser = XContentHelper
            .createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, source)) {
        //move to the first alias
        parser.nextToken();
        while ((parser.nextToken()) != XContentParser.Token.END_OBJECT) {
            alias(Alias.fromXContent(parser));
        }
        return this;
    } catch (IOException e) {
        throw new ElasticsearchParseException("Failed to parse aliases", e);
    }
}
 
Example #27
Source File: SuggestRequest.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public String toString() {
    String sSource = "_na_";
    try {
        sSource = XContentHelper.convertToJson(suggestSource, false);
    } catch (Exception e) {
        // ignore
    }
    return "[" + Arrays.toString(indices) + "]" + ", suggestSource[" + sSource + "]";
}
 
Example #28
Source File: PutIndexedScriptRequest.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public String toString() {
    String sSource = "_na_";
    try {
        sSource = XContentHelper.convertToJson(source, false);
    } catch (Exception e) {
        // ignore
    }
    return "index {[" + ScriptService.SCRIPT_INDEX + "][" + scriptLang + "][" + id + "], source[" + sSource + "]}";
}
 
Example #29
Source File: AbstractConfigurationValidator.java    From deprecated-security-advanced-modules with Apache License 2.0 5 votes vote down vote up
private boolean checkDatatypes() throws Exception {
    String contentAsJson = XContentHelper.convertToJson(content, false, XContentType.JSON);
    try (JsonParser parser = factory.createParser(contentAsJson)) {
        JsonToken token = null;
        while ((token = parser.nextToken()) != null) {
            if (token.equals(JsonToken.FIELD_NAME)) {
                String currentName = parser.getCurrentName();
                DataType dataType = allowedKeys.get(currentName);
                if (dataType != null) {
                    JsonToken valueToken = parser.nextToken();
                    switch (dataType) {
                        case STRING:
                            if (!valueToken.equals(JsonToken.VALUE_STRING)) {
                                wrongDatatypes.put(currentName, "String expected");
                            }
                            break;
                        case ARRAY:
                            if (!valueToken.equals(JsonToken.START_ARRAY) && !valueToken.equals(JsonToken.END_ARRAY)) {
                                wrongDatatypes.put(currentName, "Array expected");
                            }
                            break;
                        case OBJECT:
                            if (!valueToken.equals(JsonToken.START_OBJECT) && !valueToken.equals(JsonToken.END_OBJECT)) {
                                wrongDatatypes.put(currentName, "Object expected");
                            }
                            break;
                    }
                }
            }
        }
        return wrongDatatypes.isEmpty();
    }
}
 
Example #30
Source File: SearchGuardRolesMappingACLTest.java    From openshift-elasticsearch-plugin with Apache License 2.0 5 votes vote down vote up
@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));
}