Index (Google App Engine API for Java)

com.google.appengine.api.search

Interface Index



  • public interface Index
    An Index allows synchronous and asynchronous adding and deleting of Documents as well as synchronous and asynchronous searching for Documents for a given Query. The following code fragment shows how to add documents, then search the index for documents matching a query.

      // Get the SearchService for the default namespace
      SearchService searchService = SearchServiceFactory.getSearchService();
      // Get the index. If not yet created, create it.
      Index index = searchService.getIndex(
          IndexSpec.newBuilder().setIndexName("indexName"));
    
      // Create a document.
      Document document = Document.newBuilder()
          .setId("documentId")
          .addField(Field.newBuilder().setName("subject").setText("my first email"))
          .addField(Field.newBuilder().setName("body")
               .setHTML("<html>some content here</html>")
          .build();
    
      // Put the document.
      try {
        index.put(document);
      } catch (PutException e) {
        if (StatusCode.TRANSIENT_ERROR.equals(e.getOperationResult().getCode())) {
          // retry putting document
        }
      }
    
      // Query the index.
      try {
        Results<ScoredDocument> results =
            index.search(Query.newBuilder().build("subject:first body:here"));
    
        // Iterate through the search results.
        for (ScoredDocument document : results) {
          // display results
        }
      } catch (SearchException e) {
        if (StatusCode.TRANSIENT_ERROR.equals(e.getOperationResult().getCode())) {
          // retry
        }
      }