Advertisement
Guest User

Untitled

a guest
Jun 24th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. public void createAndInsertInverseIndex() {
  2.  
  3. MongoCollection<Document> collection = database.getCollection(indexName);
  4. MongoCollection<Document> inverseIndexcollection = database.getCollection(inverseIndexName);
  5. try (MongoCursor<Document> cur = collection.find().iterator()) {
  6. while (cur.hasNext()) {
  7. Document doc = cur.next();
  8.  
  9. String docName = doc.getString("docID");
  10. List<Document> idxEntries = (List<Document>) doc.get("wordsList");
  11.  
  12. BasicDBObject basic = new BasicDBObject();
  13. BasicDBObject basic1 = new BasicDBObject();
  14. BasicDBObject basic2 = new BasicDBObject();
  15. List<String> docsList;
  16. //System.out.println(docName);
  17.  
  18. for(int i=0;i<idxEntries.size();i++)
  19. {
  20.  
  21. basic1.append("wordID",idxEntries.get(i).getString("word"));
  22.  
  23. // MongoCursor<Document> cursor = inverseIndexcollection.find(basic1).first();
  24.  
  25. if ( inverseIndexcollection.find(basic1).first()== null)
  26. {
  27.  
  28. //System.out.println("insert ");
  29. docsList = new ArrayList<String>();
  30. docsList.add(docName);
  31. Document document = new Document( basic1.append("docsList", docsList));
  32. inverseIndexcollection.insertOne(document);
  33.  
  34. docsList.clear();
  35. document.clear();
  36.  
  37. }
  38. else
  39. {
  40. //System.out.println("update ");
  41. basic.append("docsList", docName);
  42. basic2.append("$addToSet", basic);
  43. inverseIndexcollection.updateOne(basic1,basic2);
  44. basic.clear();basic2.clear();
  45. }
  46. basic1.clear();
  47. }
  48.  
  49.  
  50. }
  51. }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement