如何构建 class 以在 cloudant 中使用 JSON 文档管理搜索索引查询的输出
How to build class to manage the output of search index query with JSON document in cloudant
我在 https://github.com/cloudant/java-cloudant/blob/88202a1bd7b9b04d96c4b7b8498a1b8f7f99c9e5/src/test/java/com/cloudant/tests/Animal.java
关注了样本 class 动物
我通过这个 class 成功地管理了搜索索引查询。我想我在 cloudant 中有一个 JSON 格式的文档:
{
"_id": "web",
"_rev": "11-b1d0e315272a87c2549df4004d836049",
"min_weight": 40,
"max_weight": 65,
"min_length": 1,
"max_length": 2.2,
"attributeCollection": {
"attributeArray": [
{
"updateable": false,
"lookup": "issuetype",
"issueAttributeDefinitionId": 13,
"attributeType": 1,
"name": "web issue",
"value": [
"Improper Neutralization of Input During Web Page Generation"
]
}
]
},
}
我的问题是如何构建 Java class 来管理此文档的搜索索引输出。特别是,如何管理属性集 "attributeCollection"、"attributeArray"、..."name"、"value"
根据你最近的几个 Stack Overflow posts 我认为你有几个选择:
1) 如果您像在之前的 post 中那样定义 issue
class,您可以在 Java 中执行不同类型的搜索,仅 return 你的issue中那些字段class如下:
SearchResult<issue> issues=db.search("attributes/by_name_value")
.limit(10).includeDocs(false)
.querySearchResult("name:\"web*\"", issue.class);
for (int i = 0; i < issues.getRows().size(); i++) {
SearchResult<issue>.SearchResultRow row = issues.getRows().get(i);
System.out.println(row.getId());
System.out.println(row.getFields().getName());
System.out.println(row.getFields().getValue());
}
注意:这里调用 querySearchResult
而不是 query
并且 include_docs
是错误的。
2) 如果您需要 return 整个文档,那么您需要创建 class 匹配您的 JSON 的文件。您的 classes 应如下所示:
问题2
public class Issue2 {
private String id;
private Integer min_weight;
// TODO: other fields
private AttributeCollection attributeCollection;
public Issue2() {
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public Integer getMin_weight() {
return min_weight;
}
public void setMin_weight(Integer min_weight) {
this.min_weight = min_weight;
}
public AttributeCollection getAttributeCollection() {
return attributeCollection;
}
public void setAttributeCollection(AttributeCollection attributeCollection) {
this.attributeCollection = attributeCollection;
}
}
属性集合
public class AttributeCollection {
private Attribute[] attributeArray;
public Attribute[] getAttributeArray() {
return attributeArray;
}
public void setAttributeArray(Attribute[] attributeArray) {
this.attributeArray = attributeArray;
}
}
属性
public class Attribute {
private String name;
private String value[];
// TODO: other fields
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String[] getValue() {
return value;
}
public void setValue(String[] value) {
this.value = value;
}
}
然后你可以使用之前的搜索调用(使用 Issue2 class):
List<Issue2> issues=db.search("attributes/by_name_value")
.limit(10).includeDocs(true)
.query("name:\"web*\"", Issue2.class);
for (int i = 0; i < issues.size(); i++) {
Issue2 row = issues.get(i);
System.out.println("min_weight = " + row.getMin_weight());
if (row.getAttributeCollection() != null && row.getAttributeCollection().getAttributeArray() != null) {
for (int j=0; j<row.getAttributeCollection().getAttributeArray().length; j++) {
String name = row.getAttributeCollection().getAttributeArray()[i].getName();
String[] values = row.getAttributeCollection().getAttributeArray()[i].getValue();
System.out.println(name);
if (values != null) {
for(String value: values) {
System.out.println(value);
}
}
}
}
}
我在 https://github.com/cloudant/java-cloudant/blob/88202a1bd7b9b04d96c4b7b8498a1b8f7f99c9e5/src/test/java/com/cloudant/tests/Animal.java
我通过这个 class 成功地管理了搜索索引查询。我想我在 cloudant 中有一个 JSON 格式的文档:
{
"_id": "web",
"_rev": "11-b1d0e315272a87c2549df4004d836049",
"min_weight": 40,
"max_weight": 65,
"min_length": 1,
"max_length": 2.2,
"attributeCollection": {
"attributeArray": [
{
"updateable": false,
"lookup": "issuetype",
"issueAttributeDefinitionId": 13,
"attributeType": 1,
"name": "web issue",
"value": [
"Improper Neutralization of Input During Web Page Generation"
]
}
]
},
}
我的问题是如何构建 Java class 来管理此文档的搜索索引输出。特别是,如何管理属性集 "attributeCollection"、"attributeArray"、..."name"、"value"
根据你最近的几个 Stack Overflow posts 我认为你有几个选择:
1) 如果您像在之前的 post 中那样定义 issue
class,您可以在 Java 中执行不同类型的搜索,仅 return 你的issue中那些字段class如下:
SearchResult<issue> issues=db.search("attributes/by_name_value")
.limit(10).includeDocs(false)
.querySearchResult("name:\"web*\"", issue.class);
for (int i = 0; i < issues.getRows().size(); i++) {
SearchResult<issue>.SearchResultRow row = issues.getRows().get(i);
System.out.println(row.getId());
System.out.println(row.getFields().getName());
System.out.println(row.getFields().getValue());
}
注意:这里调用 querySearchResult
而不是 query
并且 include_docs
是错误的。
2) 如果您需要 return 整个文档,那么您需要创建 class 匹配您的 JSON 的文件。您的 classes 应如下所示:
问题2
public class Issue2 {
private String id;
private Integer min_weight;
// TODO: other fields
private AttributeCollection attributeCollection;
public Issue2() {
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public Integer getMin_weight() {
return min_weight;
}
public void setMin_weight(Integer min_weight) {
this.min_weight = min_weight;
}
public AttributeCollection getAttributeCollection() {
return attributeCollection;
}
public void setAttributeCollection(AttributeCollection attributeCollection) {
this.attributeCollection = attributeCollection;
}
}
属性集合
public class AttributeCollection {
private Attribute[] attributeArray;
public Attribute[] getAttributeArray() {
return attributeArray;
}
public void setAttributeArray(Attribute[] attributeArray) {
this.attributeArray = attributeArray;
}
}
属性
public class Attribute {
private String name;
private String value[];
// TODO: other fields
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String[] getValue() {
return value;
}
public void setValue(String[] value) {
this.value = value;
}
}
然后你可以使用之前的搜索调用(使用 Issue2 class):
List<Issue2> issues=db.search("attributes/by_name_value")
.limit(10).includeDocs(true)
.query("name:\"web*\"", Issue2.class);
for (int i = 0; i < issues.size(); i++) {
Issue2 row = issues.get(i);
System.out.println("min_weight = " + row.getMin_weight());
if (row.getAttributeCollection() != null && row.getAttributeCollection().getAttributeArray() != null) {
for (int j=0; j<row.getAttributeCollection().getAttributeArray().length; j++) {
String name = row.getAttributeCollection().getAttributeArray()[i].getName();
String[] values = row.getAttributeCollection().getAttributeArray()[i].getValue();
System.out.println(name);
if (values != null) {
for(String value: values) {
System.out.println(value);
}
}
}
}
}