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



我在https://github.com/cloudant/java-cloudant/blob/88202a1bd7b9b04d96c4b7b8498a1b8f7f99c9e5/src/test/java/com/cloudant/tests/Animal.java 中跟踪样本类Animal

我成功地管理了这个类的搜索索引查询。我想我有一个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"、"attribute Array"等属性集。。。"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;
    }

}

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

最新更新