Search in sources :

Example 1 with GetRequest

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.");
}
Also used : GetRequest(com.google.appengine.api.search.GetRequest) ArrayList(java.util.ArrayList) Document(com.google.appengine.api.search.Document) PrintWriter(java.io.PrintWriter)

Aggregations

Document (com.google.appengine.api.search.Document)1 GetRequest (com.google.appengine.api.search.GetRequest)1 PrintWriter (java.io.PrintWriter)1 ArrayList (java.util.ArrayList)1