Search in sources :

Example 6 with Index

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;
}
Also used : IndexSpec(com.google.appengine.api.search.IndexSpec) Index(com.google.appengine.api.search.Index)

Example 7 with 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);
            }
        }
    });
}
Also used : PutException(com.google.appengine.api.search.PutException) Index(com.google.appengine.api.search.Index) OperationResult(com.google.appengine.api.search.OperationResult) PutResponse(com.google.appengine.api.search.PutResponse)

Example 8 with Index

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;
}
Also used : IndexSpec(com.google.appengine.api.search.IndexSpec) ScoredDocument(com.google.appengine.api.search.ScoredDocument) Query(com.google.appengine.api.search.Query) SearchException(com.google.appengine.api.search.SearchException) Index(com.google.appengine.api.search.Index) QueryOptions(com.google.appengine.api.search.QueryOptions) SortOptions(com.google.appengine.api.search.SortOptions)

Example 9 with Index

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]
}
Also used : Schema(com.google.appengine.api.search.Schema) Index(com.google.appengine.api.search.Index) Document(com.google.appengine.api.search.Document) PrintWriter(java.io.PrintWriter) FieldType(com.google.appengine.api.search.Field.FieldType)

Example 10 with Index

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;
    }
}
Also used : IndexSpec(com.google.appengine.api.search.IndexSpec) PutException(com.google.appengine.api.search.PutException) Index(com.google.appengine.api.search.Index)

Aggregations

Index (com.google.appengine.api.search.Index)13 IndexSpec (com.google.appengine.api.search.IndexSpec)9 Document (com.google.appengine.api.search.Document)5 PrintWriter (java.io.PrintWriter)4 PutException (com.google.appengine.api.search.PutException)3 ScoredDocument (com.google.appengine.api.search.ScoredDocument)3 OperationResult (com.google.appengine.api.search.OperationResult)2 PutResponse (com.google.appengine.api.search.PutResponse)2 SearchException (com.google.appengine.api.search.SearchException)2 MemcacheService (com.google.appengine.api.memcache.MemcacheService)1 FieldType (com.google.appengine.api.search.Field.FieldType)1 Query (com.google.appengine.api.search.Query)1 QueryOptions (com.google.appengine.api.search.QueryOptions)1 Schema (com.google.appengine.api.search.Schema)1 SearchService (com.google.appengine.api.search.SearchService)1 SearchServiceConfig (com.google.appengine.api.search.SearchServiceConfig)1 SortOptions (com.google.appengine.api.search.SortOptions)1 StatusCode (com.google.appengine.api.search.StatusCode)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1