【问题标题】:Using Cloudant Client Search API Doesn't Return All Results Expected使用 Cloudant 客户端搜索 API 不会返回预期的所有结果
【发布时间】:2017-10-03 20:11:51
【问题描述】:

我对 Cloudant 还很陌生,但在 DB2 上使用 SQL 开发已经有一段时间了。我遇到了一个问题,我认为我正在使用 Lucene 查询引擎和 Cloudant 索引从我的查询中返回结果,但只有三分之二的预期字段正在返回数据。不返回数据的字段是一个数组。我们的应用程序正在运行 Java,并使用 IBM 的 Bluemix 和 WebSphere Liberty Profile 执行。我已经打包了 cloudant-client-2.8.0.jar 和 cloudant-HTTP-2.8.0.jar 文件来访问 cloudant 数据库。我们有许多正在运行的查询,因此连接本身很好。

这是我访问 Cloudant API 的代码:

....
Search searchSaaSData = ServiceManagerSingleton.getInstance().getSaaSCapabilitiesDatabase().search("versioning-ddoc/versioning-indx").includeDocs(true);

SearchResult<DocTypeInfo> result = searchSaaSData.querySearchResult(this.getJsonSelector(deliverableId), DocTypeInfo.class);
....

这是 this.getJsonSelector 代码:

private String getVersioningSearch(String deliverableId) {
    String query = "";
    if (deliverableId != null && !deliverableId.isEmpty()) {
        // Search based on deliverableId
        query += "(";
        query += "deliverableId:\"" + deliverableId + "\"" + ")";
    }

    logger.log(Level.INFO, "Search query is: " + query);
    // The query is simple, will look like this: 
    // deliverableId:"0B439290AB5011E6BE74C84817AAB206")

    return query;
}

从上面的 java 代码中可以看出,我使用 java 对象 DocTypeInfo.class 来保存从搜索返回的数据。这是课程:

public class DocTypeInfo {
    private final String docType;
    private final String version;
    private String isPublishedForReports;

    public DocTypeInfo(String docType, String version) {
        this.docType = docType;
        this.version = version;
    }

    /**
     * @return the docType
     */
    private String getDocType() {
        return docType;
    }

    /**
     * @return the version
     */
    private String getVersion() {
        return version;
    }

    /**
     * @return the isPublishedForReports
     */
    public String getIsPublishedForReports() {
        return isPublishedForReports;
    }

    /**
     * @param isPublishedForReports the isPublishedForReports to set
     */
    public void setIsPublishedForReports(String isPublishedForReports) {
        this.isPublishedForReports = isPublishedForReports;
    }       

}

我已经使用 Cloudant 仪表板设置了一个设计文档和索引,如下所示:

{
"_id": "_design/versioning-ddoc",
"_rev": "22-0e0c0ccfc2b5fe7245352da7e5b1ebd3",
"views": {},
"language": "javascript",
"indexes": {
  "versioning-indx": {
  "analyzer": {
    "name": "perfield",
    "default": "standard",
    "fields": {
      "deliverableId": "whitespace",
      "docType": "standard",
      "version": "standard",
      "isPublishedForReports": "keyword"
    }
  },
  "index": "function (doc) {
               index(\"deliverableId\", doc.deliverableId, {\"store\":true, \"boost\":1.0});
   index(\"docType\", doc.docType, {\"store\":true, \"boost\":1.0});
   index(\"version\", doc.version, {\"store\":true, \"boost\":1.0});
   if (Array.isArray(doc.publishSettings)) {
     for (var i in doc.publishSettings) {
       if (doc.publishSettings[i].options) {
         for (var j in doc.publishSettings[i].options) {
           index(\"isPublishedForReports\", doc.publishSettings[i].options[j].optionId, {\"store\":true, \"boost\":1.0});
           }
         }
       }
     }
   }"
  }
  }
 }

当我执行搜索时,会填充 docType 和 version 字段,但是 isPublishedForReports 字段始终为 null 或不返回。当我在 Cloudant 仪表板中针对索引运行查询时,我可以看到返回了 isPublishedForReports 值,但我不知道为什么它没有填充到对象中?也许我误解了这些是如何构建的?

这是我查询数据库的屏幕截图,我可以看到我想要的结果:

请帮忙! -道格

【问题讨论】:

    标签: java lucene cloudant


    【解决方案1】:

    我认为您正在从result.rows 中的每一行访问docs 属性,而不是fields 属性。当您使用.includeDocs(true) 运行搜索时,您将看到类似于以下的结果:

    {
      "total_rows":3,
      "bookmark":"g1AAA",
      "rows":[
        {
          "id":"263a81ea76528dead3a4185df3676f62",
          "order":[
            1.0,
            0
          ],
          "fields":{
            "docType":"xxx",
            "deliverableId":"yyy",
            "isPublishedForReports":"pu_1_2"
          },
          "doc":{
            "_id":"263a81ea76528dead3a4185df3676f62",
            "_rev":"3-116dd3831c182fb13c12b05a8b0996e4",
            "docType":"xxx",
            "deliverableId":"yyy",
            "publishSettings":[...]
          }
        }
        ...
      ]
    } 
    

    注意如何在fields 属性中查看您在搜索索引中定义的字段,以及在doc 属性中查看完整文档。完整文档不包括isPublishedForReports

    要获取isPublishedForReports 值,您需要访问fields 属性:

    for (SearchResult<DocTypeInfo>.SearchResultRow row : result.getRows()) {
       ...
       row.getFields().getIsPublishedForReports()
       ...
    }
    

    另外,如果您不需要整个文档,您可以设置 .includeDocs(false) 并仅访问 fields 属性。

    【讨论】:

    • 您的权利,我将您的权利标记为答案。当我更改它并开始访问这些字段时,我可以得到我需要的东西。也感谢您的解释。非常感激。 -Doug M.
    猜你喜欢
    • 1970-01-01
    • 2013-06-26
    • 2020-08-30
    • 2021-07-06
    • 1970-01-01
    • 2021-01-18
    • 1970-01-01
    • 1970-01-01
    • 2018-11-17
    相关资源
    最近更新 更多