@Test
  public void testExternalVersioningInitialDelete() throws Exception {
    client.admin().indices().prepareDelete().execute().actionGet();

    client.admin().indices().prepareCreate("test").execute().actionGet();
    client.admin().cluster().prepareHealth("test").setWaitForGreenStatus().execute().actionGet();

    DeleteResponse deleteResponse =
        client2
            .prepareDelete("test", "type", "1")
            .setVersion(17)
            .setVersionType(VersionType.EXTERNAL)
            .execute()
            .actionGet();
    assertThat(deleteResponse.isNotFound(), equalTo(true));

    try {
      client
          .prepareIndex("test", "type", "1")
          .setSource("field1", "value1_1")
          .setVersion(13)
          .setVersionType(VersionType.EXTERNAL)
          .execute()
          .actionGet();
    } catch (ElasticSearchException e) {
      assertThat(e.unwrapCause(), instanceOf(VersionConflictEngineException.class));
    }

    client
        .prepareIndex("test", "type", "1")
        .setSource("field1", "value1_1")
        .setVersion(18)
        .setVersionType(VersionType.EXTERNAL)
        .execute()
        .actionGet();
  }
  @Test
  public void testFastVectorHighlighterManyDocs() throws Exception {
    try {
      client.admin().indices().prepareDelete("test").execute().actionGet();
    } catch (ElasticSearchException e) {
      assertThat(e.unwrapCause(), instanceOf(IndexMissingException.class));
    }
    client
        .admin()
        .indices()
        .prepareCreate("test")
        .addMapping("type1", type1TermVectorMapping())
        .execute()
        .actionGet();
    client.admin().cluster().prepareHealth("test").setWaitForGreenStatus().execute().actionGet();

    int COUNT = 100;
    logger.info("--> indexing docs");
    for (int i = 0; i < COUNT; i++) {
      client
          .prepareIndex("test", "type1", Integer.toString(i))
          .setSource("field1", "test " + i)
          .execute()
          .actionGet();
      if (i % 5 == 0) {
        // flush so we get updated readers and segmented readers
        client.admin().indices().prepareFlush().execute().actionGet();
      }
    }

    client.admin().indices().prepareRefresh().execute().actionGet();

    logger.info("--> searching explicitly on field1 and highlighting on it");
    SearchResponse searchResponse =
        client
            .prepareSearch()
            .setSize(COUNT)
            .setQuery(termQuery("field1", "test"))
            .addHighlightedField("field1", 100, 0)
            .execute()
            .actionGet();
    assertThat(searchResponse.hits().totalHits(), equalTo((long) COUNT));
    assertThat(searchResponse.hits().hits().length, equalTo(COUNT));
    for (SearchHit hit : searchResponse.hits()) {
      // LUCENE 3.1 UPGRADE: Caused adding the space at the end...
      assertThat(
          hit.highlightFields().get("field1").fragments()[0],
          equalTo("<em>test</em> " + hit.id() + " "));
    }

    logger.info("--> searching explicitly on field1 and highlighting on it, with DFS");
    searchResponse =
        client
            .prepareSearch()
            .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
            .setSize(COUNT)
            .setQuery(termQuery("field1", "test"))
            .addHighlightedField("field1", 100, 0)
            .execute()
            .actionGet();
    assertThat(searchResponse.hits().totalHits(), equalTo((long) COUNT));
    assertThat(searchResponse.hits().hits().length, equalTo(COUNT));
    for (SearchHit hit : searchResponse.hits()) {
      // LUCENE 3.1 UPGRADE: Caused adding the space at the end...
      assertThat(
          hit.highlightFields().get("field1").fragments()[0],
          equalTo("<em>test</em> " + hit.id() + " "));
    }

    logger.info("--> searching explicitly _all and highlighting on _all");
    searchResponse =
        client
            .prepareSearch()
            .setSize(COUNT)
            .setQuery(termQuery("_all", "test"))
            .addHighlightedField("_all", 100, 0)
            .execute()
            .actionGet();
    assertThat(searchResponse.hits().totalHits(), equalTo((long) COUNT));
    assertThat(searchResponse.hits().hits().length, equalTo(COUNT));
    for (SearchHit hit : searchResponse.hits()) {
      // LUCENE 3.1 UPGRADE: Caused adding the space at the end...
      assertThat(
          hit.highlightFields().get("_all").fragments()[0],
          equalTo("<em>test</em> " + hit.id() + "  "));
    }
  }
  @Test
  public void testSimpleVersioningWithFlush() throws Exception {
    try {
      client.admin().indices().prepareDelete("test").execute().actionGet();
    } catch (IndexMissingException e) {
      // its ok
    }
    client.admin().indices().prepareCreate("test").execute().actionGet();
    client.admin().cluster().prepareHealth("test").setWaitForGreenStatus().execute().actionGet();

    IndexResponse indexResponse =
        client
            .prepareIndex("test", "type", "1")
            .setSource("field1", "value1_1")
            .execute()
            .actionGet();
    assertThat(indexResponse.version(), equalTo(1l));

    client.admin().indices().prepareFlush().execute().actionGet();

    indexResponse =
        client
            .prepareIndex("test", "type", "1")
            .setSource("field1", "value1_2")
            .setVersion(1)
            .execute()
            .actionGet();
    assertThat(indexResponse.version(), equalTo(2l));

    client.admin().indices().prepareFlush().execute().actionGet();

    try {
      client
          .prepareIndex("test", "type", "1")
          .setSource("field1", "value1_1")
          .setVersion(1)
          .execute()
          .actionGet();
    } catch (ElasticSearchException e) {
      assertThat(e.unwrapCause(), instanceOf(VersionConflictEngineException.class));
    }

    try {
      client2
          .prepareIndex("test", "type", "1")
          .setSource("field1", "value1_1")
          .setVersion(1)
          .execute()
          .actionGet();
    } catch (ElasticSearchException e) {
      assertThat(e.unwrapCause(), instanceOf(VersionConflictEngineException.class));
    }

    try {
      client
          .prepareIndex("test", "type", "1")
          .setCreate(true)
          .setSource("field1", "value1_1")
          .setVersion(1)
          .execute()
          .actionGet();
    } catch (ElasticSearchException e) {
      assertThat(e.unwrapCause(), instanceOf(VersionConflictEngineException.class));
    }

    try {
      client2
          .prepareIndex("test", "type", "1")
          .setCreate(true)
          .setSource("field1", "value1_1")
          .setVersion(1)
          .execute()
          .actionGet();
    } catch (ElasticSearchException e) {
      assertThat(e.unwrapCause(), instanceOf(VersionConflictEngineException.class));
    }

    try {
      client.prepareDelete("test", "type", "1").setVersion(1).execute().actionGet();
    } catch (ElasticSearchException e) {
      assertThat(e.unwrapCause(), instanceOf(VersionConflictEngineException.class));
    }

    try {
      client2.prepareDelete("test", "type", "1").setVersion(1).execute().actionGet();
    } catch (ElasticSearchException e) {
      assertThat(e.unwrapCause(), instanceOf(VersionConflictEngineException.class));
    }

    client.admin().indices().prepareRefresh().execute().actionGet();
    for (int i = 0; i < 10; i++) {
      assertThat(
          client.prepareGet("test", "type", "1").execute().actionGet().getVersion(), equalTo(2l));
    }

    for (int i = 0; i < 10; i++) {
      SearchResponse searchResponse =
          client.prepareSearch().setQuery(matchAllQuery()).setVersion(true).execute().actionGet();
      assertThat(searchResponse.getHits().getAt(0).version(), equalTo(2l));
    }
  }
  @Test
  public void testExternalVersioning() throws Exception {
    try {
      client.admin().indices().prepareDelete("test").execute().actionGet();
    } catch (IndexMissingException e) {
      // its ok
    }
    client.admin().indices().prepareCreate("test").execute().actionGet();
    client.admin().cluster().prepareHealth("test").setWaitForGreenStatus().execute().actionGet();

    IndexResponse indexResponse =
        client
            .prepareIndex("test", "type", "1")
            .setSource("field1", "value1_1")
            .setVersion(12)
            .setVersionType(VersionType.EXTERNAL)
            .execute()
            .actionGet();
    assertThat(indexResponse.version(), equalTo(12l));

    indexResponse =
        client
            .prepareIndex("test", "type", "1")
            .setSource("field1", "value1_1")
            .setVersion(14)
            .setVersionType(VersionType.EXTERNAL)
            .execute()
            .actionGet();
    assertThat(indexResponse.version(), equalTo(14l));

    try {
      client
          .prepareIndex("test", "type", "1")
          .setSource("field1", "value1_1")
          .setVersion(13)
          .setVersionType(VersionType.EXTERNAL)
          .execute()
          .actionGet();
    } catch (ElasticSearchException e) {
      assertThat(e.unwrapCause(), instanceOf(VersionConflictEngineException.class));
    }

    client.admin().indices().prepareRefresh().execute().actionGet();
    for (int i = 0; i < 10; i++) {
      assertThat(
          client.prepareGet("test", "type", "1").execute().actionGet().getVersion(), equalTo(14l));
    }

    DeleteResponse deleteResponse =
        client2
            .prepareDelete("test", "type", "1")
            .setVersion(17)
            .setVersionType(VersionType.EXTERNAL)
            .execute()
            .actionGet();
    assertThat(deleteResponse.isNotFound(), equalTo(false));
    assertThat(deleteResponse.getVersion(), equalTo(17l));

    try {
      client2
          .prepareDelete("test", "type", "1")
          .setVersion(2)
          .setVersionType(VersionType.EXTERNAL)
          .execute()
          .actionGet();
    } catch (ElasticSearchException e) {
      assertThat(e.unwrapCause(), instanceOf(VersionConflictEngineException.class));
    }

    deleteResponse =
        client2
            .prepareDelete("test", "type", "1")
            .setVersion(18)
            .setVersionType(VersionType.EXTERNAL)
            .execute()
            .actionGet();
    assertThat(deleteResponse.isNotFound(), equalTo(true));
    assertThat(deleteResponse.getVersion(), equalTo(18l));
  }