Search in sources :

Example 11 with GetResponse

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.get.GetResponse in project elasticsearch by elastic.

the class GetActionIT method testSimpleGet.

public void testSimpleGet() {
    assertAcked(prepareCreate("test").addMapping("type1", "field1", "type=keyword,store=true", "field2", "type=keyword,store=true").setSettings(Settings.builder().put("index.refresh_interval", -1)).addAlias(new Alias("alias")));
    ensureGreen();
    GetResponse response = client().prepareGet(indexOrAlias(), "type1", "1").get();
    assertThat(response.isExists(), equalTo(false));
    logger.info("--> index doc 1");
    client().prepareIndex("test", "type1", "1").setSource("field1", "value1", "field2", "value2").get();
    logger.info("--> non realtime get 1");
    response = client().prepareGet(indexOrAlias(), "type1", "1").setRealtime(false).get();
    assertThat(response.isExists(), equalTo(false));
    logger.info("--> realtime get 1");
    response = client().prepareGet(indexOrAlias(), "type1", "1").get();
    assertThat(response.isExists(), equalTo(true));
    assertThat(response.getIndex(), equalTo("test"));
    assertThat(response.getSourceAsMap().get("field1").toString(), equalTo("value1"));
    assertThat(response.getSourceAsMap().get("field2").toString(), equalTo("value2"));
    logger.info("--> realtime get 1 (no source, implicit)");
    response = client().prepareGet(indexOrAlias(), "type1", "1").setStoredFields(Strings.EMPTY_ARRAY).get();
    assertThat(response.isExists(), equalTo(true));
    assertThat(response.getIndex(), equalTo("test"));
    Set<String> fields = new HashSet<>(response.getFields().keySet());
    assertThat(fields, equalTo(Collections.<String>emptySet()));
    assertThat(response.getSourceAsBytes(), nullValue());
    logger.info("--> realtime get 1 (no source, explicit)");
    response = client().prepareGet(indexOrAlias(), "type1", "1").setFetchSource(false).get();
    assertThat(response.isExists(), equalTo(true));
    assertThat(response.getIndex(), equalTo("test"));
    fields = new HashSet<>(response.getFields().keySet());
    assertThat(fields, equalTo(Collections.<String>emptySet()));
    assertThat(response.getSourceAsBytes(), nullValue());
    logger.info("--> realtime get 1 (no type)");
    response = client().prepareGet(indexOrAlias(), null, "1").get();
    assertThat(response.isExists(), equalTo(true));
    assertThat(response.getIndex(), equalTo("test"));
    assertThat(response.getSourceAsMap().get("field1").toString(), equalTo("value1"));
    assertThat(response.getSourceAsMap().get("field2").toString(), equalTo("value2"));
    logger.info("--> realtime fetch of field");
    response = client().prepareGet(indexOrAlias(), "type1", "1").setStoredFields("field1").get();
    assertThat(response.isExists(), equalTo(true));
    assertThat(response.getIndex(), equalTo("test"));
    assertThat(response.getSourceAsBytes(), nullValue());
    assertThat(response.getField("field1").getValues().get(0).toString(), equalTo("value1"));
    assertThat(response.getField("field2"), nullValue());
    logger.info("--> realtime fetch of field & source");
    response = client().prepareGet(indexOrAlias(), "type1", "1").setStoredFields("field1").setFetchSource("field1", null).get();
    assertThat(response.isExists(), equalTo(true));
    assertThat(response.getIndex(), equalTo("test"));
    assertThat(response.getSourceAsMap(), hasKey("field1"));
    assertThat(response.getSourceAsMap(), not(hasKey("field2")));
    assertThat(response.getField("field1").getValues().get(0).toString(), equalTo("value1"));
    assertThat(response.getField("field2"), nullValue());
    logger.info("--> flush the index, so we load it from it");
    flush();
    logger.info("--> realtime get 1 (loaded from index)");
    response = client().prepareGet(indexOrAlias(), "type1", "1").get();
    assertThat(response.isExists(), equalTo(true));
    assertThat(response.getIndex(), equalTo("test"));
    assertThat(response.getSourceAsMap().get("field1").toString(), equalTo("value1"));
    assertThat(response.getSourceAsMap().get("field2").toString(), equalTo("value2"));
    logger.info("--> non realtime get 1 (loaded from index)");
    response = client().prepareGet(indexOrAlias(), "type1", "1").setRealtime(false).get();
    assertThat(response.isExists(), equalTo(true));
    assertThat(response.getIndex(), equalTo("test"));
    assertThat(response.getSourceAsMap().get("field1").toString(), equalTo("value1"));
    assertThat(response.getSourceAsMap().get("field2").toString(), equalTo("value2"));
    logger.info("--> realtime fetch of field (loaded from index)");
    response = client().prepareGet(indexOrAlias(), "type1", "1").setStoredFields("field1").get();
    assertThat(response.isExists(), equalTo(true));
    assertThat(response.getIndex(), equalTo("test"));
    assertThat(response.getSourceAsBytes(), nullValue());
    assertThat(response.getField("field1").getValues().get(0).toString(), equalTo("value1"));
    assertThat(response.getField("field2"), nullValue());
    logger.info("--> realtime fetch of field & source (loaded from index)");
    response = client().prepareGet(indexOrAlias(), "type1", "1").setStoredFields("field1").setFetchSource(true).get();
    assertThat(response.isExists(), equalTo(true));
    assertThat(response.getIndex(), equalTo("test"));
    assertThat(response.getSourceAsBytes(), not(nullValue()));
    assertThat(response.getField("field1").getValues().get(0).toString(), equalTo("value1"));
    assertThat(response.getField("field2"), nullValue());
    logger.info("--> update doc 1");
    client().prepareIndex("test", "type1", "1").setSource("field1", "value1_1", "field2", "value2_1").get();
    logger.info("--> realtime get 1");
    response = client().prepareGet(indexOrAlias(), "type1", "1").get();
    assertThat(response.isExists(), equalTo(true));
    assertThat(response.getIndex(), equalTo("test"));
    assertThat(response.getSourceAsMap().get("field1").toString(), equalTo("value1_1"));
    assertThat(response.getSourceAsMap().get("field2").toString(), equalTo("value2_1"));
    logger.info("--> update doc 1 again");
    client().prepareIndex("test", "type1", "1").setSource("field1", "value1_2", "field2", "value2_2").get();
    response = client().prepareGet(indexOrAlias(), "type1", "1").get();
    assertThat(response.isExists(), equalTo(true));
    assertThat(response.getIndex(), equalTo("test"));
    assertThat(response.getSourceAsMap().get("field1").toString(), equalTo("value1_2"));
    assertThat(response.getSourceAsMap().get("field2").toString(), equalTo("value2_2"));
    DeleteResponse deleteResponse = client().prepareDelete("test", "type1", "1").get();
    assertEquals(DocWriteResponse.Result.DELETED, deleteResponse.getResult());
    response = client().prepareGet(indexOrAlias(), "type1", "1").get();
    assertThat(response.isExists(), equalTo(false));
}
Also used : DeleteResponse(org.elasticsearch.action.delete.DeleteResponse) Alias(org.elasticsearch.action.admin.indices.alias.Alias) GetResponse(org.elasticsearch.action.get.GetResponse) MultiGetResponse(org.elasticsearch.action.get.MultiGetResponse) HashSet(java.util.HashSet)

