public void testUpdate() {
    createIndexWithAlias();
    ensureYellow("test");

    UpdateRequestBuilder updateRequestBuilder =
        client()
            .prepareUpdate(indexOrAlias(), "type1", "1")
            .setUpsert("field1", "value1")
            .setDoc("field2", "value2");

    UpdateResponse updateResponse = updateRequestBuilder.get();
    assertThat(updateResponse.getIndex(), equalTo("test"));
    assertThat(updateResponse.getType(), equalTo("type1"));
    assertThat(updateResponse.getId(), equalTo("1"));
    assertThat(updateResponse.isCreated(), equalTo(true));

    GetResponse getResponse = client().prepareGet("test", "type1", "1").get();
    assertThat(getResponse.isExists(), equalTo(true));
    assertThat(getResponse.getSourceAsMap().containsKey("field1"), equalTo(true));
    assertThat(getResponse.getSourceAsMap().containsKey("field2"), equalTo(false));

    updateResponse = updateRequestBuilder.get();
    assertThat(updateResponse.getIndex(), equalTo("test"));
    assertThat(updateResponse.getType(), equalTo("type1"));
    assertThat(updateResponse.getId(), equalTo("1"));
    assertThat(updateResponse.isCreated(), equalTo(false));

    getResponse = client().prepareGet("test", "type1", "1").get();
    assertThat(getResponse.isExists(), equalTo(true));
    assertThat(getResponse.getSourceAsMap().containsKey("field1"), equalTo(true));
    assertThat(getResponse.getSourceAsMap().containsKey("field2"), equalTo(true));
  }
Ejemplo n.º 2
0
  public static String save(
      ElasticSearchContextListener es, String userId, String memeId, String value) {
    try {
      String pendingItemId = getPendingItemId(es, userId, memeId);
      Map<String, Object> data = getRatingsObjectMap(userId, memeId, value);

      if (pendingItemId != null) {
        UpdateResponse response = es.updateRequest(INDEX_NAME, pendingItemId, data).actionGet();
        return response.getId();
      } else {
        IndexResponse response = es.indexRequest(INDEX_NAME, data).actionGet();
        return response.getId();
      }
    } catch (Exception e) {
      return null;
    }
  }
