Create index with multiple different types / add new type to existing index via JavaAPI


(Alexander Kley) #1

Hi guys,

is it possible to create an index with more than one type? Things like this
don't work and thrown a 'Index allready exists':

// create index with more than one type with default mapping
public void createIndex(final String index, final Map<String, Object>
indexSetting, final String... types) throws IndexCreateException
{
final Map<String, Object> indexSettingTmp = indexSetting == null ? new
HashMap<String, Object>() : indexSetting;
final Settings settings = getImmutableSettings(indexSettingTmp);

for (final String type : types)
{

esClient.admin().indices().prepareCreate(index).setSettings(settings).addMapping(type,
null).execute().actionGet();
}

}

is it possible to to add new type(s) to an existing index via Java API. I
cant find any examples for Java API

Thanks in advance
Alex

--
You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elasticsearch+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


(Josh Canfield) #2

I don't know the java api, I'm strictly using the REST, but you are calling
create index for every type which of course is giving you "index already
exists"

Have you considered not calling execute() until you've added all the
mappings?

On Fri, Oct 25, 2013 at 2:43 AM, Alex alexander.kley@gmail.com wrote:

Hi guys,

is it possible to create an index with more than one type? Things like
this don't work and thrown a 'Index allready exists':

// create index with more than one type with default mapping
public void createIndex(final String index, final Map<String, Object>
indexSetting, final String... types) throws IndexCreateException
{
final Map<String, Object> indexSettingTmp = indexSetting == null ? new
HashMap<String, Object>() : indexSetting;
final Settings settings = getImmutableSettings(indexSettingTmp);

for (final String type : types)
{

esClient.admin().indices().prepareCreate(index).setSettings(settings).addMapping(type,
null).execute().actionGet();
}

}

is it possible to to add new type(s) to an existing index via Java API. I
cant find any examples for Java API

Thanks in advance
Alex

--
You received this message because you are subscribed to the Google Groups
"elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an
email to elasticsearch+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

--
You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elasticsearch+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


(Alexander Kley) #3

Hi Josh,

thats a good hint! I'll try to add .setType() in a for-loop and after this
the execute() will be append. Good idea to create multiple types in one
request. But how can i add further types on existing indexes? Via REST it
seems to be quite simple. But how can I do this via Java API?
Have anybody ideas?

regards,
Alex

Am Freitag, 25. Oktober 2013 19:20:05 UTC+2 schrieb Josh Canfield:

I don't know the java api, I'm strictly using the REST, but you are
calling create index for every type which of course is giving you "index
already exists"

Have you considered not calling execute() until you've added all the
mappings?

On Fri, Oct 25, 2013 at 2:43 AM, Alex <alexand...@gmail.com <javascript:>>wrote:

Hi guys,

is it possible to create an index with more than one type? Things like
this don't work and thrown a 'Index allready exists':

// create index with more than one type with default mapping
public void createIndex(final String index, final Map<String, Object>
indexSetting, final String... types) throws IndexCreateException
{
final Map<String, Object> indexSettingTmp = indexSetting == null ? new
HashMap<String, Object>() : indexSetting;
final Settings settings = getImmutableSettings(indexSettingTmp);

for (final String type : types)
{

esClient.admin().indices().prepareCreate(index).setSettings(settings).addMapping(type,
null).execute().actionGet();
}

}

is it possible to to add new type(s) to an existing index via Java API. I
cant find any examples for Java API

Thanks in advance
Alex

--
You received this message because you are subscribed to the Google Groups
"elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an
email to elasticsearc...@googlegroups.com <javascript:>.
For more options, visit https://groups.google.com/groups/opt_out.

--
You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elasticsearch+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


(Ivan Brusic) #4

I have never created a new type via the Java API, so I can't easily answer
your question, but if you ever need to find the Java equivalent of a REST
call, here are two suggestions:

  1. Look for the Java endpoint of the REST call. It will be in
    the org.elasticsearch.rest.action package and will usually have a
    recognizable name that begins with Rest, such as RestCreateIndexAction. An
    IDE with class name completion helps.
  2. Look at the tests. The majority of elasticsearch tasks are tested via
    numerous unit and integration tests. Good place to steal, I mean borrow,
    code from. :slight_smile:

Give a man a fish, and you feed him for a day; show him how to catch fish,
and you feed him for a lifetime. :slight_smile:

Cheers,

Ivan

On Fri, Oct 25, 2013 at 10:41 AM, Alexander Kley
alexander.kley@gmail.comwrote:

Hi Josh,

thats a good hint! I'll try to add .setType() in a for-loop and after this
the execute() will be append. Good idea to create multiple types in one
request. But how can i add further types on existing indexes? Via REST it
seems to be quite simple. But how can I do this via Java API?
Have anybody ideas?

