Search in sources :

Example 1 with SortOptions

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

the class SearchAccessorGaeImpl method search.

// Helper Methods
public Results<ScoredDocument> search(String searchQuery, SortOptions sortOptions, String cursorStr, Integer offset, Integer resultCount, String... fieldsToReturn) {
    if (sortOptions == null) {
        SortOptions.Builder sortOptionsBuilder = SortOptions.newBuilder();
        sortOptionsBuilder.setMatchScorer(MatchScorer.newBuilder());
        sortOptionsBuilder.addSortExpression(SortExpression.newBuilder().setExpression(SortExpression.SCORE_FIELD_NAME).setDirection(SortExpression.SortDirection.DESCENDING).setDefaultValueNumeric(0.0));
        // Max allowed: 10000
        sortOptions = sortOptionsBuilder.setLimit(1000).build();
    }
    QueryOptions.Builder queryOptionsBuilder = QueryOptions.newBuilder().setSortOptions(sortOptions).setLimit(resultCount == null ? 1000 : resultCount).setNumberFoundAccuracy(// Max allowed: 10000
    1000).setFieldsToReturn(fieldsToReturn);
    if (cursorStr != null && !cursorStr.trim().isEmpty())
        queryOptionsBuilder.setCursor(Cursor.newBuilder().build(cursorStr));
    else if (offset != null && offset > 0)
        queryOptionsBuilder.setOffset(offset);
    else
        // Without this, query will not return a cursor.
        queryOptionsBuilder.setCursor(Cursor.newBuilder());
    Query query = Query.newBuilder().setOptions(queryOptionsBuilder).build(searchQuery);
    logger.log(Level.INFO, "Search Query: " + query.toString());
    return searchIndex.search(query);
}
Also used : Query(com.google.appengine.api.search.Query) QueryOptions(com.google.appengine.api.search.QueryOptions) SortOptions(com.google.appengine.api.search.SortOptions)

Example 2 with SortOptions

use of com.google.appengine.api.search.SortOptions 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 3 with SortOptions

use of com.google.appengine.api.search.SortOptions 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 4 with SortOptions

use of com.google.appengine.api.search.SortOptions 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

SortOptions (com.google.appengine.api.search.SortOptions)4 ScoredDocument (com.google.appengine.api.search.ScoredDocument)3 Cursor (com.google.appengine.api.search.Cursor)2 Query (com.google.appengine.api.search.Query)2 QueryOptions (com.google.appengine.api.search.QueryOptions)2 ArrayList (java.util.ArrayList)2 Index (com.google.appengine.api.search.Index)1 IndexSpec (com.google.appengine.api.search.IndexSpec)1 SearchException (com.google.appengine.api.search.SearchException)1