Java Code Examples for org.elasticsearch.common.xcontent.XContentHelper

The following examples show how to use org.elasticsearch.common.xcontent.XContentHelper. 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
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 2
Source Project: Elasticsearch   Source File: WriterProjector.java    License: 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 3
Source Project: crate   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 {
    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 4
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 5
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 6
Source Project: Elasticsearch   Source File: RestClearScrollAction.java    License: 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 7
Source Project: Elasticsearch   Source File: RestSearchScrollAction.java    License: 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 8
Source Project: Elasticsearch   Source File: MetaDataCreateIndexService.java    License: 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 9
Source Project: Elasticsearch   Source File: MappingMetaData.java    License: 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 10
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 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 12
Source Project: crate   Source File: XContentTestUtilsTests.java    License: 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 13
Source Project: elasticsearch-carrot2   Source File: SampleIndexTestCase.java    License: 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 14
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 15
public static Map<String, Object> convertJsonToxToStructuredMap(ToXContent jsonContent) {
    Map<String, Object> map = null;
    try {
        final BytesReference bytes = XContentHelper.toXContent(jsonContent, XContentType.JSON, false);
        map = XContentHelper.convertToMap(bytes, false, XContentType.JSON).v2();
    } catch (IOException e1) {
        throw ExceptionsHelper.convertToElastic(e1);
    }

    return map;
}
 
Example 16
public static String convertStructuredMapToJson(Map<String, Object> structuredMap) {
    try {
        return XContentHelper.convertToJson(convertStructuredMapToBytes(structuredMap), false, XContentType.JSON);
    } catch (IOException e) {
        throw new ElasticsearchParseException("Failed to convert map", e);
    }
}
 
Example 17
Source Project: crate   Source File: CreateIndexRequest.java    License: 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 CreateIndexRequest 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 18
public static <T> T serializeToXContentToPojo(ToXContent jsonContent, Class<T> clazz) {
    try {

        if (jsonContent instanceof BytesReference) {
            return serializeToXContentToPojo(((BytesReference) jsonContent).utf8ToString(), clazz);
        }

        final BytesReference bytes = XContentHelper.toXContent(jsonContent, XContentType.JSON, false);
        return DefaultObjectMapper.readValue(bytes.utf8ToString(), clazz);
    } catch (IOException e1) {
        throw ExceptionsHelper.convertToElastic(e1);
    }

}
 
Example 19
public void addTupleToRequestBody(Tuple<XContentType, BytesReference> xContentTuple) {
    if (xContentTuple != null) {
        try {
            auditInfo.put(REQUEST_BODY, XContentHelper.convertToJson(xContentTuple.v2(), false, xContentTuple.v1()));
        } catch (Exception e) {
            auditInfo.put(REQUEST_BODY, "ERROR: Unable to convert to json because of "+e.toString());
        }
    }
}
 
Example 20
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 21
Source Project: Elasticsearch   Source File: DocIndexMetaData.java    License: Apache License 2.0 5 votes vote down vote up
protected DocIndexMetaData merge(DocIndexMetaData other,
                                 TransportPutIndexTemplateAction transportPutIndexTemplateAction,
                                 boolean thisIsCreatedFromTemplate) throws IOException {
    if (schemaEquals(other)) {
        return this;
    } else if (thisIsCreatedFromTemplate) {
        if (this.references.size() < other.references.size()) {
            // this is older, update template and return other
            // settings in template are always authoritative for table information about
            // number_of_shards and number_of_replicas
            updateTemplate(other, transportPutIndexTemplateAction, this.metaData.getSettings());
            // merge the new mapping with the template settings
            return new DocIndexMetaData(
                    functions,
                    IndexMetaData.builder(other.metaData).settings(this.metaData.getSettings()).build(),
                    other.ident).build();
        } else if (references().size() == other.references().size() &&
                   !references().keySet().equals(other.references().keySet())) {
            XContentHelper.update(defaultMappingMap, other.defaultMappingMap, false);
            // update the template with new information
            updateTemplate(this, transportPutIndexTemplateAction, this.metaData.getSettings());
            return this;
        }
        // other is older, just return this
        return this;
    } else {
        throw new TableAliasSchemaException(other.ident.name());
    }
}
 
Example 22
Source Project: elasticsearch-carrot2   Source File: SampleIndexTestCase.java    License: Apache License 2.0 5 votes vote down vote up
protected static void expectErrorResponseWithMessage(HttpResponse response,
                                                     int expectedStatus,
                                                     String messageSubstring) 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(expectedStatus);

   XContentType xContentType = XContentType.fromMediaTypeOrFormat(
       response.getFirstHeader(HttpHeaders.CONTENT_TYPE).getValue());
   try (XContentParser parser = XContentHelper.createParser(
       NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION,
       new BytesArray(responseBytes), xContentType)) {
      Map<String, Object> responseJson = parser.mapOrdered();

      Assertions.assertThat(responseJson)
          .describedAs(responseString)
          .containsKey("error");

      Assertions.assertThat(responseJson.get("error").toString())
          .describedAs(responseString)
          .contains(messageSubstring);
   }
}
 
