com.google.appengine.api.search.Results Java Examples

The following examples show how to use com.google.appengine.api.search.Results. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SearchServiceTest.java    From appengine-tck with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetIndexesAsyncRequest() throws ExecutionException, InterruptedException {
    String indexName = "put-index";
    String docId = "testPutDocs";
    Index index = createIndex(indexName, docId);

    GetIndexesRequest request = GetIndexesRequest.newBuilder()
            .setIndexNamePrefix(indexName)
            .build();

    Future<GetResponse<Index>> response = searchService.getIndexesAsync(request);
    List<Index> listIndexes = response.get().getResults();

    for (Index oneIndex : listIndexes) {
        Future<Results<ScoredDocument>> Fres = oneIndex.searchAsync("");

        Iterator<ScoredDocument> it = Fres.get().iterator();
        assertEquals(docId + "1", it.next().getId());
        assertEquals(docId + "2", it.next().getId());
        sync();
    }
}
 
Example #2
Source File: SearchServiceTest.java    From appengine-tck with Apache License 2.0 6 votes vote down vote up
@Test
public void testSearchAsyncString() throws ExecutionException, InterruptedException {
    String indexName = "put-index";
    String docId = "testPutDocs";
    Index index = createIndex(indexName, docId);

    GetIndexesRequest request = GetIndexesRequest.newBuilder()
            .setIndexNamePrefix(indexName)
            .build();
    GetResponse<Index> response = searchService.getIndexes(request);
    List<Index> listIndexes = response.getResults();

    for (Index oneIndex : listIndexes) {
        Future<Results<ScoredDocument>> rRes = oneIndex.searchAsync("");

        Iterator<ScoredDocument> it = rRes.get().iterator();
        assertEquals(docId + "1", it.next().getId());
        assertEquals(docId + "2", it.next().getId());
        sync();
    }
}
 
Example #3
Source File: SearchServiceTest.java    From appengine-tck with Apache License 2.0 6 votes vote down vote up
@Test
public void  testGetField() {
    String namelist = "geofield; textfield; numfield; atomfield; datefield; htmlfield; mixfield";
    Results<ScoredDocument> result = searchDocs(index, "", 0);
    for (ScoredDocument doc : result) {
        assertEquals(2, doc.getFieldCount("textfield"));
        String id = doc.getId();
        assertNotNull(id);
        if (id.endsWith("0")) {
            assertEquals(new Locale("cn"), doc.getLocale());
        } else {
            assertEquals(new Locale("en"), doc.getLocale());
        }

        for (String name : doc.getFieldNames()) {
            assertTrue(namelist.contains(name));
        }

        Iterator<Field> fields = doc.getFields().iterator();
        int count = 0;
        for ( ; fields.hasNext() ; ++count ) {
            fields.next();
        }
        assertEquals(9, count);
    }
}
 
Example #4
Source File: InstructorSearchDocument.java    From teammates with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Produces an {@link InstructorSearchResultBundle} from the {@code Results<ScoredDocument>} collection.
 *
 * <p>This method should be used by admin only since the searching does not restrict the
 * visibility according to the logged-in user's google ID.</p>
 */
public static InstructorSearchResultBundle fromResults(Results<ScoredDocument> results) {
    InstructorSearchResultBundle bundle = new InstructorSearchResultBundle();
    if (results == null) {
        return bundle;
    }

    for (ScoredDocument doc : results) {
        InstructorAttributes instructor = instructorsDb.getInstructorForRegistrationKey(doc.getId());
        if (instructor == null) {
            // search engine out of sync as SearchManager may fail to delete documents due to GAE error
            // the chance is low and it is generally not a big problem
            instructorsDb.deleteDocumentByEncryptedInstructorKey(doc.getId());
            continue;
        }

        bundle.instructorList.add(instructor);
        bundle.numberOfResults++;
    }

    sortInstructorResultList(bundle.instructorList);

    return bundle;
}
 
Example #5
Source File: SearchDocument.java    From teammates with GNU General Public License v2.0 6 votes vote down vote up
/**
 * This method must be called to filter out the search result for course Id.
 */
