use of com.google.appengine.api.search.GetRequest in project java-docs-samples by GoogleCloudPlatform.
the class DeleteServlet method doGet.
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
// Put one document to avoid an error
Document document = Document.newBuilder().addField(Field.newBuilder().setName("f").setText("v")).build();
try {
Utils.indexADocument(SEARCH_INDEX, document);
} catch (InterruptedException e) {
// ignore
}
// [START delete_documents]
try {
// looping because getRange by default returns up to 100 documents at a time
while (true) {
List<String> docIds = new ArrayList<>();
// Return a set of doc_ids.
GetRequest request = GetRequest.newBuilder().setReturningIdsOnly(true).build();
GetResponse<Document> response = getIndex().getRange(request);
if (response.getResults().isEmpty()) {
break;
}
for (Document doc : response) {
docIds.add(doc.getId());
}
getIndex().delete(docIds);
}
} catch (RuntimeException e) {
LOG.log(Level.SEVERE, "Failed to delete documents", e);
}
// [END delete_documents]
PrintWriter out = resp.getWriter();
out.println("Deleted documents.");
}
Aggregations