// 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;
  }
Пример #2
0
  public void testQueryInGeobox() {
    String cursorString = null;

    CursorPage<DEmployee, Long> page =
        employeeDao.queryInGeobox(
            20f, 110f, Geobox.BITS_18_154m, 60, null, false, null, false, cursorString);
    assertEquals(60, page.getItems().size());
    final DLocation centre = new DLocation(20f, 110f);
    for (DEmployee actual : page.getItems()) {
      double distance = Geobox.distance(centre, employeeDao.getGeoLocation(actual));
      System.out.println("   distance=" + distance);
      assertTrue("distance", distance < 308);
    }

    page =
        employeeDao.queryInGeobox(
            20f, 110f, Geobox.BITS_18_154m, 60, null, false, null, false, page.getCursorKey());
    assertEquals(23, page.getItems().size());
    for (DEmployee actual : page.getItems()) {
      double distance = Geobox.distance(centre, employeeDao.getGeoLocation(actual));
      System.out.println("   distance=" + distance);
      assertTrue("distance", distance < 308);
    }
  }