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

The following examples show how to use com.google.appengine.api.search.GetResponse. 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: MaintenanceTasksServlet.java    From MobileShoppingAssistant-sample with Apache License 2.0 6 votes vote down vote up
/**
 * Cleans the index of places from all entries.
 */
private void removeAllDocumentsFromIndex() {
    Index index = PlacesHelper.getIndex();
    // As the request will only return up to 1000 documents,
    // we need to loop until there are no more documents in the index.
    // We batch delete 1000 documents per iteration.
    final int numberOfDocuments = 1000;
    while (true) {
        GetRequest request = GetRequest.newBuilder()
                .setReturningIdsOnly(true)
                .build();

        ArrayList<String> documentIds = new ArrayList<>(numberOfDocuments);
        GetResponse<Document> response = index.getRange(request);
        for (Document document : response.getResults()) {
            documentIds.add(document.getId());
        }

        if (documentIds.size() == 0) {
            break;
        }

        index.delete(documentIds);
    }
}
 
Example #2
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 #3
Source File: IndexTest.java    From appengine-tck with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetIndexes() throws InterruptedException, ParseException {
    String indexName = "indextest";
    addData(indexName);
    GetIndexesRequest request = GetIndexesRequest.newBuilder()
        .setIndexNamePrefix(indexName)
        .setOffset(0)
        .setLimit(10)
        .build();
    GetResponse<Index> response = searchService.getIndexes(request);
    List<Index> listIndexes = response.getResults();
    assertEquals(2, listIndexes.size());

    for (Index oneIndex : listIndexes) {
        String name = oneIndex.getName();
        assertTrue(name.startsWith(indexName));
        verifyDocCount(oneIndex, -1);
    }
}
 
Example #4
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 #5
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 #6
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 #7
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 #8
Source File: IndexTest.java    From appengine-tck with Apache License 2.0 6 votes vote down vote up
@Test
public void testPutGetRangeGetRequest() throws 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) {
        GetResponse<Document> docs = oneIndex.getRange(GetRequest.newBuilder().setStartId(docId + "1").setLimit(10).build());
        sync();
        assertEquals(docs.getResults().size(), 2);
    }
}
 
Example #9
Source File: IndexTest.java    From appengine-tck with Apache License 2.0 6 votes vote down vote up
@Test
public void testPutGetRangeBuilder() throws 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) {
        GetResponse<Document> docs = oneIndex.getRange(GetRequest.newBuilder().setStartId(docId + "1").setLimit(10));
        sync();
        assertEquals(docs.getResults().size(), 2);
    }
}
 
Example #10
Source File: IndexTest.java    From appengine-tck with Apache License 2.0 6 votes vote down vote up
@Test
public void testPutGetRangeAsyncGetResponse() throws InterruptedException, ExecutionException {
    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<GetResponse<Document>> futurDocs = oneIndex.getRangeAsync(GetRequest.newBuilder().setStartId(docId + "1").setLimit(10).build());
        sync();
        assertEquals(futurDocs.get().getResults().size(), 2);
    }
}
 
Example #11
Source File: IndexTest.java    From appengine-tck with Apache License 2.0 6 votes vote down vote up
@Test
public void testPutGetRangeAsyncBuilder() throws InterruptedException, ExecutionException {
    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<GetResponse<Document>> futurDocs = oneIndex.getRangeAsync(GetRequest.newBuilder().setStartId(docId + "1").setLimit(10));
        sync();
        assertEquals(futurDocs.get().getResults().size(), 2);
    }
}
 
Example #12
Source File: MaintenanceTasksServlet.java    From solutions-mobile-shopping-assistant-backend-java with Apache License 2.0 5 votes vote down vote up
private void removeAllDocumentsFromIndex() {
  Index index = PlacesHelper.getIndex();

  GetRequest request = GetRequest.newBuilder().setReturningIdsOnly(true).build();

  GetResponse<Document> response = index.getRange(request);
  for (Document document : response.getResults()) {
    index.delete(document.getId());
  }
}
 
Example #13
Source File: IndexTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@Test
public void testPutAsyncDocument() {
    String indexName = "put-index";
    String docId = "testPutDocs";

    Index index = searchService.getIndex(IndexSpec.newBuilder()
            .setName(indexName)
            .build());

    Field field = Field.newBuilder().setName("subject").setText("put(Document)").build();
    Document document = Document.newBuilder()
            .setId(docId + "1")
            .addField(field).build();

    Future<PutResponse> resp = index.putAsync(document);

    while (!resp.isDone()) {
        if (resp.isCancelled()) {
            break;
        }
    }

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

    for (Index oneIndex : listIndexes) {
        Field retField = oneIndex.get(docId + "1").getOnlyField("subject");
        sync();
        assertEquals("put(Document)", retField.getText());
    }
}
 
Example #14
Source File: IndexTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@Test
public void testPutAsyncBuilder() {
    String indexName = "put-index";
    String docId = "testPutDocs";

    Index index = searchService.getIndex(IndexSpec.newBuilder()
            .setName(indexName)
            .build());

    Field field = Field.newBuilder().setName("subject").setText("put(Document.Builder)").build();
    Document.Builder docBuilder = Document.newBuilder()
            .setId(docId + "1")
            .addField(field);

    index.putAsync(docBuilder);

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

    for (Index oneIndex : listIndexes) {
        Field retField = oneIndex.get(docId + "1").getOnlyField("subject");
        sync();
        assertEquals("put(Document.Builder)", retField.getText());
    }
}
 
Example #15
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 #16
Source File: IndexTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@Test
public void testPutDeleteAsyncDocsByString() throws InterruptedException {
    String indexName = "put-index";
    String docId = "testPutDocs";
    List<String> docIdList = new ArrayList<>();
    Index index = searchService.getIndex(IndexSpec.newBuilder()
            .setName(indexName)
            .build());

    Field field = Field.newBuilder().setName("subject").setText("put(Document.Builder)").build();
    Document.Builder docBuilder = Document.newBuilder()
            .setId(docId + "1")
            .addField(field);
    index.put(docBuilder);
    docIdList.add(docId + "1");

    field = Field.newBuilder().setName("subject").setText("put(Document)").build();
    Document document = Document.newBuilder()
            .setId(docId + "2")
            .addField(field).build();
    index.put(document);
    docIdList.add(docId + "1");

    GetIndexesRequest request = GetIndexesRequest.newBuilder()
            .setIndexNamePrefix(indexName)
            .build();
    GetResponse<Index> response = searchService.getIndexes(request);
    List<Index> listIndexes = response.getResults();
    for (Index oneIndex : listIndexes) {
        Field retField = oneIndex.get(docId + "1").getOnlyField("subject");
        assertEquals("put(Document.Builder)", retField.getText());
        retField = oneIndex.get(docId + "2").getOnlyField("subject");
        assertEquals("put(Document)", retField.getText());
        oneIndex.deleteAsync(docIdList.get(0));
        sync();
        assertNull(oneIndex.get(docIdList.get(0)));
    }
}
 
Example #17
Source File: IndexTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@Test
public void testPutDeleteDocs() throws InterruptedException {
    String indexName = "put-index";
    String docId = "testPutDocs";
    List<String> docIdList = new ArrayList<>();
    Index index = searchService.getIndex(IndexSpec.newBuilder()
        .setName(indexName)
        .build());

    Field field = Field.newBuilder().setName("subject").setText("put(Document.Builder)").build();
    Document.Builder docBuilder = Document.newBuilder()
        .setId(docId + "1")
        .addField(field);
    index.put(docBuilder);
    docIdList.add(docId + "1");

    field = Field.newBuilder().setName("subject").setText("put(Document)").build();
    Document document = Document.newBuilder()
        .setId(docId + "2")
        .addField(field).build();
    index.put(document);
    docIdList.add(docId + "1");

    GetIndexesRequest request = GetIndexesRequest.newBuilder()
        .setIndexNamePrefix(indexName)
        .build();
    GetResponse<Index> response = searchService.getIndexes(request);
    List<Index> listIndexes = response.getResults();
    for (Index oneIndex : listIndexes) {
        Field retField = oneIndex.get(docId + "1").getOnlyField("subject");
        assertEquals("put(Document.Builder)", retField.getText());
        retField = oneIndex.get(docId + "2").getOnlyField("subject");
        assertEquals("put(Document)", retField.getText());
        oneIndex.delete(docIdList.get(0));
        sync();
        assertNull(oneIndex.get(docIdList.get(0)));
    }
}
 
Example #18
Source File: IndexTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
public void testNamespace() throws InterruptedException, ParseException {
    String ns = "ns-indextest";
    String indexName = "ns-index";
    int docCount = 5;
    NamespaceManager.set(ns);
    Index index = searchService.getIndex(IndexSpec.newBuilder()
            .setName(indexName)
            .build());
    delDocs(index);
    addDocs(index, docCount);

    GetIndexesRequest request = GetIndexesRequest.newBuilder()
        .setIndexNamePrefix(indexName)
        .setOffset(0)
        .setNamespace(ns)
        .setLimit(10)
        .build();
    assertEquals(ns, request.getNamespace());
    GetResponse<Index> response = searchService.getIndexes(request);
    List<Index> listIndexes = response.getResults();
    for (Index oneIndex : listIndexes) {
        assertEquals(ns, listIndexes.get(0).getNamespace());
        assertEquals(indexName, listIndexes.get(0).getName());
        verifyDocCount(oneIndex, docCount);
    }
    assertEquals(ns, searchService.getNamespace());
    NamespaceManager.set("");
}
 
