Stay organized with collections
Save and categorize content based on your preferences.
publicinterfaceIndex
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 namespaceSearchServicesearchService=SearchServiceFactory.getSearchService();// Get the index. If not yet created, create it.Indexindex=searchService.getIndex(IndexSpec.newBuilder().setIndexName("indexName"));// Create a document.Documentdocument=Document.newBuilder().setId("documentId").addField(Field.newBuilder().setName("subject").setText("my first email")).addField(Field.newBuilder().setName("body").setHTML(somecontenthere") .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:firstbody:here")); // Iterate through the search results. for (ScoredDocument document : results) { // display results } } catch (SearchException e) { if (StatusCode.TRANSIENT_ERROR.equals(e.getOperationResult().getCode())) { // retry } }
Delete the schema from the index. To fully delete an index, you must delete both the index's
documents and schema. This method deletes the index's schema, which contains field names and
field types of previously indexed documents.
an PutResponse containing the result of the put operations indicating success
or failure as well as the document ids. The search service will allocate document ids for
documents which have none provided
Search the index for documents matching the query. The query must specify a query string, and
optionally, how many documents are requested, how the results are to be sorted, scored and
which fields are to be returned.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-09-03 UTC."],[[["\u003cp\u003eThe \u003ccode\u003eIndex\u003c/code\u003e interface facilitates the addition, deletion, and searching of \u003ccode\u003eDocuments\u003c/code\u003e, both synchronously and asynchronously, within a specified index.\u003c/p\u003e\n"],["\u003cp\u003eDocuments can be added to an index using the \u003ccode\u003eput\u003c/code\u003e method and retrieved individually by their \u003ccode\u003edocumentId\u003c/code\u003e using the \u003ccode\u003eget\u003c/code\u003e method, or in ranges using the \u003ccode\u003egetRange\u003c/code\u003e methods.\u003c/p\u003e\n"],["\u003cp\u003eDocuments can be searched within the index by providing a \u003ccode\u003eQuery\u003c/code\u003e object or a query string to the \u003ccode\u003esearch\u003c/code\u003e methods, returning \u003ccode\u003eScoredDocument\u003c/code\u003e results.\u003c/p\u003e\n"],["\u003cp\u003eDocuments can be removed from the index using the \u003ccode\u003edelete\u003c/code\u003e methods, or remove the schema using \u003ccode\u003edeleteSchema\u003c/code\u003e methods, and these actions are available both synchronously and asynchronously.\u003c/p\u003e\n"],["\u003cp\u003eMethods exist to get storage limit and usage in bytes, and access the namespace and schema of the current index.\u003c/p\u003e\n"]]],[],null,["# Interface Index (2.0.0)\n\n public interface Index\n\nAn Index allows synchronous and asynchronous adding and deleting of [Documents](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.search.Document) as\nwell as synchronous and asynchronous searching for Documents for a given [Query](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.search.Query(class)). The\nfollowing code fragment shows how to add documents, then search the index for documents matching\na query.\n\n\n\n // Get the SearchService for the default namespace\n SearchService searchService = SearchServiceFactory.getSearchService();\n // Get the index. If not yet created, create it.\n Index index = searchService.getIndex(\n IndexSpec.newBuilder().setIndexName(\"indexName\"));\n\n // Create a document.\n Document document = Document.newBuilder()\n .setId(\"documentId\")\n .addField(Field.newBuilder().setName(\"subject\").setText(\"my first email\"))\n .addField(Field.newBuilder().setName(\"body\")\n .setHTML(some content here\")\n .build();\n\n // Put the document.\n try {\n index.put(document);\n } catch (PutException e) {\n if (StatusCode.TRANSIENT_ERROR.equals(e.getOperationResult().getCode())) {\n // retry putting document\n }\n }\n\n // Query the index.\n try {\n Results\u003cScoredDocument\u003e results =\n index.search(Query.newBuilder().build(\"subject:first body:here\"));\n\n // Iterate through the search results.\n for (ScoredDocument document : results) {\n // display results\n }\n } catch (SearchException e) {\n if (StatusCode.TRANSIENT_ERROR.equals(e.getOperationResult().getCode())) {\n // retry\n }\n }\n \n \nMethods\n-------\n\n### delete(Iterable\\\u003cString\\\u003e documentIds)\n\n public abstract void delete(Iterable\u003cString\u003e documentIds)\n\nSee Also: [#delete(String...)](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.search.Index#com_google_appengine_api_search_Index_delete_)\n\n### delete(String\\[\\] documentIds)\n\n public abstract void delete(String[] documentIds)\n\nDelete documents for the given document ids from the index if they are in the index.\n\n### deleteAsync(Iterable\\\u003cString\\\u003e documentIds)\n\n public abstract Future\u003cVoid\u003e deleteAsync(Iterable\u003cString\u003e documentIds)\n\nSee Also: [#delete(String...)](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.search.Index#com_google_appengine_api_search_Index_delete_)\n\n### deleteAsync(String\\[\\] documentId)\n\n public abstract Future\u003cVoid\u003e deleteAsync(String[] documentId)\n\nSee Also: [#delete(String...)](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.search.Index#com_google_appengine_api_search_Index_delete_)\n\n### deleteSchema()\n\n public abstract void deleteSchema()\n\nDelete the schema from the index. To fully delete an index, you must delete both the index's\ndocuments and schema. This method deletes the index's schema, which contains field names and\nfield types of previously indexed documents.\n\n### deleteSchemaAsync()\n\n public abstract Future\u003cVoid\u003e deleteSchemaAsync()\n\nSee Also: [#deleteSchema()](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.search.Index#com_google_appengine_api_search_Index_deleteSchema__)\n\n### get(String documentId)\n\n public abstract Document get(String documentId)\n\nGets a [Document](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.search.Document) for the given document Id.\n\n### getName()\n\n public abstract String getName()\n\n### getNamespace()\n\n public abstract String getNamespace()\n\n### getRange(GetRequest request)\n\n public abstract GetResponse\u003cDocument\u003e getRange(GetRequest request)\n\nGet an index's documents, in document Id order.\n\n### getRange(GetRequest.Builder builder)\n\n public abstract GetResponse\u003cDocument\u003e getRange(GetRequest.Builder builder)\n\nSee Also: [#getRange(GetRequest)](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.search.Index#com_google_appengine_api_search_Index_getRange_com_google_appengine_api_search_GetRequest_)\n\n### getRangeAsync(GetRequest request)\n\n public abstract Future\u003cGetResponse\u003cDocument\u003e\u003e getRangeAsync(GetRequest request)\n\nSee Also: [#getRange(GetRequest)](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.search.Index#com_google_appengine_api_search_Index_getRange_com_google_appengine_api_search_GetRequest_)\n\n### getRangeAsync(GetRequest.Builder builder)\n\n public abstract Future\u003cGetResponse\u003cDocument\u003e\u003e getRangeAsync(GetRequest.Builder builder)\n\nSee Also: [#getRange(GetRequest)](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.search.Index#com_google_appengine_api_search_Index_getRange_com_google_appengine_api_search_GetRequest_)\n\n### getSchema()\n\n public abstract Schema getSchema()\n\n### getStorageLimit()\n\n public abstract long getStorageLimit()\n\n### getStorageUsage()\n\n public abstract long getStorageUsage()\n\n### put(Document\\[\\] documents)\n\n public abstract PutResponse put(Document[] documents)\n\nPut the documents into the index, updating any document that is already present.\n\n### put(Document.Builder\\[\\] builders)\n\n public abstract PutResponse put(Document.Builder[] builders)\n\nSee Also: [#put(Document...)](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.search.Index#com_google_appengine_api_search_Index_put_)\n\n### put(Iterable\\\u003cDocument\\\u003e documents)\n\n public abstract PutResponse put(Iterable\u003cDocument\u003e documents)\n\nSee Also: [#put(Document...)](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.search.Index#com_google_appengine_api_search_Index_put_)\n\n### putAsync(Document\\[\\] document)\n\n public abstract Future\u003cPutResponse\u003e putAsync(Document[] document)\n\nSee Also: [#put(Document...)](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.search.Index#com_google_appengine_api_search_Index_put_)\n\n### putAsync(Document.Builder\\[\\] document)\n\n public abstract Future\u003cPutResponse\u003e putAsync(Document.Builder[] document)\n\nSee Also: [#put(Document...)](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.search.Index#com_google_appengine_api_search_Index_put_)\n\n### putAsync(Iterable\\\u003cDocument\\\u003e documents)\n\n public abstract Future\u003cPutResponse\u003e putAsync(Iterable\u003cDocument\u003e documents)\n\nSee Also: [#put(Document...)](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.search.Index#com_google_appengine_api_search_Index_put_)\n\n### search(Query query)\n\n public abstract Results\u003cScoredDocument\u003e search(Query query)\n\nSearch the index for documents matching the query. The query must specify a query string, and\noptionally, how many documents are requested, how the results are to be sorted, scored and\nwhich fields are to be returned.\n\n### search(String query)\n\n public abstract Results\u003cScoredDocument\u003e search(String query)\n\nSearch the index for documents matching the query string.\nSee Also: [#search(Query)](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.search.Index#com_google_appengine_api_search_Index_search_com_google_appengine_api_search_Query_)\n\n### searchAsync(Query query)\n\n public abstract Future\u003cResults\u003cScoredDocument\u003e\u003e searchAsync(Query query)\n\nSee Also: [#search(Query)](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.search.Index#com_google_appengine_api_search_Index_search_com_google_appengine_api_search_Query_)\n\n### searchAsync(String query)\n\n public abstract Future\u003cResults\u003cScoredDocument\u003e\u003e searchAsync(String query)\n\nSee Also: [#search(String)](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.search.Index#com_google_appengine_api_search_Index_search_java_lang_String_)"]]