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

标签 java google-app-engine google-cloud-datastore

我正在尝试将文档存储到 Google AppEngine 数据存储区。起初,我尝试使用 Text 对象,但存储的文本被某种我无法找到任何信息的索引截断。我现在收到错误:test.at.example.com: com.google.appengine.api.search.Document 不是受支持的属性类型。 如何存储 String 长度超过 500 个字符?

这是存储字符串的方法:

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。它有自己的插入和检索文档的方法 - 您不能将 Document 对象存储在数据存储实体的属性中。

关于java - 在 Google AppEngine 数据存储中使用 500 多个字符字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28507106/

相关文章:

java - 使用JAVA按2个字段对文件中的行进行排序

mysql - 在 app.yaml 中存储 secret 的最佳实践

google-cloud-datastore - 谷歌云数据存储模拟器初始化数据

java - 关于 objectify 事务排队任务的问题

java - 有 SQLite 查询 validator 库吗?

javascript - 读取数据产生 null

java - TCP/IP 打开连接

google-app-engine - Google App Engine - 保留以前版本的静态文件

django - 既然 GAE(据称)开箱即用地支持 Django,我们还需要 django-nonrel 吗?

google-app-engine - 在迁移到高复制数据存储后,在另一个增量副本之前可以安全地接触数据吗?