Search in sources :

Example 21 with XContentType

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.common.xcontent.XContentType in project elasticsearch by elastic.

the class SuggestionEntryTests method testFromXContent.

@SuppressWarnings("unchecked")
public void testFromXContent() throws IOException {
    for (Class<? extends Entry> entryType : ENTRY_PARSERS.keySet()) {
        Entry<Option> entry = createTestItem(entryType);
        XContentType xContentType = randomFrom(XContentType.values());
        boolean humanReadable = randomBoolean();
        BytesReference originalBytes = toXContent(entry, xContentType, humanReadable);
        Entry<Option> parsed;
        try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) {
            ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser::getTokenLocation);
            parsed = ENTRY_PARSERS.get(entry.getClass()).apply(parser);
            assertEquals(XContentParser.Token.END_OBJECT, parser.currentToken());
            assertNull(parser.nextToken());
        }
        assertEquals(entry.getClass(), parsed.getClass());
        assertEquals(entry.getText(), parsed.getText());
        assertEquals(entry.getLength(), parsed.getLength());
        assertEquals(entry.getOffset(), parsed.getOffset());
        assertEquals(entry.getOptions().size(), parsed.getOptions().size());
        for (int i = 0; i < entry.getOptions().size(); i++) {
            assertEquals(entry.getOptions().get(i).getClass(), parsed.getOptions().get(i).getClass());
        }
        assertToXContentEquivalent(originalBytes, toXContent(parsed, xContentType, humanReadable), xContentType);
    }
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) XContentType(org.elasticsearch.common.xcontent.XContentType) Option(org.elasticsearch.search.suggest.Suggest.Suggestion.Entry.Option) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 22 with XContentType

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.common.xcontent.XContentType in project elasticsearch by elastic.

the class SuggestionTests method testFromXContent.

@SuppressWarnings({ "rawtypes" })
public void testFromXContent() throws IOException {
    ToXContent.Params params = new ToXContent.MapParams(Collections.singletonMap(RestSearchAction.TYPED_KEYS_PARAM, "true"));
    for (Class<Suggestion<? extends Entry<? extends Option>>> type : SUGGESTION_TYPES) {
        Suggestion suggestion = createTestItem(type);
        XContentType xContentType = randomFrom(XContentType.values());
        boolean humanReadable = randomBoolean();
        BytesReference originalBytes = toXContent(suggestion, xContentType, params, humanReadable);
        Suggestion parsed;
        try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) {
            ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser::getTokenLocation);
            ensureExpectedToken(XContentParser.Token.FIELD_NAME, parser.nextToken(), parser::getTokenLocation);
            parsed = Suggestion.fromXContent(parser);
            assertEquals(XContentParser.Token.END_OBJECT, parser.nextToken());
            assertNull(parser.nextToken());
        }
        assertEquals(suggestion.getName(), parsed.getName());
        assertEquals(suggestion.getEntries().size(), parsed.getEntries().size());
        // We don't parse size via xContent, instead we set it to -1 on the client side
        assertEquals(-1, parsed.getSize());
        assertToXContentEquivalent(originalBytes, toXContent(parsed, xContentType, params, humanReadable), xContentType);
    }
}
Also used : ToXContent(org.elasticsearch.common.xcontent.ToXContent) BytesReference(org.elasticsearch.common.bytes.BytesReference) CompletionSuggestion(org.elasticsearch.search.suggest.completion.CompletionSuggestion) Suggestion(org.elasticsearch.search.suggest.Suggest.Suggestion) PhraseSuggestion(org.elasticsearch.search.suggest.phrase.PhraseSuggestion) TermSuggestion(org.elasticsearch.search.suggest.term.TermSuggestion) Entry(org.elasticsearch.search.suggest.Suggest.Suggestion.Entry) XContentType(org.elasticsearch.common.xcontent.XContentType) Option(org.elasticsearch.search.suggest.Suggest.Suggestion.Entry.Option) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 23 with XContentType

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.common.xcontent.XContentType in project elasticsearch by elastic.

the class TermSuggestionOptionTests method testFromXContent.

public void testFromXContent() throws IOException {
    Option option = createTestItem();
    XContentType xContentType = randomFrom(XContentType.values());
    boolean humanReadable = randomBoolean();
    BytesReference originalBytes = toXContent(option, xContentType, humanReadable);
    Option parsed;
    try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) {
        ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser::getTokenLocation);
        parsed = Option.fromXContent(parser);
        assertEquals(XContentParser.Token.END_OBJECT, parser.currentToken());
        assertNull(parser.nextToken());
    }
    assertEquals(option.getText(), parsed.getText());
    assertEquals(option.getScore(), parsed.getScore(), Float.MIN_VALUE);
    assertEquals(option.getFreq(), parsed.getFreq());
    assertToXContentEquivalent(originalBytes, toXContent(parsed, xContentType, humanReadable), xContentType);
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) XContentType(org.elasticsearch.common.xcontent.XContentType) Option(org.elasticsearch.search.suggest.term.TermSuggestion.Entry.Option) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 24 with XContentType

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.common.xcontent.XContentType in project elasticsearch by elastic.

the class RemoteScrollableHitSource method execute.

private <T> void execute(String method, String uri, Map<String, String> params, HttpEntity entity, BiFunction<XContentParser, XContentType, T> parser, Consumer<? super T> listener) {
    // Preserve the thread context so headers survive after the call
    java.util.function.Supplier<ThreadContext.StoredContext> contextSupplier = threadPool.getThreadContext().newRestorableContext(true);
    class RetryHelper extends AbstractRunnable {

        private final Iterator<TimeValue> retries = backoffPolicy.iterator();

        @Override
        protected void doRun() throws Exception {
            client.performRequestAsync(method, uri, params, entity, new ResponseListener() {

                @Override
                public void onSuccess(org.elasticsearch.client.Response response) {
                    // Restore the thread context to get the precious headers
                    try (ThreadContext.StoredContext ctx = contextSupplier.get()) {
                        // eliminates compiler warning
                        assert ctx != null;
                        T parsedResponse;
                        try {
                            HttpEntity responseEntity = response.getEntity();
                            InputStream content = responseEntity.getContent();
                            XContentType xContentType = null;
                            if (responseEntity.getContentType() != null) {
                                final String mimeType = ContentType.parse(responseEntity.getContentType().getValue()).getMimeType();
                                xContentType = XContentType.fromMediaType(mimeType);
                            }
                            if (xContentType == null) {
                                try {
                                    throw new ElasticsearchException("Response didn't include Content-Type: " + bodyMessage(response.getEntity()));
                                } catch (IOException e) {
                                    ElasticsearchException ee = new ElasticsearchException("Error extracting body from response");
                                    ee.addSuppressed(e);
                                    throw ee;
                                }
                            }
                            // EMPTY is safe here because we don't call namedObject
                            try (XContentParser xContentParser = xContentType.xContent().createParser(NamedXContentRegistry.EMPTY, content)) {
                                parsedResponse = parser.apply(xContentParser, xContentType);
                            } catch (ParsingException e) {
                                /* Because we're streaming the response we can't get a copy of it here. The best we can do is hint that it
                                 * is totally wrong and we're probably not talking to Elasticsearch. */
                                throw new ElasticsearchException("Error parsing the response, remote is likely not an Elasticsearch instance", e);
                            }
                        } catch (IOException e) {
                            throw new ElasticsearchException("Error deserializing response, remote is likely not an Elasticsearch instance", e);
                        }
                        listener.accept(parsedResponse);
                    }
                }

                @Override
                public void onFailure(Exception e) {
                    try (ThreadContext.StoredContext ctx = contextSupplier.get()) {
                        // eliminates compiler warning
                        assert ctx != null;
                        if (e instanceof ResponseException) {
                            ResponseException re = (ResponseException) e;
                            if (RestStatus.TOO_MANY_REQUESTS.getStatus() == re.getResponse().getStatusLine().getStatusCode()) {
                                if (retries.hasNext()) {
                                    TimeValue delay = retries.next();
                                    logger.trace((Supplier<?>) () -> new ParameterizedMessage("retrying rejected search after [{}]", delay), e);
                                    countSearchRetry.run();
                                    threadPool.schedule(delay, ThreadPool.Names.SAME, RetryHelper.this);
                                    return;
                                }
                            }
                            e = wrapExceptionToPreserveStatus(re.getResponse().getStatusLine().getStatusCode(), re.getResponse().getEntity(), re);
                        } else if (e instanceof ContentTooLongException) {
                            e = new IllegalArgumentException("Remote responded with a chunk that was too large. Use a smaller batch size.", e);
                        }
                        fail.accept(e);
                    }
                }
            });
        }

        @Override
        public void onFailure(Exception t) {
            fail.accept(t);
        }
    }
    new RetryHelper().run();
}
Also used : AbstractRunnable(org.elasticsearch.common.util.concurrent.AbstractRunnable) HttpEntity(org.apache.http.HttpEntity) ResponseException(org.elasticsearch.client.ResponseException) ElasticsearchException(org.elasticsearch.ElasticsearchException) XContentType(org.elasticsearch.common.xcontent.XContentType) ParsingException(org.elasticsearch.common.ParsingException) Iterator(java.util.Iterator) Supplier(org.apache.logging.log4j.util.Supplier) TimeValue(org.elasticsearch.common.unit.TimeValue) InputStream(java.io.InputStream) ContentTooLongException(org.apache.http.ContentTooLongException) ResponseListener(org.elasticsearch.client.ResponseListener) IOException(java.io.IOException) ElasticsearchException(org.elasticsearch.ElasticsearchException) ResponseException(org.elasticsearch.client.ResponseException) ContentTooLongException(org.apache.http.ContentTooLongException) ParsingException(org.elasticsearch.common.ParsingException) ElasticsearchStatusException(org.elasticsearch.ElasticsearchStatusException) IOException(java.io.IOException) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 25 with XContentType

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.common.xcontent.XContentType in project elasticsearch by elastic.