Example 23
Source Project: Elasticsearch   Source File: InternalSearchHit.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public String sourceAsString() {
    if (source == null) {
        return null;
    }
    try {
        return XContentHelper.convertToJson(sourceRef(), false);
    } catch (IOException e) {
        throw new ElasticsearchParseException("failed to convert source to a json string");
    }
}
 
Example 24
public Query getInnerQuery() throws IOException {
    if (queryParsed) {
        return innerQuery;
    } else {
        if (path == null) {
            throw new QueryParsingException(parseContext, "[nested] requires 'path' field");
        }
        if (!queryFound) {
            throw new QueryParsingException(parseContext, "[nested] requires either 'query' or 'filter' field");
        }

        XContentParser old = parseContext.parser();
        try {
            XContentParser innerParser = XContentHelper.createParser(source);
            parseContext.parser(innerParser);
            setPathLevel();
            try {
                innerQuery = parseContext.parseInnerQuery();
            } finally {
                resetPathLevel();
            }
            queryParsed = true;
            return innerQuery;
        } finally {
            parseContext.parser(old);
        }
    }
}
 
Example 25
Source Project: crate   Source File: MappingMetaData.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Converts the serialized compressed form of the mappings into a parsed map.
 */
public Map<String, Object> sourceAsMap() throws ElasticsearchParseException {
    Map<String, Object> mapping = XContentHelper.convertToMap(source.compressedReference(), true).v2();
    if (mapping.size() == 1 && mapping.containsKey(type())) {
        // the type name is the root value, reduce it
        mapping = (Map<String, Object>) mapping.get(type());
    }
    return mapping;
}
 
Example 26
Source Project: Elasticsearch   Source File: PercolatorQueriesRegistry.java    License: Apache License 2.0 5 votes vote down vote up
Query parsePercolatorDocument(String id, BytesReference source) {
    String type = null;
    BytesReference querySource = null;
    try (XContentParser sourceParser = XContentHelper.createParser(source)) {
        String currentFieldName = null;
        XContentParser.Token token = sourceParser.nextToken(); // move the START_OBJECT
        if (token != XContentParser.Token.START_OBJECT) {
            throw new ElasticsearchException("failed to parse query [" + id + "], not starting with OBJECT");
        }
        while ((token = sourceParser.nextToken()) != XContentParser.Token.END_OBJECT) {
            if (token == XContentParser.Token.FIELD_NAME) {
                currentFieldName = sourceParser.currentName();
            } else if (token == XContentParser.Token.START_OBJECT) {
                if ("query".equals(currentFieldName)) {
                    if (type != null) {
                        return parseQuery(type, sourceParser);
                    } else {
                        XContentBuilder builder = XContentFactory.contentBuilder(sourceParser.contentType());
                        builder.copyCurrentStructure(sourceParser);
                        querySource = builder.bytes();
                        builder.close();
                    }
                } else {
                    sourceParser.skipChildren();
                }
            } else if (token == XContentParser.Token.START_ARRAY) {
                sourceParser.skipChildren();
            } else if (token.isValue()) {
                if ("type".equals(currentFieldName)) {
                    type = sourceParser.text();
                }
            }
        }
        try (XContentParser queryParser = XContentHelper.createParser(querySource)) {
            return parseQuery(type, queryParser);
        }
    } catch (Exception e) {
        throw new PercolatorException(shardId().index(), "failed to parse query [" + id + "]", e);
    }
}
 
Example 27
Source Project: Elasticsearch   Source File: ShapeFetchService.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Fetches the Shape with the given ID in the given type and index.
 *
 * @param getRequest GetRequest containing index, type and id
 * @param path      Name or path of the field in the Shape Document where the Shape itself is located
 * @return Shape with the given ID
 * @throws IOException Can be thrown while parsing the Shape Document and extracting the Shape
 */
