Skip to content
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 @@ -19,14 +19,18 @@

package org.elasticsearch.index.reindex;

import org.elasticsearch.Version;
import org.elasticsearch.action.admin.indices.alias.Alias;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.index.IndexNotFoundException;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.search.sort.SortOrder;
import org.elasticsearch.test.InternalSettingsPlugin;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import static org.elasticsearch.cluster.metadata.IndexMetaData.SETTING_READ_ONLY;
Expand All @@ -39,6 +43,12 @@
import static org.hamcrest.Matchers.hasSize;

public class DeleteByQueryBasicTests extends ReindexTestCase {
@Override
protected Collection<Class<? extends Plugin>> nodePlugins() {
List<Class<? extends Plugin>> plugins = new ArrayList<>(super.nodePlugins());
plugins.add(InternalSettingsPlugin.class);
return plugins;
}

public void testBasics() throws Exception {
indexRandom(true,
Expand Down Expand Up @@ -237,4 +247,26 @@ public void testWorkers() throws Exception {
assertThat(request.get(), matcher().deleted(5).slices(hasSize(5)));
assertHitCount(client().prepareSearch("test").setTypes("test").setSize(0).get(), 0);
}

/**
* Test delete by query support for filtering by type. This entire feature
* can and should be removed when we drop support for types index with
* multiple types from core.
*/
public void testFilterByType() throws Exception {
assertAcked(client().admin().indices().prepareCreate("test")
.setSettings("index.version.created", Version.V_5_6_0.id)); // allows for multiple types
indexRandom(true,
client().prepareIndex("test", "test1", "1").setSource("foo", "a"),
client().prepareIndex("test", "test2", "2").setSource("foo", "a"),
client().prepareIndex("test", "test2", "3").setSource("foo", "b"));

assertHitCount(client().prepareSearch("test").setSize(0).get(), 3);

// Deletes doc of the type "type2" that also matches foo:a
DeleteByQueryRequestBuilder builder = deleteByQuery().source("test").filter(termQuery("foo", "a")).refresh(true);
builder.source().setTypes("test2");
assertThat(builder.get(), matcher().deleted(1));
assertHitCount(client().prepareSearch("test").setSize(0).get(), 2);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,19 @@
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.join.ParentJoinPlugin;
import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.script.MockScriptPlugin;
import org.elasticsearch.test.InternalSettingsPlugin;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

import static org.elasticsearch.index.query.QueryBuilders.idsQuery;
import static org.elasticsearch.index.query.QueryBuilders.typeQuery;
import static org.elasticsearch.join.query.JoinQueryBuilders.hasParentQuery;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchHits;
Expand All @@ -42,7 +47,8 @@
import static org.hamcrest.Matchers.instanceOf;

/**
* Index-by-search tests for parent/child.
* Reindex tests for legacy parent/child. Tests for the new {@code join}
* field are in a qa project.
*/
public class ReindexParentChildTests extends ReindexTestCase {
QueryBuilder findsCountry;
Expand All @@ -59,6 +65,7 @@ protected Collection<Class<? extends Plugin>> nodePlugins() {
final List<Class<? extends Plugin>> plugins = new ArrayList<>(super.nodePlugins());
plugins.add(ParentJoinPlugin.class);
plugins.add(InternalSettingsPlugin.class);
plugins.add(CustomScriptPlugin.class);
return Collections.unmodifiableList(plugins);
}

Expand All @@ -70,7 +77,7 @@ protected Collection<Class<? extends Plugin>> transportClientPlugins() {
public void testParentChild() throws Exception {
createParentChildIndex("source");
createParentChildIndex("dest");
createParentChildDocs("source");
createParentChildDocs("source", true);

// Copy parent to the new index
ReindexRequestBuilder copy = reindex().source("source").destination("dest").filter(findsCountry).refresh(true);
Expand Down Expand Up @@ -101,9 +108,32 @@ public void testParentChild() throws Exception {
"make-believe");
}

/**
* Tests for adding the {@code _parent} via script and adding *both* {@code _parent} and {@code _routing} values via scripts.
*/
public void testScriptAddsParent() throws Exception {
assertAcked(client().admin().indices().prepareCreate("source")
.setSettings("index.version.created", Version.V_5_6_0.id)); // allows for multiple types

createParentChildIndex("dest");
createParentChildDocs("source", false);

ReindexRequestBuilder copy = reindex().source("source").destination("dest").filter(typeQuery("country")).refresh(true);
assertThat(copy.get(), matcher().created(1));
copy = reindex().source("source").destination("dest").filter(typeQuery("city"))
.script(mockScript("ctx._parent='united states'")).refresh(true);
assertThat(copy.get(), matcher().created(1));
assertSearchHits(client().prepareSearch("dest").setQuery(findsCity).get(), "pittsburgh");

copy = reindex().source("source").destination("dest").filter(typeQuery("neighborhood"))
.script(mockScript("ctx._parent='pittsburgh';ctx._routing='united states'")).refresh(true);
assertThat(copy.get(), matcher().created(1));
assertSearchHits(client().prepareSearch("dest").setQuery(findsNeighborhood).get(), "make-believe");
}

public void testErrorMessageWhenBadParentChild() throws Exception {
createParentChildIndex("source");
createParentChildDocs("source");
createParentChildDocs("source", true);

ReindexRequestBuilder copy = reindex().source("source").destination("dest").filter(findsCity);
final BulkByScrollResponse response = copy.get();
Expand All @@ -119,25 +149,55 @@ public void testErrorMessageWhenBadParentChild() throws Exception {
*/
private void createParentChildIndex(String indexName) throws Exception {
CreateIndexRequestBuilder create = client().admin().indices().prepareCreate(indexName);
create.setSettings("index.version.created", Version.V_5_6_0.id);
create.setSettings("index.version.created", Version.V_5_6_0.id); // allows for multiple types
create.addMapping("city", "{\"_parent\": {\"type\": \"country\"}}", XContentType.JSON);
create.addMapping("neighborhood", "{\"_parent\": {\"type\": \"city\"}}", XContentType.JSON);
assertAcked(create);
ensureGreen();
}

private void createParentChildDocs(String indexName) throws Exception {
indexRandom(true, client().prepareIndex(indexName, "country", "united states").setSource("foo", "bar"),
client().prepareIndex(indexName, "city", "pittsburgh").setParent("united states").setSource("foo", "bar"),
client().prepareIndex(indexName, "neighborhood", "make-believe").setParent("pittsburgh")
.setSource("foo", "bar").setRouting("united states"));
private void createParentChildDocs(String indexName, boolean addParents) throws Exception {
indexRandom(true,
client().prepareIndex(indexName, "country", "united states")
.setSource("foo", "bar"),
client().prepareIndex(indexName, "city", "pittsburgh")
.setParent(addParents ? "united states" : null)
.setSource("foo", "bar"),
client().prepareIndex(indexName, "neighborhood", "make-believe")
.setParent(addParents ? "pittsburgh" : null)
.setRouting(addParents ? "united states" : null)
.setSource("foo", "bar"));

findsCountry = idsQuery("country").addIds("united states");
findsCity = hasParentQuery("country", findsCountry, false);
findsNeighborhood = hasParentQuery("city", findsCity, false);

// Make sure we built the parent/child relationship
assertSearchHits(client().prepareSearch(indexName).setQuery(findsCity).get(), "pittsburgh");
assertSearchHits(client().prepareSearch(indexName).setQuery(findsNeighborhood).get(), "make-believe");
if (addParents) {
// Make sure we built the parent/child relationship
assertSearchHits(client().prepareSearch(indexName).setQuery(findsCity).get(), "pittsburgh");
assertSearchHits(client().prepareSearch(indexName).setQuery(findsNeighborhood).get(), "make-believe");
}
}

public static class CustomScriptPlugin extends MockScriptPlugin {
@Override
@SuppressWarnings("unchecked")
protected Map<String, Function<Map<String, Object>, Object>> pluginScripts() {
Map<String, Function<Map<String, Object>, Object>> scripts = new HashMap<>();

scripts.put("ctx._parent='united states'", vars -> {
Map<String, String> ctx = (Map<String, String>) vars.get("ctx");
ctx.put("_parent", "united states");
return null;
});
scripts.put("ctx._parent='pittsburgh';ctx._routing='united states'", vars -> {
Map<String, String> ctx = (Map<String, String>) vars.get("ctx");
ctx.put("_parent", "pittsburgh");
ctx.put("_routing", "united states");
return null;
});

return scripts;
}
}
}

This file was deleted.

Loading