Using the java API to create/update mappings

Hi all,

A couple questions on mapping definition. I run ES embedded in my
application nodes.

  1. Using the java API only, no source file, how do you initialize your
    mappings? What I want to do is change the "default" mapping so that
    all indexing and query parsing is done with this configuration.

  2. When I index and build my JSon entity representation, I want to
    specify the tokenize and store attribute for each field individually.

  3. Is there some example code I can check somewhere?

Here's what I have right now but I'm getting a 'mapping type is
missing' exception...

Thanks for helping...

    // Initialize the ES embedded client.

    // Set path.data from the app.lucene.dir setting in

application.properties.

    Properties esProperties = new Properties();
    esProperties.putAll(configuration);
    esProperties.setProperty("path.data",

configuration.getProperty(ConfigurationConstants.PROP_APP_LUCENE_DIR));

    Settings settings =

ImmutableSettings.settingsBuilder().put(esProperties).build();
elasticSearchNode =
NodeBuilder.nodeBuilder().settings(settings).node();
elasticSearchClient = elasticSearchNode.client();

    // Default configuration.

    XContentBuilder mappings = null;
    try
    {
        mappings = XContentFactory.jsonBuilder();

mappings.startObject().startObject("analysis").startObject("analyzer").startObject("default")
.field("type", "custom").field("tokenizer",
"whitespace")
.field("filter", new String[] { "snowball",
"standard", "lowercase" }).endObject().endObject()
.endObject().endObject();

        logger.info(mappings.string());
    }
    catch (IOException e)
    {
        ValidateState.fail();
    }

    PutMappingRequestBuilder mappingRequest =

elasticSearchClient.admin().indices().preparePutMapping("default");
mappingRequest.setSource(mappings);
PutMappingResponse mappingResponse =
mappingRequest.execute().actionGet();

You put the index configuration as a mapping for a type. Mappings are for document types, index settings are for things like custom analyzers.

On Tuesday, June 28, 2011 at 11:23 PM, Spring Ninja wrote:

Hi all,

A couple questions on mapping definition. I run ES embedded in my
application nodes.

  1. Using the java API only, no source file, how do you initialize your
    mappings? What I want to do is change the "default" mapping so that
    all indexing and query parsing is done with this configuration.

  2. When I index and build my JSon entity representation, I want to
    specify the tokenize and store attribute for each field individually.

  3. Is there some example code I can check somewhere?

Here's what I have right now but I'm getting a 'mapping type is
missing' exception...

Thanks for helping...

// Initialize the ES embedded client.

// Set path.data from the app.lucene.dir setting in
application.properties.

Properties esProperties = new Properties();
esProperties.putAll(configuration);
esProperties.setProperty("path.data",
configuration.getProperty(ConfigurationConstants.PROP_APP_LUCENE_DIR));

Settings settings =
ImmutableSettings.settingsBuilder().put(esProperties).build();
elasticSearchNode =
NodeBuilder.nodeBuilder().settings(settings).node();
elasticSearchClient = elasticSearchNode.client();

// Default configuration.

XContentBuilder mappings = null;
try
{
mappings = XContentFactory.jsonBuilder();

mappings.startObject().startObject("analysis").startObject("analyzer").startObject("default")
.field("type", "custom").field("tokenizer",
"whitespace")
.field("filter", new String { "snowball",
"standard", "lowercase" }).endObject().endObject()
.endObject().endObject();

logger.info (http://logger.info)(mappings.string());
}
catch (IOException e)
{
ValidateState.fail();
}

PutMappingRequestBuilder mappingRequest =
elasticSearchClient.admin().indices().preparePutMapping("default");
mappingRequest.setSource(mappings);
PutMappingResponse mappingResponse =
mappingRequest.execute().actionGet();

Yeah, that makes sense and... it works!!! :wink:

Thanks!

Here's how I redefined the default analyser for those that may have
the same need:

    XContentBuilder indexSettings = null;
    try
    {
        indexSettings = XContentFactory.jsonBuilder();

indexSettings.startObject().startObject("index").startObject("analysis").startObject("analyzer")
.startObject("default").field("type",
"custom").field("tokenizer", "whitespace")
.field("filter", new String[] { "asciifolding",
"lowercase" }).endObject().endObject().endObject()
.endObject().endObject();

        logger.info(indexSettings.string());

        UpdateSettingsRequestBuilder settingsRequest =

elasticSearchClient.admin().indices()
.prepareUpdateSettings();
settingsRequest.setSettings(indexSettings.string());
UpdateSettingsResponse settingsResponse =
settingsRequest.execute().actionGet();
}
catch (IOException e)
{
ValidateState.fail();
}