Skip to content

Commit

Permalink
Added support for aliases to create index api
Browse files Browse the repository at this point in the history
It is now possible to specify aliases during index creation:

curl -XPUT 'http://localhost:9200/test' -d '
{
    "aliases" : {
        "alias1" : {},
        "alias2" : {
            "filter" : { "term" : {"field":"value"}}
        }
    }
}'

Closes #4920
  • Loading branch information
javanna committed Feb 17, 2014
1 parent ccfd7f8 commit 70a38d5
Show file tree
Hide file tree
Showing 13 changed files with 647 additions and 59 deletions.
24 changes: 23 additions & 1 deletion docs/reference/indices/aliases.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ Adding user alias:
[source,js]
--------------------------------------------------
curl -XPUT 'localhost:9200/users/_alias/user_12' -d '{
"routing" : "12",
"routing" : "12",
"filter" : {
"term" : {
"user_id" : 12
Expand All @@ -194,6 +194,28 @@ curl -XPUT 'localhost:9200/users/_alias/user_12' -d '{
}'
--------------------------------------------------

[float]
[[alias-index-creation]]
=== Aliases during index creation

coming[1.1.0]

Aliases can also be specified during <<create-index-aliases,index creation>>:

[source,js]
--------------------------------------------------
curl -XPUT localhost:9200/logs_20142801 -d '{
"aliases" : {
"current_day" : {},
"2014" : {
"filter" : {
"term" : {"year" : 2014 }
}
}
}
}'
--------------------------------------------------

[float]
[[deleting]]
=== Delete aliases
Expand Down
23 changes: 23 additions & 0 deletions docs/reference/indices/create-index.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,26 @@ curl -XPUT localhost:9200/test -d '{
}
}'
--------------------------------------------------

[float]
[[create-index-aliases]]
=== Aliases

coming[1.1.0]

The create index API allows also to provide a set of <<indices-aliases,aliases>>:

[source,js]
--------------------------------------------------
curl -XPUT localhost:9200/test -d '{
"aliases" : {
"alias_1" : {},
"alias_2" : {
"filter" : {
"term" : {"user" : "kimchy" }
},
"routing" : "kimchy"
}
}
}'
--------------------------------------------------
41 changes: 40 additions & 1 deletion rest-api-spec/test/indices.create/10_basic.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,35 @@
- match: {test_index.warmers.test_warmer.source.query.match_all: {}}

---
"Create index with mappings, settings and warmers":
"Create index with aliases":

- do:
indices.create:
index: test_index
body:
aliases:
test_alias: {}
test_blias:
routing: b
test_clias:
filter:
term:
field : value

- do:
indices.get_alias:
index: test_index

- match: {test_index.aliases.test_alias: {}}
- match: {test_index.aliases.test_blias.search_routing: b}
- match: {test_index.aliases.test_blias.index_routing: b}
- is_false: test_index.aliases.test_blias.filter
- match: {test_index.aliases.test_clias.filter.term.field: value}
- is_false: test_index.aliases.test_clias.index_routing
- is_false: test_index.aliases.test_clias.search_routing

---
"Create index with mappings, settings, warmers and aliases":

- do:
indices.create:
Expand All @@ -65,6 +93,9 @@
source:
query:
match_all: {}
aliases:
test_alias: {}
test_blias: {routing: b}

- do:
indices.get_mapping:
Expand All @@ -83,3 +114,11 @@
index: test_index

- match: { test_index.warmers.test_warmer.source.query.match_all: {}}

- do:
indices.get_alias:
index: test_index

- match: { test_index.aliases.test_alias: {}}
- match: { test_index.aliases.test_blias.search_routing: b}
- match: { test_index.aliases.test_blias.index_routing: b}
28 changes: 8 additions & 20 deletions rest-api-spec/test/indices.get_alias/10_basic.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,18 @@ setup:
- do:
indices.create:
index: test_index
body:
aliases:
test_alias: {}
test_blias: {}

- do:
indices.create:
index: test_index_2

- do:
indices.put_alias:
index: test_index
name: test_alias

- do:
indices.put_alias:
index: test_index
name: test_blias

- do:
indices.put_alias:
index: test_index_2
name: test_alias

- do:
indices.put_alias:
index: test_index_2
name: test_blias
body:
aliases:
test_alias: {}
test_blias: {}

---
"Get all aliases via /_alias":
Expand Down
234 changes: 234 additions & 0 deletions src/main/java/org/elasticsearch/action/admin/indices/alias/Alias.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,234 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.elasticsearch.action.admin.indices.alias;