Example #19
Source File: IndexTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@Test
public void testNamespaceWithBug() throws InterruptedException, ParseException {
    String ns = "ns-indextest";
    String indexName = "ns-index";
    int docCount = 5;
    NamespaceManager.set(ns);
    SearchService searchService2 = SearchServiceFactory.getSearchService();
    Index index = searchService2.getIndex(IndexSpec.newBuilder()
            .setName(indexName)
            .build());
    delDocs(index);
    addDocs(index, docCount);

    GetIndexesRequest request = GetIndexesRequest.newBuilder()
        .setIndexNamePrefix(indexName)
        .setOffset(0)
        .setNamespace(ns)
        .setLimit(10)
        .build();
    assertEquals(ns, request.getNamespace());
    GetResponse<Index> response = searchService2.getIndexes(request);
    List<Index> listIndexes = response.getResults();
    for (Index oneIndex : listIndexes) {
        assertEquals(ns, listIndexes.get(0).getNamespace());
        assertEquals(indexName, listIndexes.get(0).getName());
        verifyDocCount(oneIndex, docCount);
    }
    assertEquals(ns, searchService2.getNamespace());
    NamespaceManager.set("");
}
 
Example #20
Source File: SchemaServlet.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
  PrintWriter out = resp.getWriter();
  Document doc =
      Document.newBuilder()
          .setId("theOnlyCar")
          .addField(Field.newBuilder().setName("maker").setText("Toyota"))
          .addField(Field.newBuilder().setName("price").setNumber(300000))
          .addField(Field.newBuilder().setName("color").setText("lightblue"))
          .addField(Field.newBuilder().setName("model").setText("Prius"))
          .build();
  try {
    Utils.indexADocument(SEARCH_INDEX, doc);
  } catch (InterruptedException e) {
    // ignore
  }
  // [START list_schema]
  GetResponse<Index> response =
      SearchServiceFactory.getSearchService()
          .getIndexes(GetIndexesRequest.newBuilder().setSchemaFetched(true).build());

  // List out elements of each Schema
  for (Index index : response) {
    Schema schema = index.getSchema();
    for (String fieldName : schema.getFieldNames()) {
      List<FieldType> typesForField = schema.getFieldTypes(fieldName);
      // Just printing out the field names and types
      for (FieldType type : typesForField) {
        out.println(index.getName() + ":" + fieldName + ":" + type.name());
      }
    }
  }
  // [END list_schema]
}
 
Example #21
Source File: IndexServlet.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
  PrintWriter out = resp.getWriter();
  Document document =
      Document.newBuilder()
          .setId("AZ125")
          .addField(Field.newBuilder().setName("myField").setText("myValue"))
          .build();
  try {
    Utils.indexADocument(INDEX, document);
  } catch (InterruptedException e) {
    out.println("Interrupted");
    return;
  }
  out.println("Indexed a new document.");
  // [START get_document]
  IndexSpec indexSpec = IndexSpec.newBuilder().setName(INDEX).build();
  Index index = SearchServiceFactory.getSearchService().getIndex(indexSpec);

  // Fetch a single document by its  doc_id
  Document doc = index.get("AZ125");

  // Fetch a range of documents by their doc_ids
  GetResponse<Document> docs =
      index.getRange(GetRequest.newBuilder().setStartId("AZ125").setLimit(100).build());
  // [END get_document]
  out.println("myField: " + docs.getResults().get(0).getOnlyField("myField").getText());
}
 
Example #22
Source File: IndexTest.java    From appengine-tck with Apache License 2.0 4 votes vote down vote up
@Test
public void testPutAsyncIterable() {
    String indexName = "put-index";
    String docId = "testPutDocs";

    Index index = searchService.getIndex(IndexSpec.newBuilder()
            .setName(indexName)
            .build());

    List<Document> documents = new ArrayList<>();
    Field field = Field.newBuilder().setName("subject").setText("put(Document)").build();
    Document document1 = Document.newBuilder()
            .setId(docId + "1")
            .addField(field).build();
    field = Field.newBuilder().setName("subject").setText("put(Document)").build();
    Document document2 = Document.newBuilder()
            .setId(docId + "2")
            .addField(field).build();

    documents.add(document1);
    documents.add(document2);

    Future<PutResponse> resp = index.putAsync(documents);

    while (!resp.isDone()) {
        if (resp.isCancelled()) {
            break;
        }
    }

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

    for (Index oneIndex : listIndexes) {
        Field retField = oneIndex.get(docId + "1").getOnlyField("subject");
        assertEquals("put(Document)", retField.getText());
        retField = oneIndex.get(docId + "2").getOnlyField("subject");
        assertEquals("put(Document)", retField.getText());
        sync();
    }
}