【问题标题】:Using 500+ Char Strings with Google AppEngine Datastore在 Google AppEngine 数据存储中使用 500 多个字符字符串
【发布时间】:2015-02-13 19:19:42
【问题描述】:

我正在尝试将文档存储到 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();
    }

【问题讨论】:

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


    【解决方案1】:

    您需要使用 Text 值类型。它没有被截断。无法索引此值类型。

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

    Properties and value types

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

    【讨论】:

    • 谢谢。我刚刚得知我错误地使用了Text 对象。我打电话给toString 而不是getValue
    猜你喜欢
    • 2013-12-01
    • 2012-10-01
    • 2013-03-18
    • 2021-08-06
    • 1970-01-01
    • 2013-01-10
    • 2018-10-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多