Ejemplo n.º 3
0
  @Test
  public void testRequiredRoutingMapping_variousAPIs() throws Exception {
    client()
        .admin()
        .indices()
        .prepareCreate("test")
        .addAlias(new Alias("alias"))
        .addMapping(
            "type1",
            XContentFactory.jsonBuilder()
                .startObject()
                .startObject("type1")
                .startObject("_routing")
                .field("required", true)
                .endObject()
                .endObject()
                .endObject())
        .execute()
        .actionGet();
    ensureGreen();

    logger.info("--> indexing with id [1], and routing [0]");
    client()
        .prepareIndex(indexOrAlias(), "type1", "1")
        .setRouting("0")
        .setSource("field", "value1")
        .get();
    logger.info("--> indexing with id [2], and routing [0]");
    client()
        .prepareIndex(indexOrAlias(), "type1", "2")
        .setRouting("0")
        .setSource("field", "value2")
        .setRefresh(true)
        .get();

    logger.info("--> verifying get with id [1] with routing [0], should succeed");
    assertThat(
        client()
            .prepareGet(indexOrAlias(), "type1", "1")
            .setRouting("0")
            .execute()
            .actionGet()
            .isExists(),
        equalTo(true));

    logger.info("--> verifying get with id [1], with no routing, should fail");
    try {
      client().prepareGet(indexOrAlias(), "type1", "1").get();
      fail();
    } catch (RoutingMissingException e) {
      assertThat(e.getMessage(), equalTo("routing is required for [test]/[type1]/[1]"));
    }

    logger.info("--> verifying explain with id [2], with routing [0], should succeed");
    ExplainResponse explainResponse =
        client()
            .prepareExplain(indexOrAlias(), "type1", "2")
            .setQuery(QueryBuilders.matchAllQuery())
            .setRouting("0")
            .get();
    assertThat(explainResponse.isExists(), equalTo(true));
    assertThat(explainResponse.isMatch(), equalTo(true));

    logger.info("--> verifying explain with id [2], with no routing, should fail");
    try {
      client()
          .prepareExplain(indexOrAlias(), "type1", "2")
          .setQuery(QueryBuilders.matchAllQuery())
          .get();
      fail();
    } catch (RoutingMissingException e) {
      assertThat(e.getMessage(), equalTo("routing is required for [test]/[type1]/[2]"));
    }

    logger.info("--> verifying term vector with id [1], with routing [0], should succeed");
    TermVectorResponse termVectorResponse =
        client().prepareTermVector(indexOrAlias(), "type1", "1").setRouting("0").get();
    assertThat(termVectorResponse.isExists(), equalTo(true));
    assertThat(termVectorResponse.getId(), equalTo("1"));

    try {
      client().prepareTermVector(indexOrAlias(), "type1", "1").get();
      fail();
    } catch (RoutingMissingException e) {
      assertThat(e.getMessage(), equalTo("routing is required for [test]/[type1]/[1]"));
    }

    UpdateResponse updateResponse =
        client()
            .prepareUpdate(indexOrAlias(), "type1", "1")
            .setRouting("0")
            .setDoc("field1", "value1")
            .get();
    assertThat(updateResponse.getId(), equalTo("1"));
    assertThat(updateResponse.getVersion(), equalTo(2l));

    try {
      client().prepareUpdate(indexOrAlias(), "type1", "1").setDoc("field1", "value1").get();
      fail();
    } catch (RoutingMissingException e) {
      assertThat(e.getMessage(), equalTo("routing is required for [test]/[type1]/[1]"));
    }

    logger.info("--> verifying mget with ids [1,2], with routing [0], should succeed");
    MultiGetResponse multiGetResponse =
        client()
            .prepareMultiGet()
            .add(new MultiGetRequest.Item(indexOrAlias(), "type1", "1").routing("0"))
            .add(new MultiGetRequest.Item(indexOrAlias(), "type1", "2").routing("0"))
            .get();
    assertThat(multiGetResponse.getResponses().length, equalTo(2));
    assertThat(multiGetResponse.getResponses()[0].isFailed(), equalTo(false));
    assertThat(multiGetResponse.getResponses()[0].getResponse().getId(), equalTo("1"));
    assertThat(multiGetResponse.getResponses()[1].isFailed(), equalTo(false));
    assertThat(multiGetResponse.getResponses()[1].getResponse().getId(), equalTo("2"));

    logger.info("--> verifying mget with ids [1,2], with no routing, should fail");
    multiGetResponse =
        client()
            .prepareMultiGet()
            .add(new MultiGetRequest.Item(indexOrAlias(), "type1", "1"))
            .add(new MultiGetRequest.Item(indexOrAlias(), "type1", "2"))
            .get();
    assertThat(multiGetResponse.getResponses().length, equalTo(2));
    assertThat(multiGetResponse.getResponses()[0].isFailed(), equalTo(true));
    assertThat(multiGetResponse.getResponses()[0].getFailure().getId(), equalTo("1"));
    assertThat(
        multiGetResponse.getResponses()[0].getFailure().getMessage(),
        equalTo("routing is required for [test]/[type1]/[1]"));
    assertThat(multiGetResponse.getResponses()[1].isFailed(), equalTo(true));
    assertThat(multiGetResponse.getResponses()[1].getFailure().getId(), equalTo("2"));
    assertThat(
        multiGetResponse.getResponses()[1].getFailure().getMessage(),
        equalTo("routing is required for [test]/[type1]/[2]"));

    MultiTermVectorsResponse multiTermVectorsResponse =
        client()
            .prepareMultiTermVectors()
            .add(new TermVectorRequest(indexOrAlias(), "type1", "1").routing("0"))
            .add(new TermVectorRequest(indexOrAlias(), "type1", "2").routing("0"))
            .get();
    assertThat(multiTermVectorsResponse.getResponses().length, equalTo(2));
    assertThat(multiTermVectorsResponse.getResponses()[0].getId(), equalTo("1"));
    assertThat(multiTermVectorsResponse.getResponses()[0].isFailed(), equalTo(false));
    assertThat(multiTermVectorsResponse.getResponses()[0].getResponse().getId(), equalTo("1"));
    assertThat(multiTermVectorsResponse.getResponses()[0].getResponse().isExists(), equalTo(true));
    assertThat(multiTermVectorsResponse.getResponses()[1].getId(), equalTo("2"));
    assertThat(multiTermVectorsResponse.getResponses()[1].isFailed(), equalTo(false));
    assertThat(multiTermVectorsResponse.getResponses()[1].getResponse().getId(), equalTo("2"));
    assertThat(multiTermVectorsResponse.getResponses()[1].getResponse().isExists(), equalTo(true));

    multiTermVectorsResponse =
        client()
            .prepareMultiTermVectors()
            .add(new TermVectorRequest(indexOrAlias(), "type1", "1"))
            .add(new TermVectorRequest(indexOrAlias(), "type1", "2"))
            .get();
    assertThat(multiTermVectorsResponse.getResponses().length, equalTo(2));
    assertThat(multiTermVectorsResponse.getResponses()[0].getId(), equalTo("1"));
    assertThat(multiTermVectorsResponse.getResponses()[0].isFailed(), equalTo(true));
    assertThat(
        multiTermVectorsResponse.getResponses()[0].getFailure().getMessage(),
        equalTo("routing is required for [test]/[type1]/[1]"));
    assertThat(multiTermVectorsResponse.getResponses()[0].getResponse(), nullValue());
    assertThat(multiTermVectorsResponse.getResponses()[1].getId(), equalTo("2"));
    assertThat(multiTermVectorsResponse.getResponses()[1].isFailed(), equalTo(true));
    assertThat(multiTermVectorsResponse.getResponses()[1].getResponse(), nullValue());
    assertThat(
        multiTermVectorsResponse.getResponses()[1].getFailure().getMessage(),
        equalTo("routing is required for [test]/[type1]/[2]"));
  }