public ShapeBuilder fetch(GetRequest getRequest,String path) throws IOException {
    getRequest.preference("_local");
    getRequest.operationThreaded(false);
    GetResponse response = client.get(getRequest).actionGet();
    if (!response.isExists()) {
        throw new IllegalArgumentException("Shape with ID [" + getRequest.id() + "] in type [" + getRequest.type() + "] not found");
    }

    String[] pathElements = Strings.splitStringToArray(path, '.');
    int currentPathSlot = 0;

    XContentParser parser = null;
    try {
        parser = XContentHelper.createParser(response.getSourceAsBytesRef());
        XContentParser.Token currentToken;
        while ((currentToken = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
            if (currentToken == XContentParser.Token.FIELD_NAME) {
                if (pathElements[currentPathSlot].equals(parser.currentName())) {
                    parser.nextToken();
                    if (++currentPathSlot == pathElements.length) {
                        return ShapeBuilder.parse(parser);
                    }
                } else {
                    parser.nextToken();
                    parser.skipChildren();
                }
            }
        }
        throw new IllegalStateException("Shape with name [" + getRequest.id() + "] found but missing " + path + " field");
    } finally {
        if (parser != null) {
            parser.close();
        }
    }
}
 
Example 28
Source Project: Elasticsearch   Source File: TranslogRecoveryPerformer.java    License: Apache License 2.0 5 votes vote down vote up
private static Engine.DeleteByQuery prepareDeleteByQuery(IndexQueryParserService queryParserService, MapperService mapperService, IndexAliasesService indexAliasesService, IndexCache indexCache, BytesReference source, @Nullable String[] filteringAliases, Engine.Operation.Origin origin, String... types) {
    long startTime = System.nanoTime();
    if (types == null) {
        types = Strings.EMPTY_ARRAY;
    }
    Query query;
    try {
        query = queryParserService.parseQuery(source).query();
    } catch (QueryParsingException ex) {
        // for BWC we try to parse directly the query since pre 1.0.0.Beta2 we didn't require a top level query field
        if (queryParserService.getIndexCreatedVersion().onOrBefore(Version.V_1_0_0_Beta2)) {
            try {
                XContentParser parser = XContentHelper.createParser(source);
                ParsedQuery parse = queryParserService.parse(parser);
                query = parse.query();
            } catch (Throwable t) {
                ex.addSuppressed(t);
                throw ex;
            }
        } else {
            throw ex;
        }
    }
    Query searchFilter = mapperService.searchFilter(types);
    if (searchFilter != null) {
        query = Queries.filtered(query, searchFilter);
    }

    Query aliasFilter = indexAliasesService.aliasFilter(filteringAliases);
    BitSetProducer parentFilter = mapperService.hasNested() ? indexCache.bitsetFilterCache().getBitSetProducer(Queries.newNonNestedFilter()) : null;
    return new Engine.DeleteByQuery(query, source, filteringAliases, aliasFilter, parentFilter, origin, startTime, types);
}
 
Example 29
Source Project: Elasticsearch   Source File: DocumentMapperParser.java    License: Apache License 2.0 5 votes vote down vote up
public DocumentMapper parse(@Nullable String type, CompressedXContent source, String defaultSource) throws MapperParsingException {
    Map<String, Object> mapping = null;
    if (source != null) {
        Map<String, Object> root = XContentHelper.convertToMap(source.compressedReference(), true).v2();
        Tuple<String, Map<String, Object>> t = extractMapping(type, root);
        type = t.v1();
        mapping = t.v2();
    }
    if (mapping == null) {
        mapping = Maps.newHashMap();
    }
    return parse(type, mapping, defaultSource);
}
 
Example 30
Source Project: crate   Source File: DocumentMapperParser.java    License: Apache License 2.0 5 votes vote down vote up
public DocumentMapper parse(@Nullable String type, CompressedXContent source) throws MapperParsingException {
    Map<String, Object> mapping = null;
    if (source != null) {
        Map<String, Object> root = XContentHelper.convertToMap(source.compressedReference(), true, XContentType.JSON).v2();
        Tuple<String, Map<String, Object>> t = extractMapping(type, root);
        type = t.v1();
        mapping = t.v2();
    }
    if (mapping == null) {
        mapping = new HashMap<>();
    }
    return parse(type, mapping);
}