Search in sources :

Example 36 with BytesReference

use of org.elasticsearch.common.bytes.BytesReference in project elasticsearch by elastic.

the class SuggestionOptionTests method testToXContent.

public void testToXContent() throws IOException {
    Option option = new Option(new Text("someText"), new Text("somethingHighlighted"), 1.3f, true);
    BytesReference xContent = toXContent(option, XContentType.JSON, randomBoolean());
    assertEquals("{\"text\":\"someText\"," + "\"highlighted\":\"somethingHighlighted\"," + "\"score\":1.3," + "\"collate_match\":true" + "}", xContent.utf8ToString());
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) Option(org.elasticsearch.search.suggest.Suggest.Suggestion.Entry.Option) Text(org.elasticsearch.common.text.Text)

Example 37 with BytesReference

use of org.elasticsearch.common.bytes.BytesReference 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 38 with BytesReference

use of org.elasticsearch.common.bytes.BytesReference in project elasticsearch by elastic.

the class CompletionSuggestionOptionTests method testToXContent.

public void testToXContent() throws IOException {
    Map<String, Set<CharSequence>> contexts = Collections.singletonMap("key", Collections.singleton("value"));
    CompletionSuggestion.Entry.Option option = new CompletionSuggestion.Entry.Option(1, new Text("someText"), 1.3f, contexts);
    BytesReference xContent = toXContent(option, XContentType.JSON, randomBoolean());
    assertEquals("{\"text\":\"someText\",\"score\":1.3,\"contexts\":{\"key\":[\"value\"]}}", xContent.utf8ToString());
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) CompletionSuggestion(org.elasticsearch.search.suggest.completion.CompletionSuggestion) Set(java.util.Set) HashSet(java.util.HashSet) Option(org.elasticsearch.search.suggest.completion.CompletionSuggestion.Entry.Option) Option(org.elasticsearch.search.suggest.completion.CompletionSuggestion.Entry.Option) Text(org.elasticsearch.common.text.Text)

Example 39 with BytesReference

use of org.elasticsearch.common.bytes.BytesReference 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 40 with BytesReference

use of org.elasticsearch.common.bytes.BytesReference in project elasticsearch by elastic.

the class SharedClusterSnapshotRestoreIT method testIncludeGlobalState.