protected static List<ScoredDocument> filterOutCourseId(Results<ScoredDocument> results,
                                                        List<InstructorAttributes> instructors) {
    Set<String> courseIdSet = new HashSet<>();
    for (InstructorAttributes ins : instructors) {
        courseIdSet.add(ins.courseId);
    }

    List<ScoredDocument> filteredResults = new ArrayList<>();
    for (ScoredDocument document : results) {
        String resultCourseId = document.getOnlyField(Const.SearchDocumentField.COURSE_ID).getText();
        if (courseIdSet.contains(resultCourseId)) {
            filteredResults.add(document);
        }
    }
    return filteredResults;
}
 
Example #6
Source File: SearchServiceTest.java    From appengine-tck with Apache License 2.0 6 votes vote down vote up
@Test
public void testSearchServiceConfig() throws ExecutionException, InterruptedException {
    String indexName = "put-index";
    String docId = "testPutDocs";
    searchService = SearchServiceFactory.getSearchService(SearchServiceConfig.newBuilder().setDeadline(10.).build());

    Index index = createIndex(indexName, docId);

    GetIndexesRequest request = GetIndexesRequest.newBuilder()
            .setIndexNamePrefix(indexName)
            .build();
    GetResponse<Index> response = searchService.getIndexes(request);
    List<Index> listIndexes = response.getResults();

    for (Index oneIndex : listIndexes) {
        Results<ScoredDocument> res = oneIndex.search("");

        Iterator<ScoredDocument> it = res.iterator();
        assertEquals(docId + "1", it.next().getId());
        assertEquals(docId + "2", it.next().getId());
        sync();
    }
}
 
Example #7
Source File: StudentSearchDocument.java    From teammates with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Produces a {@link StudentSearchResultBundle} from the {@code Results<ScoredDocument>} collection.
 *
 * <p>The list of {@link InstructorAttributes} is used to filter out the search result.</p>
 */
public static StudentSearchResultBundle fromResults(Results<ScoredDocument> results,
                                                    List<InstructorAttributes> instructors) {
    if (results == null) {
        return new StudentSearchResultBundle();
    }

    List<ScoredDocument> filteredResults = filterOutCourseId(results, instructors);
    StudentSearchResultBundle bundle = constructBaseBundle(filteredResults);

    for (InstructorAttributes ins : instructors) {
        bundle.courseIdInstructorMap.put(ins.courseId, ins);
    }

    sortStudentResultList(bundle.studentList);

    return bundle;
}
 
Example #8
Source File: SearchServiceTest.java    From appengine-tck with Apache License 2.0 6 votes vote down vote up
@Test
public void testLocale() {
    Results<ScoredDocument> result = searchDocs(index, "", 0);
    for (ScoredDocument doc : result) {
        String id = doc.getId();
        if (id.endsWith("0")) {
            assertEquals(new Locale("cn"), doc.getLocale());
        } else {
            assertEquals(new Locale("en"), doc.getLocale());
        }

        boolean found = false;
        for (Field textField : doc.getFields("textfield")) {
            Locale locale = textField.getLocale();
            if ((locale != null) && locale.equals(Locale.FRENCH)) {
                found = true;
            }
        }
        assertTrue(found);
    }
}
 
Example #9
Source File: SearchServiceTest.java    From appengine-tck with Apache License 2.0 6 votes vote down vote up
@Test
public void testSearchAsyncQuery() throws ExecutionException, InterruptedException {
    String indexName = "put-index";
    String docId = "testPutDocs";
    Index index = createIndex(indexName, docId);

    GetIndexesRequest request = GetIndexesRequest.newBuilder()
            .setIndexNamePrefix(indexName)
            .build();
    GetResponse<Index> response = searchService.getIndexes(request);
    List<Index> listIndexes = response.getResults();

    for (Index oneIndex : listIndexes) {
        QueryOptions.Builder optionBuilder = QueryOptions.newBuilder();
        optionBuilder.setLimit(10);
        Query.Builder queryBuilder = Query.newBuilder().setOptions(optionBuilder.build());
        Future<Results<ScoredDocument>> Fres = oneIndex.searchAsync(queryBuilder.build(""));

        Iterator<ScoredDocument> it = Fres.get().iterator();
        assertEquals(docId + "1", it.next().getId());
        assertEquals(docId + "2", it.next().getId());
        sync();
    }
}
 
