# Using Java API, how do I find out whether a given type exists in the index?

**URL:** <https://discuss.elastic.co/t/using-java-api-how-do-i-find-out-whether-a-given-type-exists-in-the-index/10418>\
**Category:** Elasticsearch\
**Created:** [January 18, 2013, 11:31pm UTC](https://discuss.elastic.co/t/using-java-api-how-do-i-find-out-whether-a-given-type-exists-in-the-index/10418 "2013-01-18T23:31:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mxims\_2](https://avatars.discourse-cdn.com/v4/letter/m/958977/32.png) [@Mxims\_2](https://discuss.elastic.co/u/Mxims_2)\
**Post date:** [January 18, 2013, 11:31pm UTC](https://discuss.elastic.co/t/using-java-api-how-do-i-find-out-whether-a-given-type-exists-in-the-index/10418/1 "2013-01-18T23:31:12Z")

</div>

I am using the following line to delete the mapping for a type.

client.admin().indices().prepareDeleteMapping().setType("SUPPLIER\_CODE\_1").execute().actionGet();

How do I check whether the "TYPE" "SUPPLIER\_CODE\_1" exists in the index  
before I try to delete it? Right now, if the TYPE doesn't exists, ES throws  
few exceptions, which is expected I guess. I just want things to be clean.

Also I wonder whether this is the correct method to clear the index for a  
given TYPE.

Please help.

Thanks  
Mxims

--

---

<div class="post-metadata">

**Author:** ![egaumer](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/egaumer/32/2365_2.png) [@egaumer](https://discuss.elastic.co/u/egaumer)\
**Post date:** [January 19, 2013, 12:11am UTC](https://discuss.elastic.co/t/using-java-api-how-do-i-find-out-whether-a-given-type-exists-in-the-index/10418/2 "2013-01-19T00:11:22Z")

</div>

I think it looks something like this...

ClusterStateResponse resp =  
client.admin().cluster().prepareState().execute().actionGet();  
Map\<String, MappingMetaData\> mappings =  
resp.state().metaData().index(index).mappings();  
if (mappings.containsKey(type)) {/\* type exists \*/}

Deleting the mapping...

client.admin().indices().deleteMapping(mapping\_name).actionGet();

On Friday, January 18, 2013 6:31:12 PM UTC-5, Mxims wrote:

> I am using the following line to delete the mapping for a type.
> 
> client.admin().indices().prepareDeleteMapping().setType("SUPPLIER\_CODE\_1").execute().actionGet();
> 
> How do I check whether the "TYPE" "SUPPLIER\_CODE\_1" exists in the index  
> before I try to delete it? Right now, if the TYPE doesn't exists, ES throws  
> few exceptions, which is expected I guess. I just want things to be clean.
> 
> Also I wonder whether this is the correct method to clear the index for a  
> given TYPE.
> 
> Please help.
> 
> Thanks  
> Mxims

--

---

<div class="post-metadata">

**Author:** ![Mxims\_2](https://avatars.discourse-cdn.com/v4/letter/m/958977/32.png) [@Mxims\_2](https://discuss.elastic.co/u/Mxims_2)\
**Post date:** [January 19, 2013, 1:19am UTC](https://discuss.elastic.co/t/using-java-api-how-do-i-find-out-whether-a-given-type-exists-in-the-index/10418/3 "2013-01-19T01:19:00Z")

</div>

Hi egaumer,

Thank you so much for your help!

Here is what I did.. it worked 🙂 Have a great weekend.

```
    ClusterStateResponse resp = 

```

client.admin().cluster().prepareState().execute().actionGet();  
Map mappings = resp.state().metaData().index("supplier").mappings();  
System.out.println("mappings " + mappings);

```
    //if (mappings.containsKey(type)) {/* type exists */}

    MappingMetaData data = (MappingMetaData) mappings.get("0000112275");

    System.out.println("data >>>>>>>> " + data.type());
    System.out.println("data >>>>>>>> " + data.getSourceAsMap());
    DeleteMappingRequest req = new DeleteMappingRequest("supplier");
    req.type(data.type());
    client.admin().indices().deleteMapping(req).actionGet();

```

Thanks

On Friday, January 18, 2013 4:11:22 PM UTC-8, egaumer wrote:

> I think it looks something like this...
> 
> ClusterStateResponse resp =  
> client.admin().cluster().prepareState().execute().actionGet();  
> Map\<String, MappingMetaData\> mappings =  
> resp.state().metaData().index(index).mappings();  
> if (mappings.containsKey(type)) {/\* type exists \*/}
> 
> Deleting the mapping...
> 
> client.admin().indices().deleteMapping(mapping\_name).actionGet();
> 
> On Friday, January 18, 2013 6:31:12 PM UTC-5, Mxims wrote:
> 
> > I am using the following line to delete the mapping for a type.
> > 
> > client.admin().indices().prepareDeleteMapping().setType("SUPPLIER\_CODE\_1").execute().actionGet();
> > 
> > How do I check whether the "TYPE" "SUPPLIER\_CODE\_1" exists in the index  
> > before I try to delete it? Right now, if the TYPE doesn't exists, ES throws  
> > few exceptions, which is expected I guess. I just want things to be clean.
> > 
> > Also I wonder whether this is the correct method to clear the index for a  
> > given TYPE.
> > 
> > Please help.
> > 
> > Thanks  
> > Mxims

--

---

<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, 2017, 2:55am UTC](https://discuss.elastic.co/t/using-java-api-how-do-i-find-out-whether-a-given-type-exists-in-the-index/10418/4 "2017-07-06T02:55:35Z")

</div>