Example 12 with GetResponse

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.get.GetResponse in project elasticsearch by elastic.

the class GetActionIT method assertGetFieldNull.

protected void assertGetFieldNull(String index, String type, String docId, String field, @Nullable String routing) {
    //for get
    GetResponse response = getDocument(index, type, docId, field, routing);
    assertTrue(response.isExists());
    assertNull(response.getField(field));
    assertThat(response.getId(), equalTo(docId));
    //same for multi get
    response = multiGetDocument(index, type, docId, field, routing);
    assertNull(response.getField(field));
    assertThat(response.getId(), equalTo(docId));
    assertTrue(response.isExists());
}
Also used : GetResponse(org.elasticsearch.action.get.GetResponse) MultiGetResponse(org.elasticsearch.action.get.MultiGetResponse)

Example 13 with GetResponse

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.get.GetResponse in project elasticsearch by elastic.

the class GetActionIT method assertGetFieldWorks.

private void assertGetFieldWorks(String index, String type, String docId, String field, @Nullable String routing) {
    GetResponse response = getDocument(index, type, docId, field, routing);
    assertThat(response.getId(), equalTo(docId));
    assertTrue(response.isExists());
    assertNotNull(response.getField(field));
    response = multiGetDocument(index, type, docId, field, routing);
    assertThat(response.getId(), equalTo(docId));
    assertTrue(response.isExists());
    assertNotNull(response.getField(field));
}
Also used : GetResponse(org.elasticsearch.action.get.GetResponse) MultiGetResponse(org.elasticsearch.action.get.MultiGetResponse)

Example 14 with GetResponse

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.get.GetResponse in project elasticsearch by elastic.

