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);
}
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());
}
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;
}
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());
}
Aggregations