Java 使用Cloudant客户端搜索API不会返回预期的所有结果
我对Cloudant非常陌生,但已经在DB2上使用SQL开发了一段时间。我遇到了一个问题,我*认为我正在使用Lucene查询引擎和Cloudant索引从我的查询返回结果,但预期的三个字段中只有两个返回数据。不返回数据的字段是数组。我们的应用程序运行Java,并使用IBM的Bluemix和WebSphere Liberty概要文件执行。我已经打包了cloudant-client-2.8.0.jar和cloudant-HTTP-2.8.0.jar文件来访问cloudant数据库。我们有许多查询正在工作,因此连接本身是良好的 以下是我访问Cloudant API的代码:Java 使用Cloudant客户端搜索API不会返回预期的所有结果,java,lucene,cloudant,Java,Lucene,Cloudant,我对Cloudant非常陌生,但已经在DB2上使用SQL开发了一段时间。我遇到了一个问题,我*认为我正在使用Lucene查询引擎和Cloudant索引从我的查询返回结果,但预期的三个字段中只有两个返回数据。不返回数据的字段是数组。我们的应用程序运行Java,并使用IBM的Bluemix和WebSphere Liberty概要文件执行。我已经打包了cloudant-client-2.8.0.jar和cloudant-HTTP-2.8.0.jar文件来访问cloudant数据库。我们有许多查询正在工
....
Search searchSaaSData = ServiceManagerSingleton.getInstance().getSaaSCapabilitiesDatabase().search("versioning-ddoc/versioning-indx").includeDocs(true);
SearchResult<DocTypeInfo> result = searchSaaSData.querySearchResult(this.getJsonSelector(deliverableId), DocTypeInfo.class);
....
从上面的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值被返回,我不知道为什么它没有填充到对象中?也许我误解了这些是如何建造的
这里是一个屏幕截图,我在这里查询数据库,我可以看到我想要的结果:
请帮忙!
-Doug我认为您正在从result.rows中的每一行访问docs属性,而不是fields属性。使用.includeDocstrue运行搜索时,您将看到类似以下结果:
{
"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":[...]
}
}
...
]
}
请注意如何在“字段”属性中查看搜索索引中定义的字段,以及在“文档”属性中查看完整文档。完整文档不包括iPublishedForReports
要获取isPublishedForReports值,您需要访问fields属性:
for (SearchResult<DocTypeInfo>.SearchResultRow row : result.getRows()) {
...
row.getFields().getIsPublishedForReports()
...
}
另外,如果您不需要整个文档,您可以设置.includeDocsfalse并仅访问fields属性。您的权利,我将您的标记为答案。当我更改它并开始访问字段时,我可以得到我需要的。也谢谢你的解释。非常感谢-道格M。
for (SearchResult<DocTypeInfo>.SearchResultRow row : result.getRows()) {
...
row.getFields().getIsPublishedForReports()
...
}