Skip to content

Instantly share code, notes, and snippets.

@amir20
Last active December 24, 2015 20:29
Show Gist options
  • Save amir20/6858428 to your computer and use it in GitHub Desktop.
Save amir20/6858428 to your computer and use it in GitHub Desktop.
Shows issues 3832
import com.google.common.util.concurrent.Futures;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import java.util.concurrent.TimeUnit;
import static org.junit.Assert.assertEquals;
/**
* 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 {
private static int TOTAL = 100_000;
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 static void main(String... args) {
// Setup client using transport client. In production I use node but this issue occurred with transport client.
Settings settings = ImmutableSettings.settingsBuilder().put("cluster.name", "elasticsearch_amirraminfar").build();
Client client = new TransportClient(settings).addTransportAddress(new InetSocketTransportAddress("localhost", 9300));
// Drop existing index
Futures.getUnchecked(client.admin().indices().prepareDelete(INDEX).execute());
// 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 (found % 100 == 0) { // Let's simulate a random deletion
Futures.getUnchecked(client.prepareDeleteByQuery()
.setQuery(QueryBuilders.fieldQuery("_id", 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());
}
}
@amir20
Copy link
Author

amir20 commented Oct 6, 2013

Installed elasticsearch with brew install elasticsearch

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment