Created
October 9, 2013 22:24
-
-
Save s1monw/6909562 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.google.common.util.concurrent.Futures; | |
import org.apache.lucene.queryparser.classic.QueryParser; | |
import org.elasticsearch.action.search.SearchResponse; | |
import org.elasticsearch.action.search.SearchType; | |
import org.elasticsearch.client.Client; | |
import org.elasticsearch.common.unit.TimeValue; | |
import org.elasticsearch.index.query.QueryBuilders; | |
import org.elasticsearch.search.SearchHit; | |
import org.elasticsearch.test.AbstractIntegrationTest; | |
import java.util.concurrent.TimeUnit; | |
/** | |
* This main class shows an issue I have found while scrolling through results. If one tries to delete a document then | |
* it actually deletes everything. | |
* <br/> | |
* See <a href="https://github.com/elasticsearch/elasticsearch/issues/3832">issue elasticsearch#3382</a> | |
* | |
* @author amir.raminfar | |
*/ | |
public class TestExampleIssue3832 extends AbstractIntegrationTest { | |
private static int TOTAL = 1000; | |
private static String INDEX = "test"; | |
private static String TYPE = "document"; | |
private static TimeValue FIVE_MINS = new TimeValue(5, TimeUnit.MINUTES); | |
private static void createSomeObjects(Client client) { | |
for (int i = 0; i < TOTAL; i++) { | |
Futures.getUnchecked(client.prepareIndex(INDEX, TYPE).setSource("data", "foo_" + i).execute()); | |
} | |
// Force a refresh | |
client.admin().indices().prepareRefresh().execute().actionGet(); | |
} | |
public void test() { | |
// Setup client using transport client. In production I use node but this issue occurred with transport client. | |
Client client = client(); | |
// Let's create some test objects | |
createSomeObjects(client); | |
assertEquals(TOTAL, client.prepareCount(INDEX).get().getCount()); | |
SearchResponse searchResponse = client.prepareSearch() | |
.setSize(2000) | |
.setSearchType(SearchType.SCAN) | |
.setScroll(FIVE_MINS) | |
.execute() | |
.actionGet(); | |
int found = 0; | |
int deleted = 0; | |
while (true) { | |
searchResponse = client.prepareSearchScroll(searchResponse.getScrollId()) | |
.setScroll(FIVE_MINS) | |
.execute() | |
.actionGet(); | |
for (SearchHit hit : searchResponse.getHits()) { | |
if (rarely()) { // Let's simulate a random deletion | |
System.out.println(hit.id()); | |
Futures.getUnchecked(client.prepareDeleteByQuery() | |
.setQuery(QueryBuilders.fieldQuery("_id", QueryParser.escape(hit.id()))).execute()); | |
// todo Using term query works every time | |
// Futures.getUnchecked(client.prepareDeleteByQuery() | |
// .setQuery(QueryBuilders.termQuery("_id", hit.id())).execute()); | |
deleted++; | |
} | |
found++; | |
} | |
if (searchResponse.getHits().getHits().length == 0) { | |
break; | |
} | |
} | |
// Force refresh before looking for counts | |
client.admin().indices().prepareRefresh().execute().actionGet(); | |
assertEquals(TOTAL, found); | |
// This will fail java.lang.AssertionError: expected:<99000> but was:<0> | |
assertEquals(TOTAL - deleted, client.prepareCount(INDEX).get().getCount()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment