Search in sources :

Example 1 with SearchException

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

use of com.google.appengine.api.search.SearchException in project java-docs-samples by GoogleCloudPlatform.

the class SearchServlet method doGet.

@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    PrintWriter out = resp.getWriter();
    Document doc = Document.newBuilder().setId("theOnlyPiano").addField(Field.newBuilder().setName("product").setText("piano")).addField(Field.newBuilder().setName("maker").setText("Yamaha")).addField(Field.newBuilder().setName("price").setNumber(4000)).build();
    try {
        Utils.indexADocument(SEARCH_INDEX, doc);
    } catch (InterruptedException e) {
    // ignore
    }
    // [START search_document]
    final int maxRetry = 3;
    int attempts = 0;
    int delay = 2;
    while (true) {
        try {
            String queryString = "product = piano AND price < 5000";
            Results<ScoredDocument> results = getIndex().search(queryString);
            // Iterate over the documents in the results
            for (ScoredDocument document : results) {
                // handle results
                out.print("maker: " + document.getOnlyField("maker").getText());
                out.println(", price: " + document.getOnlyField("price").getNumber());
            }
        } catch (SearchException e) {
            if (StatusCode.TRANSIENT_ERROR.equals(e.getOperationResult().getCode()) && ++attempts < maxRetry) {
                // retry
                try {
                    Thread.sleep(delay * 1000);
                } catch (InterruptedException e1) {
                // ignore
                }
                // easy exponential backoff
                delay *= 2;
                continue;
            } else {
                throw e;
            }
        }
        break;
    }
    // [END search_document]
    // We don't test the search result below, but we're fine if it runs without errors.
    out.println("Search performed");
    Index index = getIndex();
    // [START simple_search_1]
    index.search("rose water");
    // [END simple_search_1]
    // [START simple_search_2]
    index.search("1776-07-04");
    // [END simple_search_2]
    // [START simple_search_3]
    // search for documents with pianos that cost less than $5000
    index.search("product = piano AND price < 5000");
// [END simple_search_3]
}
Also used : ScoredDocument(com.google.appengine.api.search.ScoredDocument) SearchException(com.google.appengine.api.search.SearchException) Index(com.google.appengine.api.search.Index) Document(com.google.appengine.api.search.Document) ScoredDocument(com.google.appengine.api.search.ScoredDocument) PrintWriter(java.io.PrintWriter)

Aggregations

Index (com.google.appengine.api.search.Index)2 ScoredDocument (com.google.appengine.api.search.ScoredDocument)2 SearchException (com.google.appengine.api.search.SearchException)2 Document (com.google.appengine.api.search.Document)1 IndexSpec (com.google.appengine.api.search.IndexSpec)1 Query (com.google.appengine.api.search.Query)1 QueryOptions (com.google.appengine.api.search.QueryOptions)1 SortOptions (com.google.appengine.api.search.SortOptions)1 PrintWriter (java.io.PrintWriter)1