Java Code Examples for org.elasticsearch.common.xcontent.XContentType#SMILE

The following examples show how to use org.elasticsearch.common.xcontent.XContentType#SMILE . 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: ElasticsearchClusterRunner.java    From elasticsearch-cluster-runner with Apache License 2.0 5 votes vote down vote up
private static XContentType xContentType(CharSequence content) {
    int length = content.length() < 20 ? content.length() : 20;
    if (length == 0) {
        return null;
    }
    char first = content.charAt(0);
    if (first == '{') {
        return XContentType.JSON;
    }
    // Should we throw a failure here? Smile idea is to use it in bytes....
    if (length > 2 && first == SmileConstants.HEADER_BYTE_1 && content.charAt(1) == SmileConstants.HEADER_BYTE_2
            && content.charAt(2) == SmileConstants.HEADER_BYTE_3) {
        return XContentType.SMILE;
    }
    if (length > 2 && first == '-' && content.charAt(1) == '-' && content.charAt(2) == '-') {
        return XContentType.YAML;
    }

    // CBOR is not supported

    for (int i = 0; i < length; i++) {
        char c = content.charAt(i);
        if (c == '{') {
            return XContentType.JSON;
        }
        if (!Character.isWhitespace(c)) {
            break;
        }
    }
    return null;
}
 
Example 2
Source File: SmileXContentGenerator.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public XContentType contentType() {
    return XContentType.SMILE;
}
 
Example 3
Source File: SmileXContentParser.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public XContentType contentType() {
    return XContentType.SMILE;
}
 
Example 4
Source File: SmileXContentGenerator.java    From crate with Apache License 2.0 4 votes vote down vote up
@Override
public XContentType contentType() {
    return XContentType.SMILE;
}
 
Example 5
Source File: SmileXContent.java    From crate with Apache License 2.0 4 votes vote down vote up
@Override
public XContentType type() {
    return XContentType.SMILE;
}
 
Example 6
Source File: SmileXContentParser.java    From crate with Apache License 2.0 4 votes vote down vote up
@Override
public XContentType contentType() {
    return XContentType.SMILE;
}
 
Example 7
Source File: ESTestCase.java    From crate with Apache License 2.0 4 votes vote down vote up
@AfterClass
public static void restoreContentType() {
    Requests.CONTENT_TYPE = XContentType.SMILE;
    Requests.INDEX_CONTENT_TYPE = XContentType.JSON;
}
 
Example 8
Source File: RandomObjects.java    From crate with Apache License 2.0 4 votes vote down vote up
/**
 * Returns a tuple containing random stored field values and their corresponding expected values once printed out
 * via {@link org.elasticsearch.common.xcontent.ToXContent#toXContent(XContentBuilder, ToXContent.Params)} and parsed back via
 * {@link org.elasticsearch.common.xcontent.XContentParser#objectText()}.
 * Generates values based on what can get printed out. Stored fields values are retrieved from lucene and converted via
 * {@link org.elasticsearch.index.mapper.MappedFieldType#valueForDisplay(Object)} to either strings, numbers or booleans.
 *
 * @param random Random generator
 * @param xContentType the content type, used to determine what the expected values are for float numbers.
 */
public static Tuple<List<Object>, List<Object>> randomStoredFieldValues(Random random, XContentType xContentType) {
    int numValues = randomIntBetween(random, 1, 5);
    List<Object> originalValues = new ArrayList<>();
    List<Object> expectedParsedValues = new ArrayList<>();
    int dataType = randomIntBetween(random, 0, 8);
    for (int i = 0; i < numValues; i++) {
        switch(dataType) {
            case 0:
                long randomLong = random.nextLong();
                originalValues.add(randomLong);
                expectedParsedValues.add(randomLong);
                break;
            case 1:
                int randomInt = random.nextInt();
                originalValues.add(randomInt);
                expectedParsedValues.add(randomInt);
                break;
            case 2:
                Short randomShort = (short) random.nextInt();
                originalValues.add(randomShort);
                expectedParsedValues.add(randomShort.intValue());
                break;
            case 3:
                Byte randomByte = (byte)random.nextInt();
                originalValues.add(randomByte);
                expectedParsedValues.add(randomByte.intValue());
                break;
            case 4:
                double randomDouble = random.nextDouble();
                originalValues.add(randomDouble);
                expectedParsedValues.add(randomDouble);
                break;
            case 5:
                Float randomFloat = random.nextFloat();
                originalValues.add(randomFloat);
                if (xContentType == XContentType.SMILE) {
                    //with SMILE we get back a double (this will change in Jackson 2.9 where it will return a Float)
                    expectedParsedValues.add(randomFloat.doubleValue());
                } else {
                    //with JSON AND YAML we get back a double, but with float precision.
                    expectedParsedValues.add(Double.parseDouble(randomFloat.toString()));
                }
                break;
            case 6:
                boolean randomBoolean = random.nextBoolean();
                originalValues.add(randomBoolean);
                expectedParsedValues.add(randomBoolean);
                break;
            case 7:
                String randomString = random.nextBoolean() ? RandomStrings.randomAsciiLettersOfLengthBetween(random, 3, 10) :
                        randomUnicodeOfLengthBetween(random, 3, 10);
                originalValues.add(randomString);
                expectedParsedValues.add(randomString);
                break;
            case 8:
                byte[] randomBytes = RandomStrings.randomUnicodeOfLengthBetween(random, 10, 50).getBytes(StandardCharsets.UTF_8);
                BytesArray randomBytesArray = new BytesArray(randomBytes);
                originalValues.add(randomBytesArray);
                if (xContentType == XContentType.JSON || xContentType == XContentType.YAML) {
                    //JSON and YAML write the base64 format
                    expectedParsedValues.add(Base64.getEncoder().encodeToString(randomBytes));
                } else {
                    //SMILE and CBOR write the original bytes as they support binary format
                    expectedParsedValues.add(randomBytesArray);
                }
                break;
            default:
                throw new UnsupportedOperationException();
        }
    }
    return Tuple.tuple(originalValues, expectedParsedValues);
}