regards,
Alex

Am Freitag, 25. Oktober 2013 19:20:05 UTC+2 schrieb Josh Canfield:

I don't know the java api, I'm strictly using the REST, but you are
calling create index for every type which of course is giving you "index
already exists"

Have you considered not calling execute() until you've added all the
mappings?

On Fri, Oct 25, 2013 at 2:43 AM, Alex alexand...@gmail.com wrote:

Hi guys,

is it possible to create an index with more than one type? Things like
this don't work and thrown a 'Index allready exists':

// create index with more than one type with default mapping
public void createIndex(final String index, final Map<String, Object>
indexSetting, final String... types) throws IndexCreateException
{
final Map<String, Object> indexSettingTmp = indexSetting == null ? new
HashMap<String, Object>() : indexSetting;
final Settings settings = getImmutableSettings(**indexSettingTmp);

for (final String type : types)
{
esClient.admin().indices().prepareCreate(index).
setSettings(settings).**addMapping(type, null).execute().actionGet();
}

}

is it possible to to add new type(s) to an existing index via Java API.
I cant find any examples for Java API

Thanks in advance
Alex

--
You received this message because you are subscribed to the Google
Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send
an email to elasticsearc...@**googlegroups.com.

For more options, visit https://groups.google.com/**groups/opt_outhttps://groups.google.com/groups/opt_out
.

--
You received this message because you are subscribed to the Google Groups
"elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an
email to elasticsearch+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

--
You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elasticsearch+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


(Jörg Prante) #5
  1. You use the addMapping() method in CreateIndexRequestBuilder with a null
    argument for the mapping. This will not work.

  2. Use the PutMappingRequest

Jörg

--
You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elasticsearch+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


(Alexander Kley) #6

Many thanks to you Ivan, also for the hint to borro code :wink: !

Cheers
Alex

Am Samstag, 26. Oktober 2013 00:22:36 UTC+2 schrieb Ivan Brusic:

I have never created a new type via the Java API, so I can't easily answer
your question, but if you ever need to find the Java equivalent of a REST
call, here are two suggestions:

  1. Look for the Java endpoint of the REST call. It will be in
    the org.elasticsearch.rest.action package and will usually have a
    recognizable name that begins with Rest, such as RestCreateIndexAction. An
    IDE with class name completion helps.
  2. Look at the tests. The majority of elasticsearch tasks are tested via
    numerous unit and integration tests. Good place to steal, I mean borrow,
    code from. :slight_smile:

Give a man a fish, and you feed him for a day; show him how to catch fish,
and you feed him for a lifetime. :slight_smile:

Cheers,

Ivan

On Fri, Oct 25, 2013 at 10:41 AM, Alexander Kley <alexand...@gmail.com<javascript:>

wrote:

Hi Josh,

thats a good hint! I'll try to add .setType() in a for-loop and after
this the execute() will be append. Good idea to create multiple types in
one request. But how can i add further types on existing indexes? Via REST
it seems to be quite simple. But how can I do this via Java API?
Have anybody ideas?

regards,
Alex

Am Freitag, 25. Oktober 2013 19:20:05 UTC+2 schrieb Josh Canfield:

I don't know the java api, I'm strictly using the REST, but you are
calling create index for every type which of course is giving you "index
already exists"

Have you considered not calling execute() until you've added all the
mappings?

On Fri, Oct 25, 2013 at 2:43 AM, Alex alexand...@gmail.com wrote:

Hi guys,

is it possible to create an index with more than one type? Things like
this don't work and thrown a 'Index allready exists':

// create index with more than one type with default mapping
public void createIndex(final String index, final Map<String, Object>
indexSetting, final String... types) throws IndexCreateException
{
final Map<String, Object> indexSettingTmp = indexSetting == null ?
new HashMap<String, Object>() : indexSetting;
final Settings settings = getImmutableSettings(**indexSettingTmp);

for (final String type : types)
{
esClient.admin().indices().prepareCreate(index).
setSettings(settings).**addMapping(type, null).execute().actionGet();
}

}

is it possible to to add new type(s) to an existing index via Java API.
I cant find any examples for Java API

Thanks in advance
Alex

--
You received this message because you are subscribed to the Google
Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send
an email to elasticsearc...@**googlegroups.com.

For more options, visit https://groups.google.com/**groups/opt_outhttps://groups.google.com/groups/opt_out
.

--
You received this message because you are subscribed to the Google Groups
"elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an
email to elasticsearc...@googlegroups.com <javascript:>.
For more options, visit https://groups.google.com/groups/opt_out.

--
You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elasticsearch+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


(system) #7