Example #10
Source File: SearchServiceTest.java    From appengine-tck with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetIndexesAsyncBuilder() throws ExecutionException, InterruptedException {
    String indexName = "put-index";
    String docId = "testPutDocs";
    Index index = createIndex(indexName, docId);

    GetIndexesRequest.Builder builder = GetIndexesRequest.newBuilder()
            .setIndexNamePrefix(indexName);

    Future<GetResponse<Index>> response = searchService.getIndexesAsync(builder);
    List<Index> listIndexes = response.get().getResults();

    for (Index oneIndex : listIndexes) {
        Future<Results<ScoredDocument>> Fres = oneIndex.searchAsync("");

        Iterator<ScoredDocument> it = Fres.get().iterator();
        assertEquals(docId + "1", it.next().getId());
        assertEquals(docId + "2", it.next().getId());
        sync();
    }
}
 
Example #11
Source File: SearchOptionServlet.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
  // Put one document to avoid an error
  Document document =
      Document.newBuilder()
          .setId("theOnlyCoffeeRoaster")
          .addField(Field.newBuilder().setName("price").setNumber(200))
          .addField(Field.newBuilder().setName("model").setText("TZ4000"))
          .addField(Field.newBuilder().setName("brand").setText("MyBrand"))
          .addField(Field.newBuilder().setName("product").setText("coffee roaster"))
          .addField(
              Field.newBuilder().setName("description").setText("A coffee bean roaster at home"))
          .build();
  try {
    Utils.indexADocument(SEARCH_INDEX, document);
  } catch (InterruptedException e) {
    // ignore
  }
  PrintWriter out = resp.getWriter();
  Results<ScoredDocument> result = doSearch();
  for (ScoredDocument doc : result.getResults()) {
    out.println(doc.toString());
  }
}
 
Example #12
Source File: SearchServiceTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@Test(expected = IllegalArgumentException.class)
public void  testGetOnlyField() {
    Results<ScoredDocument> result = searchDocs(index, "text with num 0", 0);
    for (ScoredDocument doc : result) {
        @SuppressWarnings("UnusedDeclaration")
        Field field = doc.getOnlyField("textfield");
    }
}
 
Example #13
Source File: SearchServiceTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@Test
public void  testSearchOperation() {
    Results<ScoredDocument> result = searchDocs(index, "NOT text", 0);
    assertEquals(0, result.getNumberFound());

    result = searchDocs(index, "0 OR 1", 0);
    assertEquals(2, result.getNumberFound());
}
 
Example #14
Source File: SearchServiceTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@Test
public void testSortOptions() {
    for (SortExpression.SortDirection direction : SortExpression.SortDirection.values()) {
        SortExpression sortExpression = SortExpression.newBuilder()
            .setExpression("numfield")
            .setDirection(direction)
            .setDefaultValueNumeric(9999)
            .build();
        SortOptions sortOptions = SortOptions.newBuilder()
            .addSortExpression(sortExpression)
            .build();
        QueryOptions options = QueryOptions.newBuilder()
            .setFieldsToReturn("numfield", "datefield")
            .setSortOptions(sortOptions)
            .build();
        Query.Builder queryBuilder = Query.newBuilder().setOptions(options);
        Results<ScoredDocument> results = index.search(queryBuilder.build(""));

        double pre = -9999;
        if (direction.equals(SortExpression.SortDirection.DESCENDING)) {
            pre = 9999;
        }
        for (ScoredDocument doc : results) {
            assertEquals(2, doc.getFieldNames().size());
            for (Field numField : doc.getFields("numfield")) {
                if (direction.equals(SortExpression.SortDirection.DESCENDING)) {
                    assertTrue(pre > numField.getNumber());
                } else {
                    assertTrue(pre < numField.getNumber());
                }
                pre = numField.getNumber();
            }
        }
    }
}
 
Example #15
Source File: SearchServiceTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@Test
public void testFieldSearch() {
    Results<ScoredDocument> result = searchDocs(index, "textfield:text with num", 0);
    assertEquals(3, result.getNumberFound());

    result = searchDocs(index, "textfield:text with num 0", 0);
    assertEquals(1, result.getNumberFound());

    result = searchDocs(index, "numfield:0", 0);
    assertEquals(1, result.getNumberFound());

    result = searchDocs(index, "numfield:-1", 0);
    assertEquals(0, result.getNumberFound());
}
 
Example #16
Source File: SearchServiceTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@Test
public void testSimpleSearch() {
    Results<ScoredDocument> result = searchDocs(index, "", 0);
    assertEquals(3, result.getNumberFound());

    result = searchDocs(index, "", 2);
    assertEquals(3, result.getNumberFound());
    assertEquals(2, result.getNumberReturned());

    result = searchDocs(index, "text with num", 0);
    assertEquals(3, result.getNumberFound());

    result = searchDocs(index, "text with num 0", 0);
    assertEquals(1, result.getNumberFound());
}
 
Example #17
Source File: SearchTestBase.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
protected Results<ScoredDocument> searchDocs(Index index, String query, int limit) {
    if (limit > 0) {
        QueryOptions.Builder optionBuilder = QueryOptions.newBuilder();
        optionBuilder.setLimit(limit);
        Query.Builder queryBuilder = Query.newBuilder().setOptions(optionBuilder.build());
        return index.search(queryBuilder.build(query));
    } else {
        return index.search(query);
    }
}
 
Example #18
Source File: IndexTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@Test
public void testPutDeleteAsyncDocsByIterable() throws InterruptedException {
    String indexName = "put-index";
    String docId = "testPutDocs";
    Index index = createIndex(indexName, docId);

    List<String> dList = new ArrayList<>();
    Results<ScoredDocument> found = searchDocs(index, "", 0);
    Iterator<ScoredDocument> it = found.iterator();

    ScoredDocument doc = it.next();
    assertEquals(docId + "1", doc.getId());
    dList.add(doc.getId());

    doc = it.next();
    assertEquals(docId + "2", doc.getId());
    dList.add(doc.getId());

    GetIndexesRequest request = GetIndexesRequest.newBuilder()
            .setIndexNamePrefix(indexName)
            .build();
    GetResponse<Index> response = searchService.getIndexes(request);
    List<Index> listIndexes = response.getResults();

    for (Index oneIndex : listIndexes) {
        oneIndex.deleteAsync(dList);
        sync();
        verifyDocCount(index, 0);
    }
}
 
Example #19
Source File: DocumentTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreateDocument() throws Exception {
    String indexName = "test-doc";
    Index index = searchService.getIndex(IndexSpec.newBuilder().setName(indexName));
    delDocs(index);
    Builder docBuilder = Document.newBuilder().setId("tck").setLocale(Locale.FRENCH).setRank(8);
    docBuilder.addField(Field.newBuilder().setName("field1").setText("text field"));
    docBuilder.addField(Field.newBuilder().setName("field1").setNumber(987));
    docBuilder.addField(Field.newBuilder().setName("field2").setNumber(123));
    docBuilder.addField(Field.newBuilder().setName("field3").setDate(new Date()));
    index.put(docBuilder.build());
    sync();

    Results<ScoredDocument> result = searchDocs(index, "", 0);
    assertEquals(1, result.getNumberReturned());
    ScoredDocument retDoc = result.iterator().next();
    assertEquals("tck", retDoc.getId());
    assertEquals(Locale.FRENCH, retDoc.getLocale());
    assertEquals(8, retDoc.getRank());
    assertEquals(2, retDoc.getFieldCount("field1"));
    assertEquals(1, retDoc.getFieldCount("field3"));
    assertEquals(3, retDoc.getFieldNames().size());

    Iterator<Field> fields = retDoc.getFields().iterator();
    int count = 0;
    for ( ; fields.hasNext() ; ++count ) {
        fields.next();
    }
    assertEquals(4, count);

    fields = retDoc.getFields("field1").iterator();
    count = 0;
    for ( ; fields.hasNext() ; ++count ) {
        fields.next();
    }
    assertEquals(2, count);

    Field field = retDoc.getOnlyField("field2");
    assertEquals(FieldType.NUMBER, field.getType());
}
 
Example #20
Source File: SearchTestBase.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
protected void delDocs(Index index) throws InterruptedException {
    List<String> dList = new ArrayList<>();
    Results<ScoredDocument> found = searchDocs(index, "", 0);
    for (ScoredDocument document : found) {
        dList.add(document.getId());
    }
    index.delete(dList);
    sync();
}
 
Example #21
Source File: StudentSearchDocument.java    From teammates with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Produces a {@link StudentSearchResultBundle} from the {@code Results<ScoredDocument>} collection.
 *
 * <p>The list of {@link InstructorAttributes} is used to filter out the search result.</p>
 *
 * <p>This method should be used by admin only since the searching does not restrict the
 * visibility according to the logged-in user's google ID.</p>
 */
public static StudentSearchResultBundle fromResults(Results<ScoredDocument> results) {
    if (results == null) {
        return new StudentSearchResultBundle();
    }

    StudentSearchResultBundle bundle = constructBaseBundle(results);

    sortStudentResultList(bundle.studentList);

    return bundle;
}
 
Example #22
Source File: InstructorsDb.java    From teammates with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Searches all instructors in the system.
 *
 * <p>This method should be used by admin only since the searching does not restrict the
 * visibility according to the logged-in user's google ID. This is used by admin to
 * search instructors in the whole system.
 */
public InstructorSearchResultBundle searchInstructorsInWholeSystem(String queryString) {

    if (queryString.trim().isEmpty()) {
        return new InstructorSearchResultBundle();
    }

    Results<ScoredDocument> results = searchDocuments(Const.SearchIndex.INSTRUCTOR,
                                                      new InstructorSearchQuery(queryString));

    return InstructorSearchDocument.fromResults(results);
}
 
Example #23
Source File: FeedbackResponseCommentsDb.java    From teammates with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Searches for comments, using a list of instructors as a constraint.
 */
public FeedbackResponseCommentSearchResultBundle search(String queryString, List<InstructorAttributes> instructors) {
    if (queryString.trim().isEmpty()) {
        return new FeedbackResponseCommentSearchResultBundle();
    }

    Results<ScoredDocument> results = searchDocuments(Const.SearchIndex.FEEDBACK_RESPONSE_COMMENT,
            new FeedbackResponseCommentSearchQuery(instructors, queryString));

    return FeedbackResponseCommentSearchDocument.fromResults(results, instructors);
}
 
Example #24
Source File: EntitiesDb.java    From teammates with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Searches documents with query.
 */
protected Results<ScoredDocument> searchDocuments(String indexName, SearchQuery query) {
    try {
        if (query.getFilterSize() > 0) {
            return SearchManager.searchDocuments(indexName, query.toQuery());
        }
        return null;
    } catch (SearchQueryException e) {
        log.info("Unsupported query for this query string: " + query.toString());
        return null;
    }
}
 
Example #25
Source File: StudentsDb.java    From teammates with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Searches all students in the system.
 *
 * <p>This method should be used by admin only since the searching does not restrict the
 * visibility according to the logged-in user's google ID. This is used by admin to
 * search instructors in the whole system.
 */
public StudentSearchResultBundle searchStudentsInWholeSystem(String queryString) {
    if (queryString.trim().isEmpty()) {
        return new StudentSearchResultBundle();
    }

    Results<ScoredDocument> results = searchDocuments(Const.SearchIndex.STUDENT,
            new StudentSearchQuery(queryString));

    return StudentSearchDocument.fromResults(results);
}
 
Example #26
Source File: StudentsDb.java    From teammates with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Searches for students.
 *
 * @param instructors the constraint that restricts the search result
 */
public StudentSearchResultBundle search(String queryString, List<InstructorAttributes> instructors) {
    if (queryString.trim().isEmpty()) {
        return new StudentSearchResultBundle();
    }

    Results<ScoredDocument> results = searchDocuments(Const.SearchIndex.STUDENT,
            new StudentSearchQuery(instructors, queryString));

    return StudentSearchDocument.fromResults(results, instructors);
}
 
Example #27
Source File: PlacesHelper.java    From solutions-mobile-shopping-assistant-backend-java with Apache License 2.0 4 votes vote down vote up
static List<PlaceInfo> getPlaces(GeoPt location, long distanceInMeters, int resultCount) {

    // TODO(user): Use memcache

    String geoPoint = "geopoint(" + location.getLatitude() + ", " + location.getLongitude() + ")";

    String query = "distance(place_location, " + geoPoint + ") < " + distanceInMeters;
    String locExpr = "distance(place_location, " + geoPoint + ")";

    SortExpression sortExpr = SortExpression.newBuilder()
        .setExpression(locExpr)
        .setDirection(SortExpression.SortDirection.ASCENDING)
        .setDefaultValueNumeric(distanceInMeters + 1)
        .build();
    Query searchQuery = Query.newBuilder().setOptions(QueryOptions.newBuilder()
        .setSortOptions(SortOptions.newBuilder().addSortExpression(sortExpr))).build(query);
    Results<ScoredDocument> results = getIndex().search(searchQuery);

    if (results.getNumberFound() == 0) {
      // geo-location doesn't work under dev_server
      if (environment.value() == Development) {
        // return all documents
        results = getIndex().search("value > 0");
      }
    }

    List<PlaceInfo> places = new ArrayList<PlaceInfo>();

    for (ScoredDocument document : results) {
      if (places.size() >= resultCount) {
        break;
      }

      GeoPoint p = document.getOnlyField("place_location").getGeoPoint();

      PlaceInfo place = new PlaceInfo();
      place.setplaceID(document.getOnlyField("id").getText());
      place.setName(document.getOnlyField("name").getText());
      place.setAddress(document.getOnlyField("address").getText());

      place.setLocation(new GeoPt((float) p.getLatitude(), (float) p.getLongitude()));

      // GeoPoints are not implemented on dev server and latitude and longitude are set to zero
      // But since those are doubles let's play safe
      // and use double comparison with epsilon set to 0.0001
      if (Math.abs(p.getLatitude()) <= 0.0001 && Math.abs(p.getLongitude()) <= 0.0001) {
        // set a fake distance of 5+ km
        place.setDistanceInKilometers(5 + places.size());
      } else {
        double distance = distanceInMeters / 1000;
        try {
          distance = getDistanceInKm(
              p.getLatitude(), p.getLongitude(), location.getLatitude(), location.getLongitude());
        } catch (Exception e) {
          log.warning("Exception when calculating a distance: " + e.getMessage());
        }

        place.setDistanceInKilometers(distance);
      }

      places.add(place);
    }

    return places;
  }
 
Example #28
Source File: PlacesHelper.java    From MobileShoppingAssistant-sample with Apache License 2.0 4 votes vote down vote up
/**
 * Returns the nearest places to the location of the user.
 * @param location the location of the user.
 * @param distanceInMeters the maximum distance to the user.
 * @param resultCount the maximum number of places returned.
 * @return List of up to resultCount places in the datastore ordered by
 *      the distance to the location parameter and less than
 *      distanceInMeters meters to the location parameter.
 */
public static List<PlaceInfo> getPlaces(final GeoPt location,
        final long distanceInMeters, final int resultCount) {

    // Optional: use memcache

    String geoPoint = "geopoint(" + location.getLatitude() + ", " + location
            .getLongitude()
            + ")";
    String locExpr = "distance(place_location, " + geoPoint + ")";

    // Build the SortOptions with 2 sort keys
    SortOptions sortOptions = SortOptions.newBuilder()
            .addSortExpression(SortExpression.newBuilder()
                    .setExpression(locExpr)
                    .setDirection(SortExpression.SortDirection.ASCENDING)
                    .setDefaultValueNumeric(distanceInMeters + 1))
            .setLimit(resultCount)
            .build();
    // Build the QueryOptions
    QueryOptions options = QueryOptions.newBuilder()
            .setSortOptions(sortOptions)
            .build();
    // Query string
    String searchQuery = "distance(place_location, " + geoPoint + ") < "
            + distanceInMeters;

    Query query = Query.newBuilder().setOptions(options).build(searchQuery);

    Results<ScoredDocument> results = getIndex().search(query);

    if (results.getNumberFound() == 0) {
        // geo-location doesn't work under dev_server
        if (environment.value() == Development) {
            // return all documents
            results = getIndex().search("value > 0");
        }
    }

    List<PlaceInfo> places = new ArrayList<>();

    for (ScoredDocument document : results) {
        if (places.size() >= resultCount) {
            break;
        }

        GeoPoint p = document.getOnlyField("place_location").getGeoPoint();

        PlaceInfo place = new PlaceInfo();
        place.setPlaceId(Long.valueOf(document.getOnlyField("id")
                .getText()));
        place.setName(document.getOnlyField("name").getText());
        place.setAddress(document.getOnlyField("address").getText());

        place.setLocation(new GeoPt((float) p.getLatitude(),
                (float) p.getLongitude()));

        // GeoPoints are not implemented on dev server and latitude and
        // longitude are set to zero
        // But since those are doubles let's play safe
        // and use double comparison with epsilon set to EPSILON
        if (Math.abs(p.getLatitude()) <= EPSILON
                && Math.abs(p.getLongitude()) <= EPSILON) {
            // set a fake distance of 5+ km
            place.setDistanceInKilometers(FAKE_DISTANCE_FOR_DEV + places
                    .size());
        } else {
            double distance = distanceInMeters / METERS_IN_KILOMETER;
            try {
                distance = getDistanceInKm(
                        p.getLatitude(), p.getLongitude(),
                        location.getLatitude(),
                        location.getLongitude());
            } catch (Exception e) {
                LOG.warning("Exception when calculating a distance: " + e
                        .getMessage());
            }

            place.setDistanceInKilometers(distance);
        }

        places.add(place);
    }
    return places;
}
 
