Exemple #1
0
  public static List<String> queryMongoForSimilar(String courtId, String facets) throws Exception {
    // Query mongo for other facets with these values.
    System.out.println("Court id is: " + courtId);
    initializeMap();
    List<String> facetList = facetStringToListOfKeys(facets);
    // Facet list has the keys to get. Query mongo for the document with given courtId, then pull
    // the values of these keys.

    MongoConnection mc = new MongoConnection("supremeCourtDataDB.properties");
    MongoCollection<Document> collection = mc.getCollection();

    int courtIdInt = Integer.parseInt(courtId);
    Document d = collection.find(eq("courtId", courtIdInt)).first();
    System.out.println("Doc with this court id: " + d);

    Document queryDoc = new Document();
    // Build query based on given keys and their values in this doc.
    for (int i = 0; i < facetList.size(); i++) {
      String key = facetList.get(i);
      String tempString;
      int tempInt;
      try {
        tempString = d.getString(key);
        // queryJSON.put(key, tempString);
        queryDoc.put(key, tempString);
      } catch (ClassCastException c) {
        tempInt = d.getInteger(key);
        queryDoc.put(key, tempInt);
        // queryJSON.put(key, tempInt);
      }
    }

    List<String> resultList = new ArrayList<String>();

    System.out.println("Querying mongo.");
    FindIterable<Document> result = collection.find(queryDoc);

    for (Document doc : result) {
      String temp = Integer.toString(doc.getInteger("courtId"));
      resultList.add(temp);
    }

    System.out.println("Done!");
    return resultList;
  }