# Reindexing using Java Client - What happens when one or both of the index do not exist

**URL:** <https://discuss.elastic.co/t/reindexing-using-java-client-what-happens-when-one-or-both-of-the-index-do-not-exist/335505>\
**Category:** Elasticsearch\
**Created:** [June 8, 2023, 6:05am UTC](https://discuss.elastic.co/t/reindexing-using-java-client-what-happens-when-one-or-both-of-the-index-do-not-exist/335505 "2023-06-08T06:05:53Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![sanskarfc](https://avatars.discourse-cdn.com/v4/letter/s/f04885/32.png) [@sanskarfc](https://discuss.elastic.co/u/sanskarfc)\
**Post date:** [June 8, 2023, 6:05am UTC](https://discuss.elastic.co/t/reindexing-using-java-client-what-happens-when-one-or-both-of-the-index-do-not-exist/335505/1 "2023-06-08T06:05:53Z")

</div>

What happens when one or both of the index do not exist if we do reindexing using Java Client? When I tested on local, it completes successfully without throwing exception.

How does one test if an index already exists or not?

I am new to Elasticsearch, any help would be really appreciated. Thanks 👋

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [June 8, 2023, 6:44am UTC](https://discuss.elastic.co/t/reindexing-using-java-client-what-happens-when-one-or-both-of-the-index-do-not-exist/335505/2 "2023-06-08T06:44:13Z")

</div>

Welcome!

What does your code look like?

---

<div class="post-metadata">

**Author:** ![sanskarfc](https://avatars.discourse-cdn.com/v4/letter/s/f04885/32.png) [@sanskarfc](https://discuss.elastic.co/u/sanskarfc)\
**Post date:** [June 8, 2023, 9:26am UTC](https://discuss.elastic.co/t/reindexing-using-java-client-what-happens-when-one-or-both-of-the-index-do-not-exist/335505/3 "2023-06-08T09:26:05Z")

</div>

I am using the `RestHighLevelClient` from ES Java REST Client to call the reindex method. I can see after the reindex that the total number of documents proccessed using the `BulkScrollByResponse.getTotal()`.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [June 8, 2023, 9:30am UTC](https://discuss.elastic.co/t/reindexing-using-java-client-what-happens-when-one-or-both-of-the-index-do-not-exist/335505/4 "2023-06-08T09:30:40Z")

</div>

Could you share the piece of code you are using please?

What is your elasticsearch version?

By the way, you should now switch to the new official Java Client.

---

<div class="post-metadata">

**Author:** ![sanskarfc](https://avatars.discourse-cdn.com/v4/letter/s/f04885/32.png) [@sanskarfc](https://discuss.elastic.co/u/sanskarfc)\
**Post date:** [June 8, 2023, 9:42am UTC](https://discuss.elastic.co/t/reindexing-using-java-client-what-happens-when-one-or-both-of-the-index-do-not-exist/335505/5 "2023-06-08T09:42:52Z")

</div>

This is the skeleton code of what I am doing:

```java
try {
    ReindexRequest request = new ReindexRequest().setSourceIndices(sourceIndex).setDestIndex(desIndex);
    BulkByScrollResponse response = restHighLevelClient.reindex(request, RequestOptions.DEFAULT);
} catch (Exception ex) {
    // do stuff
}

```

I except to get an exception if either of the indices `targetIndex` and/or `sourceIndex` are invalid/don't exist.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [June 8, 2023, 10:06am UTC](https://discuss.elastic.co/t/reindexing-using-java-client-what-happens-when-one-or-both-of-the-index-do-not-exist/335505/6 "2023-06-08T10:06:24Z")

</div>

You did not tell which version you are using, but with a simple test on 8.8 in Kibana dev console:

```auto
POST /_reindex
{
  "source": {
    "index": "doesnotexist"
  },
  "dest": {
    "index": "foo"
  }
}

```

I'm getting a 404 error:

```auto
{
  "error": {
    "root_cause": [
      {
        "type": "index_not_found_exception",
        "reason": "no such index [doesnotexist]",
        "resource.type": "index_or_alias",
        "resource.id": "doesnotexist",
        "index_uuid": "_na_",
        "index": "doesnotexist"
      }
    ],
    "type": "index_not_found_exception",
    "reason": "no such index [doesnotexist]",
    "resource.type": "index_or_alias",
    "resource.id": "doesnotexist",
    "index_uuid": "_na_",
    "index": "doesnotexist"
  },
  "status": 404
}

```

I'd expect the same to happen in the Java Client. I just checked it with the new java client:

```auto
try {
  client.reindex(rr -> rr.source(s -> s.index("does-not-exists")).dest(d -> d.index("foo")));
} catch (ElasticsearchException e) {
  logger.info("Got error {}", e.response());
}

```

This gives:

```auto
12:05:21,952 INFO [f.p.t.e.h.EsClientIT] Got error ErrorResponse: {"error":{"index_uuid":"_na_","index":"does-not-exists","resource.type":"index_or_alias","resource.id":"does-not-exists","type":"index_not_found_exception","reason":"no such index [does-not-exists]","root_cause":[{"index_uuid":"_na_","index":"does-not-exists","resource.type":"index_or_alias","resource.id":"does-not-exists","type":"index_not_found_exception","reason":"no such index [does-not-exists]"}]},"status":404}

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 6, 2023, 10:07am UTC](https://discuss.elastic.co/t/reindexing-using-java-client-what-happens-when-one-or-both-of-the-index-do-not-exist/335505/7 "2023-07-06T10:07:05Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