the class GetFieldTests method testToAndFromXContent.

public void testToAndFromXContent() throws Exception {
    XContentType xContentType = randomFrom(XContentType.values());
    Tuple<GetField, GetField> tuple = randomGetField(xContentType);
    GetField getField = tuple.v1();
    GetField expectedGetField = tuple.v2();
    boolean humanReadable = randomBoolean();
    BytesReference originalBytes = toXContent(getField, xContentType, humanReadable);
    //test that we can parse what we print out
    GetField parsedGetField;
    try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) {
        //we need to move to the next token, the start object one that we manually added is not expected
        assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken());
        assertEquals(XContentParser.Token.FIELD_NAME, parser.nextToken());
        parsedGetField = GetField.fromXContent(parser);
        assertEquals(XContentParser.Token.END_ARRAY, parser.currentToken());
        assertEquals(XContentParser.Token.END_OBJECT, parser.nextToken());
        assertNull(parser.nextToken());
    }
    assertEquals(expectedGetField, parsedGetField);
    BytesReference finalBytes = toXContent(parsedGetField, xContentType, humanReadable);
    assertToXContentEquivalent(originalBytes, finalBytes, xContentType);
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) XContentType(org.elasticsearch.common.xcontent.XContentType) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Aggregations

XContentType (org.elasticsearch.common.xcontent.XContentType)86 BytesReference (org.elasticsearch.common.bytes.BytesReference)50 XContentParser (org.elasticsearch.common.xcontent.XContentParser)45 Map (java.util.Map)20 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)20 HashMap (java.util.HashMap)14 IndexRequest (org.elasticsearch.action.index.IndexRequest)11 DocWriteRequest (org.elasticsearch.action.DocWriteRequest)10 IOException (java.io.IOException)9 DeleteRequest (org.elasticsearch.action.delete.DeleteRequest)8 ArrayList (java.util.ArrayList)6 Collections.emptyMap (java.util.Collections.emptyMap)6 List (java.util.List)6 ElasticsearchException (org.elasticsearch.ElasticsearchException)6 BulkRequest (org.elasticsearch.action.bulk.BulkRequest)6 UpdateRequest (org.elasticsearch.action.update.UpdateRequest)6 Collections.singletonMap (java.util.Collections.singletonMap)5 HttpEntity (org.apache.http.HttpEntity)5 ByteArrayEntity (org.apache.http.entity.ByteArrayEntity)5 GetRequest (org.elasticsearch.action.get.GetRequest)5