Search in sources :

Example 1 with ScoredDocument

use of com.google.appengine.api.search.ScoredDocument in project pratilipi by Pratilipi.

the class SearchAccessorGaeImpl method searchPratilipi.

@Override
public DataListCursorTuple<Long> searchPratilipi(String query, PratilipiFilter pratilipiFilter, String cursorStr, Integer offset, Integer resultCount) {
    SortOptions.Builder sortOptionsBuilder = SortOptions.newBuilder();
    if (pratilipiFilter.getOrderByReadCount() != null) {
        sortOptionsBuilder.addSortExpression(SortExpression.newBuilder().setExpression("readCount").setDirection(pratilipiFilter.getOrderByReadCount() ? SortExpression.SortDirection.ASCENDING : SortExpression.SortDirection.DESCENDING).setDefaultValueNumeric(0.0));
    } else if (query != null && !query.isEmpty()) {
        sortOptionsBuilder.setMatchScorer(MatchScorer.newBuilder());
        sortOptionsBuilder.addSortExpression(SortExpression.newBuilder().setExpression(SortExpression.SCORE_FIELD_NAME).setDirection(SortExpression.SortDirection.DESCENDING).setDefaultValueNumeric(0.0));
        sortOptionsBuilder.addSortExpression(SortExpression.newBuilder().setExpression("relevance").setDirection(SortExpression.SortDirection.DESCENDING).setDefaultValueNumeric(0.0));
    } else {
        sortOptionsBuilder.addSortExpression(SortExpression.newBuilder().setExpression("relevance").setDirection(SortExpression.SortDirection.DESCENDING).setDefaultValueNumeric(0.0));
    }
    SortOptions sortOptions = sortOptionsBuilder.setLimit(1000).build();
    String searchQuery = pratilipiFilter.getType() == null ? "docType:Pratilipi" : "docType:Pratilipi-" + pratilipiFilter.getType().getName();
    if (pratilipiFilter.getLanguage() != null)
        searchQuery = searchQuery + " AND language:" + pratilipiFilter.getLanguage().getNameEn();
    if (pratilipiFilter.getAuthorId() != null)
        searchQuery = searchQuery + " AND author:" + pratilipiFilter.getAuthorId();
    if (query != null && !query.isEmpty())
        searchQuery = "( " + query + " ) AND " + searchQuery;
    Results<ScoredDocument> result = search(searchQuery, sortOptions, cursorStr, offset, resultCount, "docId");
    List<Long> pratilipiIdList = new ArrayList<>(result.getNumberReturned());
    for (ScoredDocument document : result) pratilipiIdList.add(Long.parseLong(document.getOnlyField("docId").getAtom()));
    Cursor cursor = resultCount == null || pratilipiIdList.size() < resultCount ? null : result.getCursor();
    return new DataListCursorTuple<Long>(pratilipiIdList, cursor == null ? null : cursor.toWebSafeString(), result.getNumberFound());
}
Also used : ScoredDocument(com.google.appengine.api.search.ScoredDocument) ArrayList(java.util.ArrayList) Cursor(com.google.appengine.api.search.Cursor) SortOptions(com.google.appengine.api.search.SortOptions)

Example 2 with ScoredDocument

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

the class SearchHelper method search.

