Skip to content
This repository has been archived by the owner on Jun 20, 2023. It is now read-only.

Issue #127, JSON in alias filter is unnecessarily escaped. Changed Strin... #128

Merged
merged 2 commits into from
Jun 17, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
* @author cihat keser
*/
public abstract class AbstractAliasMappingBuilder<T extends AliasMapping, K> {
protected String filter;
protected Map<String, Object> filter;
protected String alias;
protected List<String> indices = new LinkedList<String>();
protected List<String> searchRouting = new LinkedList<String>();
Expand Down Expand Up @@ -39,7 +40,7 @@ public K addIndex(Collection<String> indices) {
* The filter can be defined using Query DSL and is applied to all Search, Count,
* Delete By Query and More Like This operations with this alias.
*/
public K setFilter(String source) {
public K setFilter(Map<String, Object> source) {
this.filter = source;
return (K) this;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.searchbox.indices.aliases;

import java.util.LinkedList;
import java.util.List;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public abstract class AliasMapping {

protected List<String> indices = new LinkedList<String>();
protected String alias;
protected String filter;
protected Map<String, Object> filter;
protected List<String> searchRouting = new LinkedList<String>();
protected List<String> indexRouting = new LinkedList<String>();

Expand All @@ -28,7 +28,7 @@ public List<Map<String, Object>> getData() {
paramsMap.put("index", index);
paramsMap.put("alias", alias);

if (StringUtils.isNotEmpty(filter)) {
if (filter != null) {
paramsMap.put("filter", filter);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
package io.searchbox.indices.aliases;

import com.google.gson.Gson;
import org.elasticsearch.common.collect.ImmutableMap;
import org.junit.Test;

import java.util.Map;

import static org.junit.Assert.assertEquals;

/**
* @author cihat keser
*/
public class AddAliasMappingTest {

public static final Map<String, Object> USER_FILTER_JSON = ImmutableMap.<String, Object>builder()
.put("term", ImmutableMap.<String, String>builder()
.put("user", "kimchy")
.build())
.build();

@Test
public void testBasicGetDataForJson() {
AddAliasMapping addAliasMapping = new AddAliasMapping
Expand All @@ -25,10 +34,10 @@ public void testBasicGetDataForJson() {
public void testGetDataForJsonWithFilter() {
AddAliasMapping addAliasMapping = new AddAliasMapping
.Builder("tIndex", "tAlias")
.setFilter("my_query")
.setFilter(USER_FILTER_JSON)
.build();
String actualJson = new Gson().toJson(addAliasMapping.getData()).toString();
String expectedJson = "[{\"add\":{\"index\":\"tIndex\",\"alias\":\"tAlias\",\"filter\":\"my_query\"}}]";
String expectedJson = "[{\"add\":{\"index\":\"tIndex\",\"alias\":\"tAlias\",\"filter\":{\"term\":{\"user\":\"kimchy\"}}}}]";

assertEquals(expectedJson, actualJson);
}
Expand All @@ -37,11 +46,12 @@ public void testGetDataForJsonWithFilter() {
public void testGetDataForJsonWithFilterAndRouting() {
AddAliasMapping addAliasMapping = new AddAliasMapping
.Builder("tIndex", "tAlias")
.setFilter("my_query")
.setFilter(USER_FILTER_JSON)
.addRouting("1")
.build();
String actualJson = new Gson().toJson(addAliasMapping.getData()).toString();
String expectedJson = "[{\"add\":{\"search_routing\":\"1\",\"index\":\"tIndex\",\"alias\":\"tAlias\",\"index_routing\":\"1\",\"filter\":\"my_query\"}}]";
String expectedJson = "[{\"add\":{\"search_routing\":\"1\",\"index\":\"tIndex\",\"alias\":\"tAlias\"," +
"\"index_routing\":\"1\",\"filter\":{\"term\":{\"user\":\"kimchy\"}}}}]";

assertEquals(expectedJson, actualJson);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package io.searchbox.indices.aliases;

import com.google.gson.Gson;
import org.elasticsearch.common.collect.ImmutableMap;
import org.junit.Test;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import static org.junit.Assert.assertEquals;

Expand All @@ -13,15 +15,21 @@
*/
public class ModifyAliasesTest {

public static final Map<String, Object> USER_FILTER_JSON = ImmutableMap.<String, Object>builder()
.put("term", ImmutableMap.<String, String>builder()
.put("user", "kimchy")
.build())
.build();

@Test
public void testBasicGetData() {
List<AliasMapping> aliasMappings = new LinkedList<AliasMapping>();
aliasMappings.add(new AddAliasMapping.Builder("t_add_index", "t_add_alias").setFilter("t_a_query").build());
aliasMappings.add(new AddAliasMapping.Builder("t_add_index", "t_add_alias").setFilter(USER_FILTER_JSON).build());
aliasMappings.add(new RemoveAliasMapping.Builder("t_remove_index", "t_remove_alias").addRouting("1").build());
ModifyAliases modifyAliases = new ModifyAliases.Builder(aliasMappings).build();
assertEquals("{" +
"\"data\":{\"actions\":[" +
"{\"add\":{\"index\":\"t_add_index\",\"alias\":\"t_add_alias\",\"filter\":\"t_a_query\"}}," +
"{\"add\":{\"index\":\"t_add_index\",\"alias\":\"t_add_alias\",\"filter\":{\"term\":{\"user\":\"kimchy\"}}}}," +
"{\"remove\":{\"search_routing\":\"1\",\"index\":\"t_remove_index\",\"alias\":\"t_remove_alias\",\"index_routing\":\"1\"}}]}," +
"\"headerMap\":{}," +
"\"parameterMap\":{}," +
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
package io.searchbox.indices.aliases;

import com.google.gson.Gson;
import org.elasticsearch.common.collect.ImmutableMap;
import org.junit.Test;

import java.util.Map;

import static org.junit.Assert.assertEquals;

/**
* @author cihat keser
*/
public class RemoveAliasMappingTest {

public static final Map<String, Object> USER_FILTER_JSON = ImmutableMap.<String, Object>builder()
.put("term", ImmutableMap.<String, String>builder()
.put("user", "kimchy")
.build())
.build();

@Test
public void testBasicGetDataForJson() {
RemoveAliasMapping addAliasMapping = new RemoveAliasMapping
Expand All @@ -25,10 +34,10 @@ public void testBasicGetDataForJson() {
public void testGetDataForJsonWithFilter() {
RemoveAliasMapping addAliasMapping = new RemoveAliasMapping
.Builder("tIndex", "tAlias")
.setFilter("my_query")
.setFilter(USER_FILTER_JSON)
.build();
String actualJson = new Gson().toJson(addAliasMapping.getData()).toString();
String expectedJson = "[{\"remove\":{\"index\":\"tIndex\",\"alias\":\"tAlias\",\"filter\":\"my_query\"}}]";
String expectedJson = "[{\"remove\":{\"index\":\"tIndex\",\"alias\":\"tAlias\",\"filter\":{\"term\":{\"user\":\"kimchy\"}}}}]";

assertEquals(expectedJson, actualJson);
}
Expand All @@ -37,11 +46,11 @@ public void testGetDataForJsonWithFilter() {
public void testGetDataForJsonWithFilterAndRouting() {
RemoveAliasMapping addAliasMapping = new RemoveAliasMapping
.Builder("tIndex", "tAlias")
.setFilter("my_query")
.setFilter(USER_FILTER_JSON)
.addRouting("1")
.build();
String actualJson = new Gson().toJson(addAliasMapping.getData()).toString();
String expectedJson = "[{\"remove\":{\"search_routing\":\"1\",\"index\":\"tIndex\",\"alias\":\"tAlias\",\"index_routing\":\"1\",\"filter\":\"my_query\"}}]";
String expectedJson = "[{\"remove\":{\"search_routing\":\"1\",\"index\":\"tIndex\",\"alias\":\"tAlias\",\"index_routing\":\"1\",\"filter\":{\"term\":{\"user\":\"kimchy\"}}}}]";

assertEquals(expectedJson, actualJson);
}
Expand Down