import org.elasticsearch.ElasticsearchGenerationException;
import org.elasticsearch.ElasticsearchIllegalArgumentException;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Streamable;
import org.elasticsearch.common.xcontent.*;
import org.elasticsearch.index.query.FilterBuilder;

import java.io.IOException;
import java.util.Map;

/**
* Represents an alias, to be associated with an index
*/
public class Alias implements Streamable {

private String name;

@Nullable
private String filter;

@Nullable
private String indexRouting;

@Nullable
private String searchRouting;

private Alias() {

}

public Alias(String name) {
this.name = name;
}

public Alias(String name, String filter) {
this.name = name;
this.filter = filter;
}

/**
* Returns the alias name
*/
public String name() {
return name;
}

/**
* Returns the filter associated with the alias
*/
public String filter() {
return filter;
}

/**
* Associates a filter to the alias
*/
public Alias filter(String filter) {
this.filter = filter;
return this;
}

/**
* Associates a filter to the alias
*/
public Alias filter(Map<String, Object> filter) {
if (filter == null || filter.isEmpty()) {
this.filter = null;
return this;
}
try {
XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON);
builder.map(filter);
this.filter = builder.string();
return this;
} catch (IOException e) {
throw new ElasticsearchGenerationException("Failed to generate [" + filter + "]", e);
}
}

/**
* Associates a filter to the alias
*/
public Alias filter(FilterBuilder filterBuilder) {
if (filterBuilder == null) {
this.filter = null;
return this;
}
try {
XContentBuilder builder = XContentFactory.jsonBuilder();
filterBuilder.toXContent(builder, ToXContent.EMPTY_PARAMS);
builder.close();
this.filter = builder.string();
return this;
} catch (IOException e) {
throw new ElasticsearchGenerationException("Failed to build json for alias request", e);
}
}

/**
* Associates a routing value to the alias
*/
public Alias routing(String routing) {
this.indexRouting = routing;
this.searchRouting = routing;
return this;
}

/**
* Returns the index routing value associated with the alias
*/
public String indexRouting() {
return indexRouting;
}

/**
* Associates an index routing value to the alias
*/
public Alias indexRouting(String indexRouting) {
this.indexRouting = indexRouting;
return this;
}

/**
* Returns the search routing value associated with the alias
*/
public String searchRouting() {
return searchRouting;
}

/**
* Associates a search routing value to the alias
*/
public Alias searchRouting(String searchRouting) {
this.searchRouting = searchRouting;
return this;
}

/**
* Allows to read an alias from the provided input stream
*/
public static Alias read(StreamInput in) throws IOException {
Alias alias = new Alias();
alias.readFrom(in);
return alias;
}

@Override
public void readFrom(StreamInput in) throws IOException {
name = in.readString();
filter = in.readOptionalString();
indexRouting = in.readOptionalString();
searchRouting = in.readOptionalString();
}

@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeString(name);
out.writeOptionalString(filter);
out.writeOptionalString(indexRouting);
out.writeOptionalString(searchRouting);
}

/**
* Parses an alias and returns its parsed representation
*/
public static Alias fromXContent(XContentParser parser) throws IOException {
Alias alias = new Alias(parser.currentName());

String currentFieldName = null;
XContentParser.Token token = parser.nextToken();
if (token == null) {
throw new ElasticsearchIllegalArgumentException("No alias is specified");
}
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) {
currentFieldName = parser.currentName();
} else if (token == XContentParser.Token.START_OBJECT) {
if ("filter".equals(currentFieldName)) {
Map<String, Object> filter = parser.mapOrdered();
alias.filter(filter);
}
} else if (token == XContentParser.Token.VALUE_STRING) {
if ("routing".equals(currentFieldName)) {
alias.routing(parser.text());
} else if ("index_routing".equals(currentFieldName) || "indexRouting".equals(currentFieldName) || "index-routing".equals(currentFieldName)) {
alias.indexRouting(parser.text());
} else if ("search_routing".equals(currentFieldName) || "searchRouting".equals(currentFieldName) || "search-routing".equals(currentFieldName)) {
alias.searchRouting(parser.text());
}
}
}
return alias;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

Alias alias = (Alias) o;

if (name != null ? !name.equals(alias.name) : alias.name != null) return false;

return true;
}

@Override
public int hashCode() {
return name != null ? name.hashCode() : 0;
}
}

0 comments on commit 70a38d5

Please sign in to comment.