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

The following examples show how to use com.google.appengine.api.search.Index. 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: Utils.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
/**
 * Put a given document into an index with the given indexName.
 *
 * @param indexName The name of the index.
 * @param document A document to add.
 * @throws InterruptedException When Thread.sleep is interrupted.
 */
// [START putting_document_with_retry]
public static void indexADocument(String indexName, Document document)
    throws InterruptedException {
  IndexSpec indexSpec = IndexSpec.newBuilder().setName(indexName).build();
  Index index = SearchServiceFactory.getSearchService().getIndex(indexSpec);

  final int maxRetry = 3;
  int attempts = 0;
  int delay = 2;
  while (true) {
    try {
      index.put(document);
    } catch (PutException e) {
      if (StatusCode.TRANSIENT_ERROR.equals(e.getOperationResult().getCode())
          && ++attempts < maxRetry) { // retrying
        Thread.sleep(delay * 1000);
        delay *= 2; // easy exponential backoff
        continue;
      } else {
        throw e; // otherwise throw
      }
    }
    break;
  }
}
 
Example #2
Source File: IndexTest.java    From appengine-tck with Apache License 2.0 6 votes vote down vote up
private Index createIndex(String indexName, String docId) {
    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);

    field = Field.newBuilder().setName("subject").setText("put(Document)").build();
    Document document = Document.newBuilder()
            .setId(docId + "2")
            .addField(field).build();
    index.put(document);
    return index;
}
 
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: 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 #5
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 #6
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 #7
Source File: UtilsTest.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
@Test
public void indexADocument_successfullyInvoked() throws Exception {
  String id = "test";
  Document doc =
      Document.newBuilder()
          .setId(id)
          .addField(Field.newBuilder().setName("f").setText("v"))
          .build();
  Utils.indexADocument(INDEX, doc);
  // get the document by id
  IndexSpec indexSpec = IndexSpec.newBuilder().setName(INDEX).build();
  Index index = SearchServiceFactory.getSearchService().getIndex(indexSpec);
  Document fetched = index.get(id);
  assertWithMessage("A value of the fetched document")
      .that(fetched.getOnlyField("f").getText())
      .isEqualTo("v");
}
 
Example #8
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 #9
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 #10
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 #11
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 #12
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 #13
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 #14
Source File: SearchServiceTest.java    From appengine-tck with Apache License 2.0 6 votes vote down vote up
private Index createIndex(String indexName, String docId) {
    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);

    field = Field.newBuilder().setName("subject").setText("put(Document)").build();
    Document document = Document.newBuilder()
            .setId(docId + "2")
            .addField(field).build();
    index.put(document);
    return index;
}
 
Example #15
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 #16
Source File: GallerySearchIndex.java    From appinventor-extensions with Apache License 2.0 6 votes vote down vote up
/**
 * index gallery app into search index
 * @param app galleryapp
 */
public void indexApp (GalleryApp app) {
  // take the title, description, and the user name and index it
  // need to build up a string with all meta data
  String indexWords = app.getTitle()+" "+app.getDescription() + " " + app.getDeveloperName();
  // now create the doc
  Document doc = Document.newBuilder()
    .setId(String.valueOf(app.getGalleryAppId()))
    .addField(Field.newBuilder().setName("content").setText(indexWords))
    .build();

  Index index = getIndex();

  try {
    index.put(doc);
  } catch (PutException e) {
    if (StatusCode.TRANSIENT_ERROR.equals(e.getOperationResult().getCode())) {
        // retry putting the document
    }
  }
}
 
Example #17
Source File: IndexTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
private void addData(String indexName) throws InterruptedException, ParseException {
    Index index;
    index = searchService.getIndex(IndexSpec.newBuilder().setName(indexName + "3"));
    delDocs(index);
    addDocs(index, 3);
    index = searchService.getIndex(IndexSpec.newBuilder().setName(indexName + "7"));
    delDocs(index);
    addDocs(index, 7);
}
 
