在 Google AppEngine 数据存储中使用 500 多个字符字符串

Using 500+ Char Strings with Google AppEngine Datastore

我正在尝试将文档存储到 Google AppEngine 数据存储区。起初我尝试使用 Text 对象,但是存储的文本被某种索引截断了,我找不到任何相关信息。我现在收到错误消息:test.at.example.com: com.google.appengine.api.search.Document is not a supported property type. 如何存储超过 500 个字符的 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();
}

这是我检索数据的方法:

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();
    }

您需要使用文本值类型。它没有被截断。无法为该值类型编制索引。

在此处查看属性支持的值类型列表:

Properties and value types

如果您需要在文本字符串中搜索,您需要使用搜索API。它有自己的插入和检索文档的方法 - 您不能将文档对象存储在数据存储实体的 属性 中。