gpt4 book ai didi

java - 如何在cloudant中构建类来管理使用JSON文档的搜索索引查询的输出

转载 作者:行者123 更新时间:2023-12-02 03:43:05 25 4
gpt4 key购买 nike

我遵循了示例类 Animal https://github.com/cloudant/java-cloudant/blob/88202a1bd7b9b04d96c4b7b8498a1b8f7f99c9e5/src/test/java/com/cloudant/tests/Animal.java

我成功地通过此类管理了搜索索引查询。我想我在 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 类来管理该文档的搜索索引输出。特别是,如何管理“attributeCollection”、“attributeArray”、...“name”、“value”等属性集

最佳答案

根据您最近在 Stack Overflow 上发表的几篇文章,我认为您有几个选择:

1) 如果您像在上一篇文章中那样定义 issue 类,则可以在 Java 中执行不同类型的搜索,以仅返回问题类中的这些字段,如下所示:

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 为 false。

2) 如果您需要返回整个文档,那么您需要创建与您的 JSON 匹配的类。你的类应该看起来像这样:

问题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 类):

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);
}
}
}
}
}

关于java - 如何在cloudant中构建类来管理使用JSON文档的搜索索引查询的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36630629/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com