the class GetActionIT method testGetWithVersion.

public void testGetWithVersion() {
    assertAcked(prepareCreate("test").addAlias(new Alias("alias")).setSettings(Settings.builder().put("index.refresh_interval", -1)));
    ensureGreen();
    GetResponse response = client().prepareGet("test", "type1", "1").get();
    assertThat(response.isExists(), equalTo(false));
    logger.info("--> index doc 1");
    client().prepareIndex("test", "type1", "1").setSource("field1", "value1", "field2", "value2").get();
    // From translog:
    response = client().prepareGet(indexOrAlias(), "type1", "1").setVersion(Versions.MATCH_ANY).get();
    assertThat(response.isExists(), equalTo(true));
    assertThat(response.getId(), equalTo("1"));
    assertThat(response.getVersion(), equalTo(1L));
    response = client().prepareGet(indexOrAlias(), "type1", "1").setVersion(1).get();
    assertThat(response.isExists(), equalTo(true));
    assertThat(response.getId(), equalTo("1"));
    assertThat(response.getVersion(), equalTo(1L));
    try {
        client().prepareGet(indexOrAlias(), "type1", "1").setVersion(2).get();
        fail();
    } catch (VersionConflictEngineException e) {
    //all good
    }
    // From Lucene index:
    refresh();
    response = client().prepareGet(indexOrAlias(), "type1", "1").setVersion(Versions.MATCH_ANY).setRealtime(false).get();
    assertThat(response.isExists(), equalTo(true));
    assertThat(response.getId(), equalTo("1"));
    assertThat(response.getIndex(), equalTo("test"));
    assertThat(response.getVersion(), equalTo(1L));
    response = client().prepareGet(indexOrAlias(), "type1", "1").setVersion(1).setRealtime(false).get();
    assertThat(response.isExists(), equalTo(true));
    assertThat(response.getId(), equalTo("1"));
    assertThat(response.getIndex(), equalTo("test"));
    assertThat(response.getVersion(), equalTo(1L));
    try {
        client().prepareGet(indexOrAlias(), "type1", "1").setVersion(2).setRealtime(false).get();
        fail();
    } catch (VersionConflictEngineException e) {
    //all good
    }
    logger.info("--> index doc 1 again, so increasing the version");
    client().prepareIndex("test", "type1", "1").setSource("field1", "value1", "field2", "value2").get();
    // From translog:
    response = client().prepareGet(indexOrAlias(), "type1", "1").setVersion(Versions.MATCH_ANY).get();
    assertThat(response.isExists(), equalTo(true));
    assertThat(response.getId(), equalTo("1"));
    assertThat(response.getIndex(), equalTo("test"));
    assertThat(response.getVersion(), equalTo(2L));
    try {
        client().prepareGet(indexOrAlias(), "type1", "1").setVersion(1).get();
        fail();
    } catch (VersionConflictEngineException e) {
    //all good
    }
    response = client().prepareGet(indexOrAlias(), "type1", "1").setVersion(2).get();
    assertThat(response.isExists(), equalTo(true));
    assertThat(response.getId(), equalTo("1"));
    assertThat(response.getIndex(), equalTo("test"));
    assertThat(response.getVersion(), equalTo(2L));
    // From Lucene index:
    refresh();
    response = client().prepareGet(indexOrAlias(), "type1", "1").setVersion(Versions.MATCH_ANY).setRealtime(false).get();
    assertThat(response.isExists(), equalTo(true));
    assertThat(response.getId(), equalTo("1"));
    assertThat(response.getIndex(), equalTo("test"));
    assertThat(response.getVersion(), equalTo(2L));
    try {
        client().prepareGet(indexOrAlias(), "type1", "1").setVersion(1).setRealtime(false).get();
        fail();
    } catch (VersionConflictEngineException e) {
    //all good
    }
    response = client().prepareGet(indexOrAlias(), "type1", "1").setVersion(2).setRealtime(false).get();
    assertThat(response.isExists(), equalTo(true));
    assertThat(response.getId(), equalTo("1"));
    assertThat(response.getIndex(), equalTo("test"));
    assertThat(response.getVersion(), equalTo(2L));
}
Also used : VersionConflictEngineException(org.elasticsearch.index.engine.VersionConflictEngineException) Alias(org.elasticsearch.action.admin.indices.alias.Alias) GetResponse(org.elasticsearch.action.get.GetResponse) MultiGetResponse(org.elasticsearch.action.get.MultiGetResponse)

