Search in sources :

Example 16 with Document

use of com.google.appengine.api.search.Document in project blogwt by billy1380.

the class PostService method toDocument.

/* (non-Javadoc)
	 * 
	 * @see
	 * com.willshex.blogwt.server.service.search.IIndex#toDocument(java.lang.
	 * Object) */
@Override
public Document toDocument(Post post) {
    Document document = null;
    if (post.authorKey != null) {
        post.author = UserServiceProvider.provide().getUser(keyToId(post.authorKey));
    }
    if (post.contentKey != null) {
        post.content = loadContent().id(keyToId(post.contentKey)).now();
    }
    if (Boolean.TRUE.equals(post.listed) && post.published != null) {
        if (post.author == null) {
            post.author = UserServiceProvider.provide().getUser(keyToId(post.authorKey));
        }
        Document.Builder documentBuilder = Document.newBuilder();
        documentBuilder.setId(getName() + post.id.toString()).addField(Field.newBuilder().setName("author").setAtom(post.author.username)).addField(Field.newBuilder().setName("author").setText(UserHelper.name(post.author))).addField(Field.newBuilder().setName("created").setDate(post.created)).addField(Field.newBuilder().setName("published").setDate(post.published)).addField(Field.newBuilder().setName("slug").setAtom(post.slug)).addField(Field.newBuilder().setName("summary").setText(post.summary)).addField(Field.newBuilder().setName("title").setText(post.title));
        if (post.content != null) {
            documentBuilder.addField(Field.newBuilder().setName("body").setText(post.content.body));
        }
        if (post.tags != null) {
            for (String tag : post.tags) {
                documentBuilder.addField(Field.newBuilder().setName("tag").setText(tag));
            }
        }
        document = documentBuilder.build();
    }
    return document;
}
Also used : Document(com.google.appengine.api.search.Document) ScoredDocument(com.google.appengine.api.search.ScoredDocument)

Example 17 with Document

use of com.google.appengine.api.search.Document in project blogwt by billy1380.

the class PageService method toDocument.

/* (non-Javadoc)
	 * 
	 * @see
	 * com.willshex.blogwt.server.service.search.IIndex#toDocument(java.lang.
	 * Object) */
@Override
public Document toDocument(Page page) {
    Document document = null;
    if (page != null) {
        page.owner = UserServiceProvider.provide().getUser(keyToId(page.ownerKey));
        populatePostContents(Arrays.asList(page));
        Document.Builder documentBuilder = Document.newBuilder();
        documentBuilder.setId(getName() + page.id.toString()).addField(Field.newBuilder().setName("owner").setAtom(page.owner.username)).addField(Field.newBuilder().setName("owner").setText(UserHelper.name(page.owner))).addField(Field.newBuilder().setName("created").setDate(page.created)).addField(Field.newBuilder().setName("title").setText(page.title));
        if (page.posts != null) {
            StringBuilder body = new StringBuilder();
            for (Post post : page.posts) {
                body.append(post.content.body).append("\n\n");
            }
            documentBuilder.addField(Field.newBuilder().setName("body").setText(body.toString()));
        }
        document = documentBuilder.build();
    }
    return document;
}
Also used : Post(com.willshex.blogwt.shared.api.datatype.Post) Document(com.google.appengine.api.search.Document) ScoredDocument(com.google.appengine.api.search.ScoredDocument)

Example 18 with Document

use of com.google.appengine.api.search.Document in project teammates by TEAMMATES.

the class DataMigrationForFeedbackResponseCommentSearchDocumentParticipantName method isMigrationNeeded.

/**
 * {@inheritDoc}
 */
@Override
protected boolean isMigrationNeeded(FeedbackResponseCommentAttributes comment) {
    FeedbackQuestionAttributes question = FeedbackQuestionsLogic.inst().getFeedbackQuestion(comment.feedbackQuestionId);
    if (question == null) {
        return false;
    }
    Document document = index.get(comment.getId().toString());
    if (isFixRequiredForGiverName(document, question) || isFixRequiredForReceiverName(document, question)) {
        println("Fix of response participant name required in document: " + document.getId());
        return true;
    }
    return false;
}
Also used : FeedbackQuestionAttributes(teammates.common.datatransfer.attributes.FeedbackQuestionAttributes) Document(com.google.appengine.api.search.Document)

