1 // Copyright 2010 Google Inc. All Rights Reserved.
3 package com
.google
.appengine
.api
.search
;
5 import com
.google
.appengine
.api
.search
.checkers
.IndexChecker
;
7 import java
.util
.concurrent
.Future
;
10 * An Index allows synchronous and asynchronous adding and deleting of
11 * {@link Document Documents} as well as synchronous and asynchronous
12 * searching for Documents for a given {@link Query}. The following
13 * code fragment shows how to add documents, then search the index for
14 * documents matching a query.
17 * // Get the SearchService for the default namespace
18 * SearchService searchService = SearchServiceFactory.getSearchService();
19 * // Get the index. If not yet created, create it.
20 * Index index = searchService.getIndex(
21 * IndexSpec.newBuilder().setIndexName("indexName"));
23 * // Create a document.
24 * Document document = Document.newBuilder()
25 * .setId("documentId")
26 * .addField(Field.newBuilder().setName("subject").setText("my first email"))
27 * .addField(Field.newBuilder().setName("body")
28 * .setHTML("<html>some content here</html>")
31 * // Put the document.
33 * index.put(document);
34 * } catch (PutException e) {
35 * if (StatusCode.TRANSIENT_ERROR.equals(e.getOperationResult().getCode())) {
36 * // retry putting document
42 * Results<ScoredDocument> results =
43 * index.search(Query.newBuilder().build("subject:first body:here"));
45 * // Iterate through the search results.
46 * for (ScoredDocument document : results) {
49 * } catch (SearchException e) {
50 * if (StatusCode.TRANSIENT_ERROR.equals(e.getOperationResult().getCode())) {
57 public interface Index
{
60 * @return the name of the index
65 * @return the namespace of the index name
67 String
getNamespace();
70 * @return the consistency mode of this index
71 * @deprecated As of 1.7.3 Consistency is deprecated since only PER_DOCUMENT
75 Consistency
getConsistency();
78 * @see #delete(String...)
79 * @deprecated as of 1.7.3. Use {@link #deleteAsync(String...)} instead
82 Future
<Void
> removeAsync(String
... documentId
);
85 * @see #delete(String...)
86 * @deprecated as of 1.7.3. Use {@link #deleteAsync(Iterable<String>)} instead
89 Future
<Void
> removeAsync(Iterable
<String
> documentIds
);
92 * @see #delete(String...)
94 Future
<Void
> deleteAsync(String
... documentId
);
97 * @see #delete(String...)
99 Future
<Void
> deleteAsync(Iterable
<String
> documentIds
);
102 * @see #deleteSchema()
103 * @deprecated as of 1.7.4
106 Future
<Void
> deleteSchemaAsync();
109 * @see #put(Document...)
110 * @deprecated as of 1.7.3. Use {@link #putAsync(Document...)} instead
113 Future
<AddResponse
> addAsync(Document
... document
);
116 * @see #put(Document...)
117 * @deprecated as of 1.7.3. Use {@link #putAsync(Document.Builder...)} instead
120 Future
<AddResponse
> addAsync(Document
.Builder
... document
);
123 * @see #put(Document...)
124 * @deprecated as of 1.7.3. Use {@link #putAsync(Iterable<Document>)} instead
127 Future
<AddResponse
> addAsync(Iterable
<Document
> documents
);
130 * @see #put(Document...)
132 Future
<PutResponse
> putAsync(Document
... document
);
135 * @see #put(Document...)
137 Future
<PutResponse
> putAsync(Document
.Builder
... document
);
140 * @see #put(Document...)
142 Future
<PutResponse
> putAsync(Iterable
<Document
> documents
);
145 * @see #search(String)
147 Future
<Results
<ScoredDocument
>> searchAsync(String query
);
150 * @see #search(Query)
152 Future
<Results
<ScoredDocument
>> searchAsync(Query query
);
155 * @see #listDocuments(ListRequest)
156 * @deprecated as of 1.7.3. Use {@link #getRangeAsync(GetRequest)}
159 Future
<ListResponse
<Document
>> listDocumentsAsync(ListRequest request
);
162 * @see #getRange(GetRequest)
164 Future
<GetResponse
<Document
>> getRangeAsync(GetRequest request
);
167 * @see #getRange(GetRequest)
169 Future
<GetResponse
<Document
>> getRangeAsync(GetRequest
.Builder builder
);
172 * Delete documents for the given document ids from the index if
173 * they are in the index.
175 * @param documentIds the ids of documents to remove
176 * @throws RemoveException if there is a failure in the search
177 * service deleting documents
178 * @throws IllegalArgumentException if some document id is invalid
179 * @deprecated as of 1.7.3. Use {@link #delete(String...)} instead
182 void remove(String
... documentIds
);
185 * @see #delete(String...)
186 * @deprecated as of 1.7.3. Use {@link #delete(Iterable<String>)} instead
189 void remove(Iterable
<String
> documentIds
);
192 * Delete documents for the given document ids from the index if
193 * they are in the index.
195 * @param documentIds the ids of documents to delete
196 * @throws DeleteException if there is a failure in the search
197 * service deleting documents
198 * @throws IllegalArgumentException if some document id is invalid
200 void delete(String
... documentIds
);
203 * @see #delete(String...)
205 void delete(Iterable
<String
> documentIds
);
208 * Delete the schema from the index. A possible use may be that there are
209 * typed fields which are no longer required. Make sure that you re-index
210 * some or all of your documents to enable search again. A sample of
211 * documents is required that uses named fields to rebuild the schema.
213 * @throws DeleteException if there is a failure in the search service
214 * deleting the schema
215 * @deprecated as of 1.7.4
221 * Add the documents to the index, updating any document that is already
224 * @param documents the documents to add to the index
225 * @return an {@link AddResponse} containing the result of
226 * the add operations indicating success or failure as well as the document
227 * ids. The search service will allocate document ids for documents which
229 * @throws AddException if there is a failure in the search
230 * service adding documents
231 * @throws IllegalArgumentException if some document is invalid or
232 * more than {@link IndexChecker#MAXIMUM_DOCS_PER_REQUEST} documents
233 * requested to be added
234 * @deprecated as of 1.7.3. Use {@link #put(Document...)} instead
237 AddResponse
add(Document
... documents
);
240 * @see #put(Document...)
241 * @deprecated as of 1.7.3. Use {@link #put(Document.Builder...)} instead
244 AddResponse
add(Document
.Builder
... builders
);
247 * @see #put(Document...)
248 * @deprecated as of 1.7.3. Use {@link #put(Iterable<Document>)} instead
251 AddResponse
add(Iterable
<Document
> documents
);
254 * Put the documents into the index, updating any document that is already
257 * @param documents the documents to put into the index
258 * @return an {@link AddResponse} containing the result of
259 * the put operations indicating success or failure as well as the document
260 * ids. The search service will allocate document ids for documents which
262 * @throws PutException if there is a failure in the search
263 * service putting documents
264 * @throws IllegalArgumentException if some document is invalid or
265 * more than {@link IndexChecker#MAXIMUM_DOCS_PER_REQUEST} documents
266 * requested to be put into the index
268 PutResponse
put(Document
... documents
);
271 * @see #put(Document...)
273 PutResponse
put(Document
.Builder
... builders
);
276 * @see #put(Document...)
278 PutResponse
put(Iterable
<Document
> documents
);
281 * Gets a {@link Document} for the given document Id.
283 * @param documentId the identifier for the document to retrieve
284 * @return the associated {@link Document}. can be null
286 Document
get(String documentId
);
289 * @see #search(Query)
291 Results
<ScoredDocument
> search(String query
);
294 * Search the index for documents matching the query. The query
295 * must specify a query string, and optionally, how many documents
296 * are requested, how the results are to be sorted, scored and
297 * which fields are to be returned.
299 * @param query the fully specified {@link Query} object
300 * @return a {@link Results} containing
301 * {@link ScoredDocument ScoredDocuments}
302 * @throws IllegalArgumentException if the query is invalid
303 * @throws SearchQueryException if the query string is invalid
304 * @throws SearchException if there is a failure in the search service
305 * performing the search
307 Results
<ScoredDocument
> search(Query query
);
310 * Lists the index's documents, in document Id order.
312 * @param request contains various options restricting which documents are
314 * @return a {@link ListResponse} containing a list of
315 * documents from the index
316 * @throws IllegalArgumentException if the list request is invalid
317 * @deprecated as of 1.7.3. Use {@link #getRange(GetRequest)} instead.
320 ListResponse
<Document
> listDocuments(ListRequest request
);
323 * Get an index's documents, in document Id order.
325 * @param request contains various options restricting which documents are
327 * @return a {@link GetResponse} containing a list of
328 * documents from the index
329 * @throws IllegalArgumentException if the get request is invalid
331 GetResponse
<Document
> getRange(GetRequest request
);
334 * @see #getRange(GetRequest)
336 GetResponse
<Document
> getRange(GetRequest
.Builder builder
);
339 * @return the {@link Schema} describing supported document field names and
340 * {@link Field.FieldType}s supported for those field names. This schema
341 * will only be populated if the {@link GetIndexesRequest#isSchemaFetched}
342 * is set to true on an {@link SearchService#getIndexes} request