// Search in index for a query private CursorPage<DProduct, String> searchInIndexWithQuery( com.google.appengine.api.search.Query query, Index index) { CursorPage<DProduct, String> cursorPage = null; try { // Query the index. Results<ScoredDocument> results = index.search(query); // Collect all the primary keys Collection<String> ids = new ArrayList<String>(); for (ScoredDocument document : results) { ids.add(document.getId()); } if (ids.size() != 0) { // We got results, get the places from datastore Iterator<DProduct> dProductIterator = queryByPrimaryKeys(null, ids).iterator(); Collection<DProduct> dProducts = new ArrayList<DProduct>(ids.size()); while (dProductIterator.hasNext()) { dProducts.add(dProductIterator.next()); } // Build a cursor page cursorPage = new CursorPage<DProduct, String>(); cursorPage.setCursorKey(results.getCursor().toWebSafeString()); cursorPage.setItems(dProducts); } } catch (SearchException e) { if (StatusCode.TRANSIENT_ERROR.equals(e.getOperationResult().getCode())) { LOG.error("Search index failed"); // TODO: Error handling missing } throw new SearchException("Searching index failed"); } return cursorPage; }
public static String[] getAddresses(String name, int numResults, String withinURL) { String query = ""; if (withinURL != null && withinURL.length() > 0) query += "inurl:" + withinURL + " "; query += name; ArrayList<String> arrayList = new ArrayList<String>(); SearchClient client = new SearchClient("OzomwBrV34GSUD6G3vWHVV43nlVF.srrxIT88JqkrDhrTPAqLMhNBwZa6TT.8WuJmCU-"); // Create the web search request. In this case we're searching for // java-related hits. WebSearchRequest request = new WebSearchRequest(query); request.setResults(numResults); try { // Execute the search. WebSearchResults results = client.webSearch(request); // Iterate over the results. for (int i = 0; i < results.listResults().length; i++) { WebSearchResult result = results.listResults()[i]; // Print out the document title and URL. arrayList.add(result.getUrl()); } } catch (IOException e) { // Most likely a network exception of some sort. System.err.println("Error calling Yahoo! Search Service: " + e.toString()); e.printStackTrace(System.err); } catch (SearchException e) { // An issue with the XML or with the service. System.err.println("Error calling Yahoo! Search Service: " + e.toString()); e.printStackTrace(System.err); } String[] dummy = new String[0]; return arrayList.toArray(dummy); }