Example 19 with Document

use of com.google.appengine.api.search.Document in project teammates by TEAMMATES.

the class SearchManager method putDocumentsWithRetry.

/**
 * Tries putting multiple documents, handling transient errors by retrying with exponential backoff.
 *
 * @throws PutException when only non-transient errors are encountered.
 * @throws MaximumRetriesExceededException with list of failed {@link Document}s as final data and
 *         final {@link OperationResult}'s message as final message, if operation fails after maximum retries.
 */
private static void putDocumentsWithRetry(String indexName, final List<Document> documents) throws PutException, MaximumRetriesExceededException {
    final Index index = getIndex(indexName);
    /*
         * The GAE Search API allows batch putting a List of Documents.
         * Results for each document are reported via a List of OperationResults.
         * We use RetryManager to retry putting a List of Documents, with each retry re-putting only
         * the documents that failed in the previous retry.
         * If we encounter one or more transient errors, we retry the operation.
         * If all results are non-transient errors, we give up and throw a PutException upwards.
         */
    RM.runUntilSuccessful(new RetryableTaskThrows<PutException>("Put documents") {

        private List<Document> documentsToPut = documents;

        private List<OperationResult> lastResults;

        private List<String> lastIds;

        @Override
        public void run() throws PutException {
            try {
                PutResponse response = index.put(documentsToPut);
                lastResults = response.getResults();
                lastIds = response.getIds();
            } catch (PutException e) {
                lastResults = e.getResults();
                lastIds = e.getIds();
            }
        }

        @Override
        public boolean isSuccessful() {
            boolean hasTransientError = false;
            List<Document> failedDocuments = new ArrayList<>();
            for (int i = 0; i < documentsToPut.size(); i++) {
                StatusCode code = lastResults.get(i).getCode();
                if (!StatusCode.OK.equals(code)) {
                    failedDocuments.add(documentsToPut.get(i));
                    if (StatusCode.TRANSIENT_ERROR.equals(code)) {
                        hasTransientError = true;
                    }
                }
            }
            // Update the list of documents to be put during the next retry
            documentsToPut = failedDocuments;
            // Update the final message and data to be shown if the task fails after maximum retries
            finalMessage = lastResults.get(0).getMessage();
            finalData = documentsToPut;
            if (documentsToPut.isEmpty()) {
                return true;
            } else if (hasTransientError) {
                // If there is at least one transient error, continue retrying
                return false;
            } else {
                // If all errors are non-transient, do not continue retrying
                throw new PutException(lastResults.get(0), lastResults, lastIds);
            }
        }
    });
}
Also used : Index(com.google.appengine.api.search.Index) OperationResult(com.google.appengine.api.search.OperationResult) Document(com.google.appengine.api.search.Document) ScoredDocument(com.google.appengine.api.search.ScoredDocument) PutResponse(com.google.appengine.api.search.PutResponse) StatusCode(com.google.appengine.api.search.StatusCode) PutException(com.google.appengine.api.search.PutException) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

Document (com.google.appengine.api.search.Document)19 ScoredDocument (com.google.appengine.api.search.ScoredDocument)8 PrintWriter (java.io.PrintWriter)6 Index (com.google.appengine.api.search.Index)5 ArrayList (java.util.ArrayList)5 Test (org.junit.Test)3 IndexSpec (com.google.appengine.api.search.IndexSpec)2 Date (java.util.Date)2 LoopHelper (teammates.client.scripts.util.LoopHelper)2 Cursor (com.google.appengine.api.search.Cursor)1 FieldType (com.google.appengine.api.search.Field.FieldType)1 GetRequest (com.google.appengine.api.search.GetRequest)1 OperationResult (com.google.appengine.api.search.OperationResult)1 PutException (com.google.appengine.api.search.PutException)1 PutResponse (com.google.appengine.api.search.PutResponse)1 Schema (com.google.appengine.api.search.Schema)1 SearchException (com.google.appengine.api.search.SearchException)1 StatusCode (com.google.appengine.api.search.StatusCode)1 User (com.google.appengine.api.users.User)1 Permission (com.willshex.blogwt.shared.api.datatype.Permission)1