use of com.google.appengine.api.search.Document in project java-docs-samples by GoogleCloudPlatform.
the class DocumentServlet method createDocument.
/**
* Code snippet for creating a Document.
* @return Document Created document.
*/
public Document createDocument() {
// [START create_document]
User currentUser = UserServiceFactory.getUserService().getCurrentUser();
String userEmail = currentUser == null ? "" : currentUser.getEmail();
String userDomain = currentUser == null ? "" : currentUser.getAuthDomain();
String myDocId = "PA6-5000";
Document doc = Document.newBuilder().setId(myDocId).addField(Field.newBuilder().setName("content").setText("the rain in spain")).addField(Field.newBuilder().setName("email").setText(userEmail)).addField(Field.newBuilder().setName("domain").setAtom(userDomain)).addField(Field.newBuilder().setName("published").setDate(new Date())).build();
// [END create_document]
return doc;
}
use of com.google.appengine.api.search.Document in project java-docs-samples by GoogleCloudPlatform.
the class SchemaServlet method doGet.
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
PrintWriter out = resp.getWriter();
Document doc = Document.newBuilder().setId("theOnlyCar").addField(Field.newBuilder().setName("maker").setText("Toyota")).addField(Field.newBuilder().setName("price").setNumber(300000)).addField(Field.newBuilder().setName("color").setText("lightblue")).addField(Field.newBuilder().setName("model").setText("Prius")).build();
try {
Utils.indexADocument(SEARCH_INDEX, doc);
} catch (InterruptedException e) {
// ignore
}
// [START list_schema]
GetResponse<Index> response = SearchServiceFactory.getSearchService().getIndexes(GetIndexesRequest.newBuilder().setSchemaFetched(true).build());
// List out elements of each Schema
for (Index index : response) {
Schema schema = index.getSchema();
for (String fieldName : schema.getFieldNames()) {
List<FieldType> typesForField = schema.getFieldTypes(fieldName);
// Just printing out the field names and types
for (FieldType type : typesForField) {
out.println(index.getName() + ":" + fieldName + ":" + type.name());
}
}
}
// [END list_schema]
}
use of com.google.appengine.api.search.Document in project java-docs-samples by GoogleCloudPlatform.
the class SearchServlet method doGet.
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
PrintWriter out = resp.getWriter();
Document doc = Document.newBuilder().setId("theOnlyPiano").addField(Field.newBuilder().setName("product").setText("piano")).addField(Field.newBuilder().setName("maker").setText("Yamaha")).addField(Field.newBuilder().setName("price").setNumber(4000)).build();
try {
Utils.indexADocument(SEARCH_INDEX, doc);
} catch (InterruptedException e) {
// ignore
}
// [START search_document]
final int maxRetry = 3;
int attempts = 0;
int delay = 2;
while (true) {
try {
String queryString = "product = piano AND price < 5000";
Results<ScoredDocument> results = getIndex().search(queryString);
// Iterate over the documents in the results
for (ScoredDocument document : results) {
// handle results
out.print("maker: " + document.getOnlyField("maker").getText());
out.println(", price: " + document.getOnlyField("price").getNumber());
}
} catch (SearchException e) {
if (StatusCode.TRANSIENT_ERROR.equals(e.getOperationResult().getCode()) && ++attempts < maxRetry) {
// retry
try {
Thread.sleep(delay * 1000);
} catch (InterruptedException e1) {
// ignore
}
// easy exponential backoff
delay *= 2;
continue;
} else {
throw e;
}
}
break;
}
// [END search_document]
// We don't test the search result below, but we're fine if it runs without errors.
out.println("Search performed");
Index index = getIndex();
// [START simple_search_1]
index.search("rose water");
// [END simple_search_1]
// [START simple_search_2]
index.search("1776-07-04");
// [END simple_search_2]
// [START simple_search_3]
// search for documents with pianos that cost less than $5000
index.search("product = piano AND price < 5000");
// [END simple_search_3]
}
use of com.google.appengine.api.search.Document in project java-docs-samples by GoogleCloudPlatform.
the class DocumentServletTest method createDocument_withSignedInUser.
@Test
public void createDocument_withSignedInUser() throws Exception {
String email = "tmatsuo@example.com";
String authDomain = "example.com";
helper.setEnvEmail(email);
helper.setEnvAuthDomain(authDomain);
helper.setEnvIsLoggedIn(true);
Document doc = servletUnderTest.createDocument();
assertThat(doc.getOnlyField("content").getText()).named("content").contains("the rain in spain");
assertThat(doc.getOnlyField("email").getText()).named("email").isEqualTo(email);
}
use of com.google.appengine.api.search.Document in project blogwt by billy1380.
the class UserService method toDocument.
/* (non-Javadoc)
*
* @see
* com.willshex.blogwt.server.service.search.IIndex#toDocument(java.lang.
* Object) */
@Override
public Document toDocument(User user) {
Document document = null;
if (user != null) {
if (user.roleKeys != null) {
user.roles = PersistenceHelper.batchLookupKeys(RoleServiceProvider.provide(), user.roleKeys);
}
if (user.permissionKeys != null) {
user.permissions = PersistenceHelper.batchLookupKeys(PermissionServiceProvider.provide(), user.permissionKeys);
}
Document.Builder documentBuilder = Document.newBuilder();
documentBuilder.setId(getName() + user.id.toString()).addField(Field.newBuilder().setName("username").setAtom(user.username)).addField(Field.newBuilder().setName("name").setText(UserHelper.name(user))).addField(Field.newBuilder().setName("forename").setText(user.forename)).addField(Field.newBuilder().setName("surname").setText(user.surname)).addField(Field.newBuilder().setName("email").setText(user.email)).addField(Field.newBuilder().setName("created").setDate(user.created)).addField(Field.newBuilder().setName("group").setText(user.group)).addField(Field.newBuilder().setName("summary").setText(user.summary));
if (user.roles != null) {
for (Role role : user.roles) {
documentBuilder.addField(Field.newBuilder().setName("role").setText(role.name));
}
}
if (user.permissions != null) {
for (Permission permission : user.permissions) {
documentBuilder.addField(Field.newBuilder().setName("permission").setText(permission.name));
}
}
document = documentBuilder.build();
}
return document;
}
Aggregations