use of com.google.appengine.api.search.Document 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.");
}
use of com.google.appengine.api.search.Document in project java-docs-samples by GoogleCloudPlatform.
the class IndexServlet method doGet.
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
PrintWriter out = resp.getWriter();
Document document = Document.newBuilder().setId("AZ125").addField(Field.newBuilder().setName("myField").setText("myValue")).build();
try {
Utils.indexADocument(INDEX, document);
} catch (InterruptedException e) {
out.println("Interrupted");
return;
}
out.println("Indexed a new document.");
// [START get_document]
IndexSpec indexSpec = IndexSpec.newBuilder().setName(INDEX).build();
Index index = SearchServiceFactory.getSearchService().getIndex(indexSpec);
// Fetch a single document by its doc_id
Document doc = index.get("AZ125");
// Fetch a range of documents by their doc_ids
GetResponse<Document> docs = index.getRange(GetRequest.newBuilder().setStartId("AZ125").setLimit(100).build());
// [END get_document]
out.println("myField: " + docs.getResults().get(0).getOnlyField("myField").getText());
}
use of com.google.appengine.api.search.Document in project java-docs-samples by GoogleCloudPlatform.
the class SearchOptionServlet method doGet.
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
// Put one document to avoid an error
Document document = Document.newBuilder().setId("theOnlyCoffeeRoaster").addField(Field.newBuilder().setName("price").setNumber(200)).addField(Field.newBuilder().setName("model").setText("TZ4000")).addField(Field.newBuilder().setName("brand").setText("MyBrand")).addField(Field.newBuilder().setName("product").setText("coffee roaster")).addField(Field.newBuilder().setName("description").setText("A coffee bean roaster at home")).build();
try {
Utils.indexADocument(SEARCH_INDEX, document);
} catch (InterruptedException e) {
// ignore
}
PrintWriter out = resp.getWriter();
Results<ScoredDocument> result = doSearch();
for (ScoredDocument doc : result.getResults()) {
out.println(doc.toString());
}
}
use of com.google.appengine.api.search.Document in project java-docs-samples by GoogleCloudPlatform.
the class DocumentServletTest method createDocument_withoutSignedIn.
@Test
public void createDocument_withoutSignedIn() throws Exception {
helper.setEnvIsLoggedIn(false);
Document doc = servletUnderTest.createDocument();
assertThat(doc.getOnlyField("content").getText()).named("content").contains("the rain in spain");
assertThat(doc.getOnlyField("email").getText()).named("email").isEmpty();
}
use of com.google.appengine.api.search.Document in project java-docs-samples by GoogleCloudPlatform.
the class UtilsTest method indexADocument_successfullyInvoked.
@Test
public void indexADocument_successfullyInvoked() throws Exception {
String id = "test";
Document doc = Document.newBuilder().setId(id).addField(Field.newBuilder().setName("f").setText("v")).build();
Utils.indexADocument(INDEX, doc);
// get the document by id
IndexSpec indexSpec = IndexSpec.newBuilder().setName(INDEX).build();
Index index = SearchServiceFactory.getSearchService().getIndex(indexSpec);
Document fetched = index.get(id);
assertThat(fetched.getOnlyField("f").getText()).named("A value of the fetched document").isEqualTo("v");
}
Aggregations