use of com.google.appengine.api.search.Index in project teammates by TEAMMATES.
the class SearchManager method getIndex.
private static Index getIndex(String indexName) {
Map<String, Index> indicesTable = getIndicesTable();
Index index = indicesTable.get(indexName);
if (index == null) {
IndexSpec indexSpec = IndexSpec.newBuilder().setName(indexName).build();
index = SearchServiceFactory.getSearchService().getIndex(indexSpec);
indicesTable.put(indexName, index);
}
return index;
}
use of com.google.appengine.api.search.Index in project teammates by TEAMMATES.
the class SearchManager method putDocumentWithRetry.
/**
* Tries putting a document, handling transient errors by retrying with exponential backoff.
*
* @throws PutException if a non-transient error is encountered.
* @throws MaximumRetriesExceededException with final {@link OperationResult}'s message as final message,
* if operation fails after maximum retries.
*/
private static void putDocumentWithRetry(String indexName, final Document document) throws PutException, MaximumRetriesExceededException {
final Index index = getIndex(indexName);
/*
* The GAE Search API signals put document failure in two ways: it either
* returns a PutResponse containing an OperationResult with a non-OK StatusCode, or
* throws a PutException that also contains an embedded OperationResult.
* We handle both ways by examining the OperationResult to determine what kind of error it is. If it is
* transient, we use RetryManager to retry the operation; if it is
* non-transient, we do not retry but throw a PutException upwards instead.
*/
RM.runUntilSuccessful(new RetryableTaskThrows<PutException>("Put document") {
private OperationResult lastResult;
@Override
public void run() {
try {
PutResponse response = index.put(document);
lastResult = response.getResults().get(0);
} catch (PutException e) {
lastResult = e.getOperationResult();
}
}
@Override
public boolean isSuccessful() throws PutException {
// Update the final message to be shown if the task fails after maximum retries
finalMessage = lastResult.getMessage();
if (StatusCode.OK.equals(lastResult.getCode())) {
return true;
} else if (StatusCode.TRANSIENT_ERROR.equals(lastResult.getCode())) {
// A transient error can be retried
return false;
} else {
// A non-transient error signals that the operation should not be retried
throw new PutException(lastResult);
}
}
});
}
use of com.google.appengine.api.search.Index in project java-docs-samples by GoogleCloudPlatform.
the class SearchOptionServlet method doSearch.
private Results<ScoredDocument> doSearch() {
String indexName = SEARCH_INDEX;
// [START search_with_options]
try {
// Build the SortOptions with 2 sort keys
SortOptions sortOptions = SortOptions.newBuilder().addSortExpression(SortExpression.newBuilder().setExpression("price").setDirection(SortExpression.SortDirection.DESCENDING).setDefaultValueNumeric(0)).addSortExpression(SortExpression.newBuilder().setExpression("brand").setDirection(SortExpression.SortDirection.DESCENDING).setDefaultValue("")).setLimit(1000).build();
// Build the QueryOptions
QueryOptions options = QueryOptions.newBuilder().setLimit(25).setFieldsToReturn("model", "price", "description").setSortOptions(sortOptions).build();
// A query string
String queryString = "product: coffee roaster AND price < 500";
// Build the Query and run the search
Query query = Query.newBuilder().setOptions(options).build(queryString);
IndexSpec indexSpec = IndexSpec.newBuilder().setName(indexName).build();
Index index = SearchServiceFactory.getSearchService().getIndex(indexSpec);
Results<ScoredDocument> result = index.search(query);
return result;
} catch (SearchException e) {
// handle exception...
}
// [END search_with_options]
return null;
}
use of com.google.appengine.api.search.Index 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.Index in project java-docs-samples by GoogleCloudPlatform.
the class Utils method indexADocument.
/**
* Put a given document into an index with the given indexName.
* @param indexName The name of the index.
* @param document A document to add.
* @throws InterruptedException When Thread.sleep is interrupted.
*/
// [START putting_document_with_retry]
public static void indexADocument(String indexName, Document document) throws InterruptedException {
IndexSpec indexSpec = IndexSpec.newBuilder().setName(indexName).build();
Index index = SearchServiceFactory.getSearchService().getIndex(indexSpec);
final int maxRetry = 3;
int attempts = 0;
int delay = 2;
while (true) {
try {
index.put(document);
} catch (PutException e) {
if (StatusCode.TRANSIENT_ERROR.equals(e.getOperationResult().getCode()) && ++attempts < maxRetry) {
// retrying
Thread.sleep(delay * 1000);
// easy exponential backoff
delay *= 2;
continue;
} else {
// otherwise throw
throw e;
}
}
break;
}
}
Aggregations