motoku motoku - 1 month ago 6
Java Question

Using 500+ Char Strings with Google AppEngine Datastore

I am trying to store a Document to the Google AppEngine Datastore. At first I tried using a Text object, but the stored text became truncated by some kind of indexing I was not able to find any information about. I now get the error:

test.at.example.com: com.google.appengine.api.search.Document is not a supported property type.
How can I store a
String
longer than 500 characters?

This is the method to store the String:

public String post(String user, String documentText) {
if (!documents.hasProperty(user)) {
String indexesstr = (String) documents.getProperty(INDEXLABEL);
documents.setProperty(INDEXLABEL, indexesstr + "\n" + user);
}
documentText = documentText.replace("<", "&lt;");
documentText = documentText.replace("\n", "<br>");
documentText = documentText.replace("\r", "<br>");
Document doc = Document.newBuilder()
.addField(Field.newBuilder().setName("user").setText(user))
.addField(Field.newBuilder().setName("content").setText(documentText))
.build();
documents.setProperty(convertId(user), doc);

datastore.put(documents);
return makeIndex();
}


This is my method for retrieving the data:

public String getDocument(String contextPath) {
int offsSlash = contextPath.lastIndexOf("/") + 1;
String ID = contextPath.substring(offsSlash,
contextPath.lastIndexOf("."));
StringBuilder sb = new StringBuilder();
sb.append(prependHTML);
Document document = (Document) documents.getProperty(convertId(ID));
if (document == null)
sb.append("Document not found.");
else {
sb.append(document.getOnlyField("document").getText());
sb.append("<br><div align=\"right\">");
sb.append(document.getOnlyField("user").getText());
sb.append("</div>");
}
sb.append(ammendHTML);
return sb.toString();
}

Answer

You need to use a Text value type. It's not truncated. This value type cannot be indexed.

See the list of supported value types for properties here:

Properties and value types

If you need to search inside a text string, you need to use the Search API. It has its own methods for inserting and retrieving documents - you cannot store a Document object in a property of a datastore entity.