Example #18
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 #19
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 #20
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 #21
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 #22
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 #23
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 #24
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 #25
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 #26
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 #27
Source File: SearchTestBase.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
protected void addDocs(Index index, int docCount) throws ParseException, InterruptedException {
    if (searchDocs(index, "", 0).getNumberFound() == 0) {
        List<Document> documents = new ArrayList<>();
        Calendar cal = Calendar.getInstance();
        DateFormat dfDate = new SimpleDateFormat("yyyy,M,d");
        for (int i = 0; i < docCount; i++) {
            Builder docBuilder = Document.newBuilder();
            // two text field with different locale
            docBuilder.addField(Field.newBuilder().setName("textfield").setText("text with num " + i));
            Field field = Field.newBuilder().setName("textfield").setText("C'est la vie " + i).setLocale(Locale.FRENCH).build();
            docBuilder.addField(field);
            docBuilder.addField(Field.newBuilder().setName("numfield").setNumber(i));
            String dateVal = "" + cal.get(Calendar.YEAR) + ",";
            dateVal += cal.get(Calendar.MONTH) + ",";
            int day = cal.get(Calendar.DATE) + i;
            dateVal += day;
            docBuilder.addField(Field.newBuilder().setName("datefield").setDate(dfDate.parse(dateVal)));
            docBuilder.addField(Field.newBuilder().setName("htmlfield").setHTML("<B>html</B> " + i));
            docBuilder.addField(Field.newBuilder().setName("atomfield").setAtom("atom" + i + ".com"));
            GeoPoint geoPoint = new GeoPoint((double) i, (double) (100 + i));
            docBuilder.addField(Field.newBuilder().setName("geofield").setGeoPoint(geoPoint));
            // two field in same name and with different field type
            docBuilder.addField(Field.newBuilder().setName("mixfield").setText("text and number mix field"));
            docBuilder.addField(Field.newBuilder().setName("mixfield").setNumber(987));
            docBuilder.setId("selfid" + i);
            // only doc(id="selfid0") has "cn" locale, others have "en" locale
            if (i == 0) {
                docBuilder.setLocale(new Locale("cn"));
            } else {
                docBuilder.setLocale(new Locale("en"));
            }
            documents.add(docBuilder.build());
        }
        index.put(documents);
        sync();
    }
}
 
Example #28
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 #29
Source File: SearchServlet.java    From appengine-java-vm-runtime with Apache License 2.0 5 votes vote down vote up
public void addDocument(Index index, String docid) {
  Document.Builder builder = Document.newBuilder();
  builder.setId(docid);
  builder.setRank(docid.hashCode());

  Field.Builder field = Field.newBuilder();
  field.setName("title");
  field.setText(String.format("Title: title%%<%s>", docid));
  builder.addField(field);

  field = Field.newBuilder();
  field.setName("body");
  field.setHTML(String.format("<h3>body of %s, some string</h3>", docid));
  builder.addField(field);

  field = Field.newBuilder();
  field.setName("atom");
  field.setAtom(String.format("atom%% <%s>", docid));
  builder.addField(field);

  field = Field.newBuilder();
  field.setName("number");
  field.setNumber(docid.hashCode() % 4096);
  builder.addField(field);

  field = Field.newBuilder();
  field.setName("date");
  field.setDate(new Date(2011 - 1900, 11 - 1, (docid.hashCode() % 30) + 1));
  builder.addField(field);

  index.put(builder.build());
}
 
Example #30
Source File: SearchManager.java    From teammates with GNU General Public License v2.0 5 votes vote down vote up
private static Map<String, Index> getIndicesTable() {
    Map<String, Index> indicesTable = PER_THREAD_INDICES_TABLE.get();
    if (indicesTable == null) {
        indicesTable = new HashMap<>();
        PER_THREAD_INDICES_TABLE.set(indicesTable);
    }
    return indicesTable;
}