本文整理汇总了Java中com.google.appengine.api.search.Document类的典型用法代码示例。如果您正苦于以下问题:Java Document类的具体用法?Java Document怎么用?Java Document使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Document类属于com.google.appengine.api.search包,在下文中一共展示了Document类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: removeAll
import com.google.appengine.api.search.Document; //导入依赖的package包/类
protected int removeAll() {
int count = 0;
Index index = getIndex();
GetRequest request = GetRequest.newBuilder().setReturningIdsOnly(true).setLimit(200).build();
GetResponse<Document> response = index.getRange(request);
// can only deleteUnit documents in blocks of 200 so we need to iterate until they're all gone
while (!response.getResults().isEmpty()) {
List<String> ids = new ArrayList<>();
for (Document document : response) {
ids.add(document.getId());
}
index.delete(ids);
count += ids.size();
response = index.getRange(request);
}
return count;
}
示例2: buildDocument
import com.google.appengine.api.search.Document; //导入依赖的package包/类
protected Document buildDocument(K id, Map<String, Object> fields) {
String stringId = convert(id, String.class);
Builder documentBuilder = Document.newBuilder();
documentBuilder.setId(stringId);
for (Map.Entry<String, Object> fieldData : fields.entrySet()) {
Object value = fieldData.getValue();
String fieldName = fieldData.getKey();
for (Object object : getCollectionValues(value)) {
try {
Field field = buildField(metadata, fieldName, object);
documentBuilder.addField(field);
} catch (Exception e) {
throw new SearchException(e, "Failed to add field '%s' with value '%s' to document with id '%s': %s", fieldName, value.toString(), id, e.getMessage());
}
}
}
return documentBuilder.build();
}
示例3: indexAsync
import com.google.appengine.api.search.Document; //导入依赖的package包/类
@Nonnull
@Override
public <E> Runnable indexAsync(Map<String, E> entities) {
if (entities.isEmpty()) {
return doNothing();
}
Class<?> entityClass = entities.values().toArray()[0].getClass();
if (!searchMetadata.hasIndexedFields(entityClass)) {
return doNothing();
}
List<Document> documents = entities.entrySet().stream()
.map(entry -> documentBuilder.apply(entry.getKey(), entry.getValue()))
.collect(Collectors.toList());
Index index = getIndex(entityClass);
return new IndexOperation(
index.putAsync(documents)
);
}
示例4: clear
import com.google.appengine.api.search.Document; //导入依赖的package包/类
@Override
public <E> int clear(Class<E> entityClass) {
Index index = getIndex(entityClass);
GetRequest request = GetRequest.newBuilder()
.setReturningIdsOnly(true)
.setLimit(200) //Delete only allows 200 records at a time.
.build();
int count = 0;
for (List<Document> documents = getDocumentIds(index, request); !documents.isEmpty(); documents = getDocumentIds(index, request)) {
count += documents.size();
unindex(entityClass, documents.stream().map(Document::getId));
}
return count;
}
示例5: indexApp
import com.google.appengine.api.search.Document; //导入依赖的package包/类
/**
* 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
}
}
}
示例6: buildDocument
import com.google.appengine.api.search.Document; //导入依赖的package包/类
/**
* Builds a new Place document to insert in the Places index.
* @param placeId the identifier of the place in the database.
* @param placeName the name of the place.
* @param placeAddress the address of the place.
* @param location the GPS location of the place, as a GeoPt.
* @return the Place document created.
*/
public static Document buildDocument(
final Long placeId, final String placeName,
final String placeAddress, final GeoPt location) {
GeoPoint geoPoint = new GeoPoint(location.getLatitude(),
location.getLongitude());
Document.Builder builder = Document.newBuilder()
.addField(Field.newBuilder().setName("id")
.setText(placeId.toString()))
.addField(Field.newBuilder().setName("name").setText(placeName))
.addField(Field.newBuilder().setName("address")
.setText(placeAddress))
.addField(Field.newBuilder().setName("place_location")
.setGeoPoint(geoPoint));
// geo-location doesn't work under dev_server, so let's add another
// field to use for retrieving documents
if (environment.value() == Development) {
builder.addField(Field.newBuilder().setName("value").setNumber(1));
}
return builder.build();
}
示例7: removeAllDocumentsFromIndex
import com.google.appengine.api.search.Document; //导入依赖的package包/类
/**
* 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);
}
}
示例8: doGet
import com.google.appengine.api.search.Document; //导入依赖的package包/类
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
PrintWriter out = resp.getWriter();
Document document = Document.newBuilder()
.setId("AZ125")
.addField(Field.newBuilder().setName("myField").setText("myValue")).build();
try {
Utils.indexADocument(INDEX, document);
} catch (InterruptedException e) {
out.println("Interrupted");
return;
}
out.println("Indexed a new document.");
// [START get_document]
IndexSpec indexSpec = IndexSpec.newBuilder().setName(INDEX).build();
Index index = SearchServiceFactory.getSearchService().getIndex(indexSpec);
// Fetch a single document by its doc_id
Document doc = index.get("AZ125");
// Fetch a range of documents by their doc_ids
GetResponse<Document> docs = index.getRange(
GetRequest.newBuilder().setStartId("AZ125").setLimit(100).build());
// [END get_document]
out.println("myField: " + docs.getResults().get(0).getOnlyField("myField").getText());
}
示例9: createDocument
import com.google.appengine.api.search.Document; //导入依赖的package包/类
/**
* Code snippet for creating a Document.
* @return Document Created document.
*/
public Document createDocument() {
// [START create_document]
User currentUser = UserServiceFactory.getUserService().getCurrentUser();
String userEmail = currentUser == null ? "" : currentUser.getEmail();
String userDomain = currentUser == null ? "" : currentUser.getAuthDomain();
String myDocId = "PA6-5000";
Document doc = Document.newBuilder()
// Setting the document identifer is optional.
// If omitted, the search service will create an identifier.
.setId(myDocId)
.addField(Field.newBuilder().setName("content").setText("the rain in spain"))
.addField(Field.newBuilder().setName("email").setText(userEmail))
.addField(Field.newBuilder().setName("domain").setAtom(userDomain))
.addField(Field.newBuilder().setName("published").setDate(new Date()))
.build();
// [END create_document]
return doc;
}
示例10: doGet
import com.google.appengine.api.search.Document; //导入依赖的package包/类
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
PrintWriter out = resp.getWriter();
Document document = Document.newBuilder()
.addField(Field.newBuilder().setName("coverLetter").setText("CoverLetter"))
.addField(Field.newBuilder().setName("resume").setHTML("<html></html>"))
.addField(Field.newBuilder().setName("fullName").setAtom("Foo Bar"))
.addField(Field.newBuilder().setName("submissionDate").setDate(new Date()))
.build();
// [START access_document]
String coverLetter = document.getOnlyField("coverLetter").getText();
String resume = document.getOnlyField("resume").getHTML();
String fullName = document.getOnlyField("fullName").getAtom();
Date submissionDate = document.getOnlyField("submissionDate").getDate();
// [END access_document]
out.println("coverLetter: " + coverLetter);
out.println("resume: " + resume);
out.println("fullName: " + fullName);
out.println("submissionDate: " + submissionDate.toString());
}
示例11: doGet
import com.google.appengine.api.search.Document; //导入依赖的package包/类
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
// Put one document to avoid an error
Document document = Document.newBuilder()
.setId("theOnlyCoffeeRoaster")
.addField(Field.newBuilder().setName("price").setNumber(200))
.addField(Field.newBuilder().setName("model").setText("TZ4000"))
.addField(Field.newBuilder().setName("brand").setText("MyBrand"))
.addField(Field.newBuilder().setName("product").setText("coffee roaster"))
.addField(Field.newBuilder()
.setName("description").setText("A coffee bean roaster at home"))
.build();
try {
Utils.indexADocument(SEARCH_INDEX, document);
} catch (InterruptedException e) {
// ignore
}
PrintWriter out = resp.getWriter();
Results<ScoredDocument> result = doSearch();
for (ScoredDocument doc : result.getResults()) {
out.println(doc.toString());
}
}
示例12: indexADocument
import com.google.appengine.api.search.Document; //导入依赖的package包/类
/**
* 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;
}
}
示例13: indexADocument_successfullyInvoked
import com.google.appengine.api.search.Document; //导入依赖的package包/类
@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);
assertThat(fetched.getOnlyField("f").getText())
.named("A value of the fetched document")
.isEqualTo("v");
}
示例14: createDocument
import com.google.appengine.api.search.Document; //导入依赖的package包/类
/**
* Code snippet for creating a Document.
*
* @return Document Created document.
*/
public Document createDocument() {
// [START create_document]
User currentUser = UserServiceFactory.getUserService().getCurrentUser();
String userEmail = currentUser == null ? "" : currentUser.getEmail();
String userDomain = currentUser == null ? "" : currentUser.getAuthDomain();
String myDocId = "PA6-5000";
Document doc =
Document.newBuilder()
// Setting the document identifer is optional.
// If omitted, the search service will create an identifier.
.setId(myDocId)
.addField(Field.newBuilder().setName("content").setText("the rain in spain"))
.addField(Field.newBuilder().setName("email").setText(userEmail))
.addField(Field.newBuilder().setName("domain").setAtom(userDomain))
.addField(Field.newBuilder().setName("published").setDate(new Date()))
.build();
// [END create_document]
return doc;
}
示例15: doGet
import com.google.appengine.api.search.Document; //导入依赖的package包/类
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
PrintWriter out = resp.getWriter();
Document document =
Document.newBuilder()
.addField(Field.newBuilder().setName("coverLetter").setText("CoverLetter"))
.addField(Field.newBuilder().setName("resume").setHTML("<html></html>"))
.addField(Field.newBuilder().setName("fullName").setAtom("Foo Bar"))
.addField(Field.newBuilder().setName("submissionDate").setDate(new Date()))
.build();
// [START access_document]
String coverLetter = document.getOnlyField("coverLetter").getText();
String resume = document.getOnlyField("resume").getHTML();
String fullName = document.getOnlyField("fullName").getAtom();
Date submissionDate = document.getOnlyField("submissionDate").getDate();
// [END access_document]
out.println("coverLetter: " + coverLetter);
out.println("resume: " + resume);
out.println("fullName: " + fullName);
out.println("submissionDate: " + submissionDate.toString());
}