public static String search(Collection<Long> resultsIds, String query, String indexName, String next, Integer count, String sortBy, SortDirectionType direction) {
    Cursor cursor = null;
    if (!SearchHelper.EMPTY_CURSOR.equals(next)) {
        // build options and query
        QueryOptions.Builder queryOptionsBuilder = QueryOptions.newBuilder().setCursor(next == null ? Cursor.newBuilder().build() : Cursor.newBuilder().build(next)).setLimit(count == null ? PagerHelper.DEFAULT_COUNT.intValue() : count.intValue()).setReturningIdsOnly(true);
        if (sortBy != null) {
            queryOptionsBuilder = queryOptionsBuilder.setSortOptions(SortOptions.newBuilder().addSortExpression(SortExpression.newBuilder().setExpression(sortBy).setDirection(direction == null || direction == SortDirectionType.SortDirectionTypeDescending ? SortExpression.SortDirection.DESCENDING : SortExpression.SortDirection.ASCENDING)).setLimit(SearchHelper.SORT_LIMIT).build());
        }
        QueryOptions options = queryOptionsBuilder.build();
        com.google.appengine.api.search.Query apiQuery = com.google.appengine.api.search.Query.newBuilder().setOptions(options).build(query);
        Results<ScoredDocument> matches = SearchHelper.getIndex(indexName).search(apiQuery);
        String id;
        if (resultsIds == null) {
            resultsIds = new ArrayList<>();
        } else {
            resultsIds.clear();
        }
        for (ScoredDocument scoredDocument : matches) {
            if ((id = scoredDocument.getId()) != null) {
                resultsIds.add(Long.valueOf(id));
            }
        }
        cursor = matches.getCursor();
    }
    return cursor == null ? SearchHelper.EMPTY_CURSOR : cursor.toWebSafeString();
}
Also used : ScoredDocument(com.google.appengine.api.search.ScoredDocument) Cursor(com.google.appengine.api.search.Cursor) QueryOptions(com.google.appengine.api.search.QueryOptions)

Example 3 with ScoredDocument

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

the class SearchHelper method search.

/**
 * @param query
 * @param start
 * @param count
 * @param sortBy
 * @param direction
 * @return
 */
public static List<Long> search(String query, String indexName, Integer start, Integer count, String sortBy, SortDirectionType direction) {
    QueryOptions.Builder queryOptionsBuilder = QueryOptions.newBuilder().setOffset(start == null ? PagerHelper.DEFAULT_START.intValue() : start.intValue()).setLimit(count == null ? PagerHelper.DEFAULT_COUNT.intValue() : count.intValue()).setReturningIdsOnly(true);
    if (sortBy != null) {
        queryOptionsBuilder.setSortOptions(SortOptions.newBuilder().addSortExpression(SortExpression.newBuilder().setExpression(sortBy).setDirection(direction == null || direction == SortDirectionType.SortDirectionTypeDescending ? SortExpression.SortDirection.DESCENDING : SortExpression.SortDirection.ASCENDING)).setLimit(SearchHelper.SORT_LIMIT).build());
    }
    QueryOptions options = queryOptionsBuilder.build();
    com.google.appengine.api.search.Query apiQuery = com.google.appengine.api.search.Query.newBuilder().setOptions(options).build(query);
    Results<ScoredDocument> matches = SearchHelper.getIndex(indexName).search(apiQuery);
    String id;
    List<Long> ids = new ArrayList<Long>();
    for (ScoredDocument scoredDocument : matches) {
        if ((id = scoredDocument.getId()) != null) {
            ids.add(Long.valueOf(id));
        }
    }
    return ids;
}
Also used : ScoredDocument(com.google.appengine.api.search.ScoredDocument) ArrayList(java.util.ArrayList) QueryOptions(com.google.appengine.api.search.QueryOptions)

Example 4 with ScoredDocument

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

the class PostService method search.

/* (non-Javadoc)
	 * 
	 * @see com.willshex.blogwt.server.service.search.ISearch#search(java.lang.
	 * String, java.lang.Integer, java.lang.Integer, java.lang.String,
	 * com.willshex.blogwt.shared.api.SortDirectionType) */
