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(some content here")
.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
}
}
Methods
delete(Iterable<String> documentIds)
public abstract void delete(Iterable<String> documentIds)
delete(String[] documentIds)
public abstract void delete(String[] documentIds)
Delete documents for the given document ids from the index if they are in the index.
Parameter |
Name |
Description |
documentIds |
String[]
the ids of documents to delete
|
deleteAsync(Iterable<String> documentIds)
public abstract Future<Void> deleteAsync(Iterable<String> documentIds)
deleteAsync(String[] documentId)
public abstract Future<Void> deleteAsync(String[] documentId)
Parameter |
Name |
Description |
documentId |
String[]
|
deleteSchema()
public abstract void deleteSchema()
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.
deleteSchemaAsync()
public abstract Future<Void> deleteSchemaAsync()
get(String documentId)
public abstract Document get(String documentId)
Gets a Document for the given document Id.
Parameter |
Name |
Description |
documentId |
String
the identifier for the document to retrieve
|
getName()
public abstract String getName()
Returns |
Type |
Description |
String |
the name of the index
|
getNamespace()
public abstract String getNamespace()
Returns |
Type |
Description |
String |
the namespace of the index name
|
getRange(GetRequest request)
public abstract GetResponse<Document> getRange(GetRequest request)
Get an index's documents, in document Id order.
Parameter |
Name |
Description |
request |
GetRequest
contains various options restricting which documents are returned.
|
getRange(GetRequest.Builder builder)
public abstract GetResponse<Document> getRange(GetRequest.Builder builder)
getRangeAsync(GetRequest request)
public abstract Future<GetResponse<Document>> getRangeAsync(GetRequest request)
getRangeAsync(GetRequest.Builder builder)
public abstract Future<GetResponse<Document>> getRangeAsync(GetRequest.Builder builder)
getSchema()
public abstract Schema getSchema()
getStorageLimit()
public abstract long getStorageLimit()
Returns |
Type |
Description |
long |
the maximum amount of storage space that the application may use in this index,
expressed in bytes
|
getStorageUsage()
public abstract long getStorageUsage()
Returns |
Type |
Description |
long |
a rough approximation of the amount of storage space currently used by this index,
expressed in bytes
|
put(Document[] documents)
public abstract PutResponse put(Document[] documents)
Put the documents into the index, updating any document that is already present.
Parameter |
Name |
Description |
documents |
Document[]
the documents to put into the index
|
Returns |
Type |
Description |
PutResponse |
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
|
put(Document.Builder[] builders)
public abstract PutResponse put(Document.Builder[] builders)
Parameter |
Name |
Description |
builders |
Builder[]
|
put(Iterable<Document> documents)
public abstract PutResponse put(Iterable<Document> documents)
putAsync(Document[] document)
public abstract Future<PutResponse> putAsync(Document[] document)
Parameter |
Name |
Description |
document |
Document[]
|
putAsync(Document.Builder[] document)
public abstract Future<PutResponse> putAsync(Document.Builder[] document)
Parameter |
Name |
Description |
document |
Builder[]
|
putAsync(Iterable<Document> documents)
public abstract Future<PutResponse> putAsync(Iterable<Document> documents)
search(Query query)
public abstract Results<ScoredDocument> search(Query query)
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.
Parameter |
Name |
Description |
query |
Query
the fully specified Query object
|
search(String query)
public abstract Results<ScoredDocument> search(String query)
Search the index for documents matching the query string.
See Also: #search(Query)
Parameter |
Name |
Description |
query |
String
the query string
|
searchAsync(Query query)
public abstract Future<Results<ScoredDocument>> searchAsync(Query query)
Parameter |
Name |
Description |
query |
Query
|
searchAsync(String query)
public abstract Future<Results<ScoredDocument>> searchAsync(String query)
Parameter |
Name |
Description |
query |
String
|