public void testIncludeGlobalState() throws Exception {
    Client client = client();
    logger.info("-->  creating repository");
    Path location = randomRepoPath();
    assertAcked(client.admin().cluster().preparePutRepository("test-repo").setType("fs").setSettings(Settings.builder().put("location", location)));
    boolean testTemplate = randomBoolean();
    boolean testPipeline = randomBoolean();
    // At least something should be stored
    boolean testScript = (testTemplate == false && testPipeline == false) || randomBoolean();
    if (testTemplate) {
        logger.info("-->  creating test template");
        assertThat(client.admin().indices().preparePutTemplate("test-template").setPatterns(Collections.singletonList("te*")).addMapping("test-mapping", XContentFactory.jsonBuilder().startObject().startObject("test-mapping").startObject("properties").startObject("field1").field("type", "text").field("store", true).endObject().startObject("field2").field("type", "keyword").field("store", true).endObject().endObject().endObject().endObject()).get().isAcknowledged(), equalTo(true));
    }
    if (testPipeline) {
        logger.info("-->  creating test pipeline");
        BytesReference pipelineSource = jsonBuilder().startObject().field("description", "my_pipeline").startArray("processors").startObject().startObject("test").endObject().endObject().endArray().endObject().bytes();
        assertAcked(client().admin().cluster().preparePutPipeline("barbaz", pipelineSource, XContentType.JSON).get());
    }
    if (testScript) {
        logger.info("-->  creating test script");
        assertAcked(client().admin().cluster().preparePutStoredScript().setLang(MockScriptEngine.NAME).setId("foobar").setContent(new BytesArray("{\"script\":\"1\"}"), XContentType.JSON));
    }
    logger.info("--> snapshot without global state");
    CreateSnapshotResponse createSnapshotResponse = client.admin().cluster().prepareCreateSnapshot("test-repo", "test-snap-no-global-state").setIndices().setIncludeGlobalState(false).setWaitForCompletion(true).get();
    assertThat(createSnapshotResponse.getSnapshotInfo().totalShards(), equalTo(0));
    assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), equalTo(0));
    assertThat(client.admin().cluster().prepareGetSnapshots("test-repo").setSnapshots("test-snap-no-global-state").get().getSnapshots().get(0).state(), equalTo(SnapshotState.SUCCESS));
    logger.info("--> snapshot with global state");
    createSnapshotResponse = client.admin().cluster().prepareCreateSnapshot("test-repo", "test-snap-with-global-state").setIndices().setIncludeGlobalState(true).setWaitForCompletion(true).get();
    assertThat(createSnapshotResponse.getSnapshotInfo().totalShards(), equalTo(0));
    assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), equalTo(0));
    assertThat(client.admin().cluster().prepareGetSnapshots("test-repo").setSnapshots("test-snap-with-global-state").get().getSnapshots().get(0).state(), equalTo(SnapshotState.SUCCESS));
    if (testTemplate) {
        logger.info("-->  delete test template");
        cluster().wipeTemplates("test-template");
        GetIndexTemplatesResponse getIndexTemplatesResponse = client().admin().indices().prepareGetTemplates().get();
        assertIndexTemplateMissing(getIndexTemplatesResponse, "test-template");
    }
    if (testPipeline) {
        logger.info("-->  delete test pipeline");
        assertAcked(client().admin().cluster().deletePipeline(new DeletePipelineRequest("barbaz")).get());
    }
    if (testScript) {
        logger.info("-->  delete test script");
        assertAcked(client().admin().cluster().prepareDeleteStoredScript(MockScriptEngine.NAME, "foobar").get());
    }
    logger.info("--> try restoring cluster state from snapshot without global state");
    RestoreSnapshotResponse restoreSnapshotResponse = client.admin().cluster().prepareRestoreSnapshot("test-repo", "test-snap-no-global-state").setWaitForCompletion(true).setRestoreGlobalState(true).execute().actionGet();
    assertThat(restoreSnapshotResponse.getRestoreInfo().totalShards(), equalTo(0));
    logger.info("--> check that template wasn't restored");
    GetIndexTemplatesResponse getIndexTemplatesResponse = client().admin().indices().prepareGetTemplates().get();
    assertIndexTemplateMissing(getIndexTemplatesResponse, "test-template");
    logger.info("--> restore cluster state");
    restoreSnapshotResponse = client.admin().cluster().prepareRestoreSnapshot("test-repo", "test-snap-with-global-state").setWaitForCompletion(true).setRestoreGlobalState(true).execute().actionGet();
    assertThat(restoreSnapshotResponse.getRestoreInfo().totalShards(), equalTo(0));
    if (testTemplate) {
        logger.info("--> check that template is restored");
        getIndexTemplatesResponse = client().admin().indices().prepareGetTemplates().get();
        assertIndexTemplateExists(getIndexTemplatesResponse, "test-template");
    }
    if (testPipeline) {
        logger.info("--> check that pipeline is restored");
        GetPipelineResponse getPipelineResponse = client().admin().cluster().prepareGetPipeline("barbaz").get();
        assertTrue(getPipelineResponse.isFound());
    }
    if (testScript) {
        logger.info("--> check that script is restored");
        GetStoredScriptResponse getStoredScriptResponse = client().admin().cluster().prepareGetStoredScript(MockScriptEngine.NAME, "foobar").get();
        assertNotNull(getStoredScriptResponse.getSource());
    }
    createIndex("test-idx");
    ensureGreen();
    logger.info("--> indexing some data");
    for (int i = 0; i < 100; i++) {
        index("test-idx", "doc", Integer.toString(i), "foo", "bar" + i);
    }
    refresh();
    assertThat(client.prepareSearch("test-idx").setSize(0).get().getHits().getTotalHits(), equalTo(100L));
    logger.info("--> snapshot without global state but with indices");
    createSnapshotResponse = client.admin().cluster().prepareCreateSnapshot("test-repo", "test-snap-no-global-state-with-index").setIndices("test-idx").setIncludeGlobalState(false).setWaitForCompletion(true).get();
    assertThat(createSnapshotResponse.getSnapshotInfo().totalShards(), greaterThan(0));
    assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), equalTo(createSnapshotResponse.getSnapshotInfo().totalShards()));
    assertThat(client.admin().cluster().prepareGetSnapshots("test-repo").setSnapshots("test-snap-no-global-state-with-index").get().getSnapshots().get(0).state(), equalTo(SnapshotState.SUCCESS));
    logger.info("-->  delete global state and index ");
    cluster().wipeIndices("test-idx");
    if (testTemplate) {
        cluster().wipeTemplates("test-template");
    }
    if (testPipeline) {
        assertAcked(client().admin().cluster().deletePipeline(new DeletePipelineRequest("barbaz")).get());
    }
    if (testScript) {
        assertAcked(client().admin().cluster().prepareDeleteStoredScript(MockScriptEngine.NAME, "foobar").get());
    }
    getIndexTemplatesResponse = client().admin().indices().prepareGetTemplates().get();
    assertIndexTemplateMissing(getIndexTemplatesResponse, "test-template");
    logger.info("--> try restoring index and cluster state from snapshot without global state");
    restoreSnapshotResponse = client.admin().cluster().prepareRestoreSnapshot("test-repo", "test-snap-no-global-state-with-index").setWaitForCompletion(true).setRestoreGlobalState(true).execute().actionGet();
    assertThat(restoreSnapshotResponse.getRestoreInfo().totalShards(), greaterThan(0));
    assertThat(restoreSnapshotResponse.getRestoreInfo().failedShards(), equalTo(0));
    logger.info("--> check that global state wasn't restored but index was");
    getIndexTemplatesResponse = client().admin().indices().prepareGetTemplates().get();
    assertIndexTemplateMissing(getIndexTemplatesResponse, "test-template");
    assertFalse(client().admin().cluster().prepareGetPipeline("barbaz").get().isFound());
    assertNull(client().admin().cluster().prepareGetStoredScript(MockScriptEngine.NAME, "foobar").get().getSource());
    assertThat(client.prepareSearch("test-idx").setSize(0).get().getHits().getTotalHits(), equalTo(100L));
}
Also used : Path(java.nio.file.Path) BytesReference(org.elasticsearch.common.bytes.BytesReference) DeletePipelineRequest(org.elasticsearch.action.ingest.DeletePipelineRequest) BytesArray(org.elasticsearch.common.bytes.BytesArray) CreateSnapshotResponse(org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse) GetIndexTemplatesResponse(org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse) Client(org.elasticsearch.client.Client) GetPipelineResponse(org.elasticsearch.action.ingest.GetPipelineResponse) RestoreSnapshotResponse(org.elasticsearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse) GetStoredScriptResponse(org.elasticsearch.action.admin.cluster.storedscripts.GetStoredScriptResponse)

Aggregations

BytesReference (org.elasticsearch.common.bytes.BytesReference)318 Matchers.containsString (org.hamcrest.Matchers.containsString)72 XContentParser (org.elasticsearch.common.xcontent.XContentParser)63 CompressedXContent (org.elasticsearch.common.compress.CompressedXContent)61 IOException (java.io.IOException)58 XContentType (org.elasticsearch.common.xcontent.XContentType)50 BytesArray (org.elasticsearch.common.bytes.BytesArray)47 BytesStreamOutput (org.elasticsearch.common.io.stream.BytesStreamOutput)37 ArrayList (java.util.ArrayList)30 HashMap (java.util.HashMap)30 Map (java.util.Map)26 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)26 Test (org.junit.Test)25 List (java.util.List)24 ParsedDocument (org.elasticsearch.index.mapper.ParsedDocument)24 Version (org.elasticsearch.Version)22 DocumentMapper (org.elasticsearch.index.mapper.DocumentMapper)20 ReleasableBytesReference (org.elasticsearch.common.bytes.ReleasableBytesReference)19 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)18 BytesRef (org.apache.lucene.util.BytesRef)18