@Override
public List<Post> search(String query, Integer start, Integer count, String sortBy, SortDirectionType direction) {
    Results<ScoredDocument> matches = SearchHelper.getIndex().search(query);
    List<Post> posts = new ArrayList<Post>();
    String id;
    Post post;
    int limit = SearchHelper.SHORT_SEARCH_LIMIT;
    final String postServiceName = getName();
    for (ScoredDocument scoredDocument : matches) {
        if (limit == 0) {
            break;
        }
        if ((id = scoredDocument.getId()).startsWith(postServiceName)) {
            post = getPost(Long.valueOf(id.replace(postServiceName, "")));
            if (post != null) {
                posts.add(post);
            }
        }
        limit--;
    }
    return posts;
}
Also used : ScoredDocument(com.google.appengine.api.search.ScoredDocument) Post(com.willshex.blogwt.shared.api.datatype.Post) ArrayList(java.util.ArrayList)

Example 5 with ScoredDocument

use of com.google.appengine.api.search.ScoredDocument in project pratilipi by Pratilipi.

the class SearchAccessorGaeImpl method searchAuthor.

// AUTHOR Data
@Override
public DataListCursorTuple<Long> searchAuthor(String query, AuthorFilter authorFilter, String cursorStr, Integer offset, Integer resultCount) {
    SortOptions.Builder sortOptionsBuilder = SortOptions.newBuilder();
    if (authorFilter.getOrderByContentPublished() != null) {
        sortOptionsBuilder.addSortExpression(SortExpression.newBuilder().setExpression("contentPublished").setDirection(authorFilter.getOrderByContentPublished() ? SortExpression.SortDirection.ASCENDING : SortExpression.SortDirection.DESCENDING).setDefaultValueNumeric(0.0));
    } else if (query != null && !query.isEmpty()) {
        sortOptionsBuilder.setMatchScorer(MatchScorer.newBuilder());
        sortOptionsBuilder.addSortExpression(SortExpression.newBuilder().setExpression(SortExpression.SCORE_FIELD_NAME).setDirection(SortExpression.SortDirection.DESCENDING).setDefaultValueNumeric(0.0));
        sortOptionsBuilder.addSortExpression(SortExpression.newBuilder().setExpression("contentPublished").setDirection(SortExpression.SortDirection.DESCENDING).setDefaultValueNumeric(0.0));
    } else {
        sortOptionsBuilder.addSortExpression(SortExpression.newBuilder().setExpression("contentPublished").setDirection(SortExpression.SortDirection.DESCENDING).setDefaultValueNumeric(0.0));
    }
    SortOptions sortOptions = sortOptionsBuilder.setLimit(1000).build();
    String searchQuery = "docType:Author";
    if (authorFilter.getLanguage() != null)
        searchQuery = searchQuery + " AND language:" + authorFilter.getLanguage().getNameEn();
    if (query != null && !query.isEmpty())
        searchQuery = "( " + query + " ) AND " + searchQuery;
    Results<ScoredDocument> result = search(searchQuery, sortOptions, cursorStr, offset, resultCount, "docId");
    List<Long> authorIdList = new ArrayList<>(result.getNumberReturned());
    for (ScoredDocument document : result) authorIdList.add(Long.parseLong(document.getOnlyField("docId").getAtom()));
    Cursor cursor = resultCount == null || authorIdList.size() < resultCount ? null : result.getCursor();
    return new DataListCursorTuple<Long>(authorIdList, cursor == null ? null : cursor.toWebSafeString(), result.getNumberFound());
}
Also used : ScoredDocument(com.google.appengine.api.search.ScoredDocument) ArrayList(java.util.ArrayList) Cursor(com.google.appengine.api.search.Cursor) SortOptions(com.google.appengine.api.search.SortOptions)

Aggregations

ScoredDocument (com.google.appengine.api.search.ScoredDocument)7 ArrayList (java.util.ArrayList)6 Cursor (com.google.appengine.api.search.Cursor)3 QueryOptions (com.google.appengine.api.search.QueryOptions)2 SortOptions (com.google.appengine.api.search.SortOptions)2 Page (com.willshex.blogwt.shared.api.datatype.Page)1 Post (com.willshex.blogwt.shared.api.datatype.Post)1 User (com.willshex.blogwt.shared.api.datatype.User)1