Example #29
Source File: FieldTest.java    From appengine-tck with Apache License 2.0 4 votes vote down vote up
@Test
public void testDocFields() throws Exception {
    String indexName = "test-doc-fields";
    Index index = searchService.getIndex(IndexSpec.newBuilder().setName(indexName));
    delDocs(index);

    Builder docBuilder = Document.newBuilder();
    Field field = Field.newBuilder().setName("textfield").setText("text field").build();
    docBuilder.addField(field);
    field = Field.newBuilder().setName("numberfield").setNumber(123).build();
    docBuilder.addField(field);
    Date now = new Date();
    field = Field.newBuilder().setName("datefield").setDate(now).build();
    docBuilder.addField(field);
    field = Field.newBuilder().setName("htmlfield").setHTML("<html>html field</html>").build();
    docBuilder.addField(field);
    User currentUser = new User("[email protected]", "appenginetest.com");
    field = Field.newBuilder().setName("atomfield").setAtom(currentUser.getAuthDomain()).build();
    docBuilder.addField(field);
    GeoPoint geoPoint = new GeoPoint((double) -10, 10.000001);
    field = Field.newBuilder().setName("geofield").setGeoPoint(geoPoint).build();
    docBuilder.addField(field);
    index.put(docBuilder);
    sync();

    Results<ScoredDocument> result = searchDocs(index, "", 0);
    assertEquals(1, result.getNumberReturned());
    ScoredDocument doc = result.iterator().next();
    Field retField = doc.getOnlyField("textfield");
    assertEquals(FieldType.TEXT, retField.getType());
    assertEquals("textfield", retField.getName());
    assertEquals("text field", retField.getText());

    retField = doc.getOnlyField("numberfield");
    assertEquals(FieldType.NUMBER, retField.getType());
    assertEquals(new Double("123"), retField.getNumber());

    retField = doc.getOnlyField("datefield");
    assertEquals(FieldType.DATE, retField.getType());
    assertEquals(now, retField.getDate());

    retField = doc.getOnlyField("htmlfield");
    assertEquals(FieldType.HTML, retField.getType());
    assertEquals("<html>html field</html>", retField.getHTML());

    retField = doc.getOnlyField("atomfield");
    assertEquals(FieldType.ATOM, retField.getType());
    assertEquals(currentUser.getAuthDomain(), retField.getAtom());

    retField = doc.getOnlyField("geofield");
    assertEquals(FieldType.GEO_POINT, retField.getType());
    assertEquals(-10, retField.getGeoPoint().getLatitude(), 0);
    assertEquals(10.000001, retField.getGeoPoint().getLongitude(), 0.000000);
}
 
Example #30
Source File: SearchManager.java    From teammates with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Searches document by the given query.
 */
public static Results<ScoredDocument> searchDocuments(String indexName, Query query) {
    return getIndex(indexName).search(query);
}