Example 15 with GetResponse

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.get.GetResponse in project elasticsearch by elastic.

the class IndexWithShadowReplicasIT method testReplicaToPrimaryPromotion.

public void testReplicaToPrimaryPromotion() throws Exception {
    Path dataPath = createTempDir();
    Settings nodeSettings = nodeSettings(dataPath);
    String node1 = internalCluster().startNode(nodeSettings);
    String IDX = "test";
    Settings idxSettings = Settings.builder().put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 1).put(IndexMetaData.SETTING_DATA_PATH, dataPath.toAbsolutePath().toString()).put(IndexMetaData.SETTING_SHADOW_REPLICAS, true).put(IndexMetaData.SETTING_SHARED_FILESYSTEM, true).build();
    prepareCreate(IDX).setSettings(idxSettings).addMapping("doc", "foo", "type=text").get();
    client().prepareIndex(IDX, "doc", "1").setSource("foo", "bar").get();
    client().prepareIndex(IDX, "doc", "2").setSource("foo", "bar").get();
    GetResponse gResp1 = client().prepareGet(IDX, "doc", "1").get();
    GetResponse gResp2 = client().prepareGet(IDX, "doc", "2").get();
    assertTrue(gResp1.isExists());
    assertTrue(gResp2.isExists());
    assertThat(gResp1.getSource().get("foo"), equalTo("bar"));
    assertThat(gResp2.getSource().get("foo"), equalTo("bar"));
    // Node1 has the primary, now node2 has the replica
    internalCluster().startNode(nodeSettings);
    ensureGreen(IDX);
    client().admin().cluster().prepareHealth().setWaitForNodes("2").get();
    flushAndRefresh(IDX);
    logger.info("--> stopping node1 [{}]", node1);
    internalCluster().stopRandomNode(InternalTestCluster.nameFilter(node1));
    // wait for the new node to be elected and process the node leave
    ensureClusterSizeConsistency();
    ensureYellow(IDX);
    logger.info("--> performing query");
    SearchResponse resp = client().prepareSearch(IDX).setQuery(matchAllQuery()).get();
    assertHitCount(resp, 2);
    gResp1 = client().prepareGet(IDX, "doc", "1").get();
    gResp2 = client().prepareGet(IDX, "doc", "2").get();
    assertTrue(gResp1.isExists());
    assertTrue(gResp2.toString(), gResp2.isExists());
    assertThat(gResp1.getSource().get("foo"), equalTo("bar"));
    assertThat(gResp2.getSource().get("foo"), equalTo("bar"));
    client().prepareIndex(IDX, "doc", "1").setSource("foo", "foobar").get();
    client().prepareIndex(IDX, "doc", "2").setSource("foo", "foobar").get();
    gResp1 = client().prepareGet(IDX, "doc", "1").get();
    gResp2 = client().prepareGet(IDX, "doc", "2").get();
    assertTrue(gResp1.isExists());
    assertTrue(gResp2.toString(), gResp2.isExists());
    assertThat(gResp1.getSource().get("foo"), equalTo("foobar"));
    assertThat(gResp2.getSource().get("foo"), equalTo("foobar"));
}
Also used : Path(java.nio.file.Path) GetResponse(org.elasticsearch.action.get.GetResponse) Settings(org.elasticsearch.common.settings.Settings) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Aggregations

GetResponse (org.elasticsearch.action.get.GetResponse)149 Test (org.junit.Test)41 Map (java.util.Map)27 MultiGetResponse (org.elasticsearch.action.get.MultiGetResponse)25 ArrayList (java.util.ArrayList)22 HashMap (java.util.HashMap)18 ESSyncConfig (com.alibaba.otter.canal.client.adapter.es.core.config.ESSyncConfig)17 Dml (com.alibaba.otter.canal.client.adapter.support.Dml)17 Date (java.util.Date)17 LinkedHashMap (java.util.LinkedHashMap)17 DataSource (javax.sql.DataSource)14 GetRequestBuilder (org.elasticsearch.action.get.GetRequestBuilder)14 SearchResponse (org.elasticsearch.action.search.SearchResponse)13 DeleteResponse (org.elasticsearch.action.delete.DeleteResponse)12 IOException (java.io.IOException)11 GetRequest (org.elasticsearch.action.get.GetRequest)11 ElasticsearchException (org.elasticsearch.ElasticsearchException)9 Settings (org.elasticsearch.common.settings.Settings)9 Alias (org.elasticsearch.action.admin.indices.alias.Alias)7 UpdateResponse (org.elasticsearch.action.update.UpdateResponse)7