public Object[] getIndexes(UserInfo user) { Map<Object, Object> map = Memcache.getAll(user.getIndexes()); if (map.size() < user.getIndexes().size()) { // some indexes are out of Memcache. List<Index> idxList = Datastore.get(i, user.getIndexes()); for (Index idx : idxList) { if (!map.containsValue(idx)) { Memcache.put(idx.getKey(), idx); // put to Memcache } } return idxList.toArray(); } else { return map.values().toArray(); } }
public List<Map<String, String>> Search(Map<String, Object> input) throws CorruptIndexException, IOException, ClassNotFoundException, ParseException, NotAuthorizedException { String email = (String) input.get("email"); String token = (String) input.get("auth"); // long start; // start = System.currentTimeMillis(); // log.info("Search start:" + start); UserInfo user = Util.findUser(email); if (!user.getTokens().contains(token)) { throw new NotAuthorizedException(); } // get indexes. Object[] result = getIndexes(user); // log.info("Search get Indexes:" + (System.currentTimeMillis() - start)); // create a MultiReader from indexes. IndexReader[] idxReaders = new IndexReader[result.length]; int c = 0; if (result.length == 0) { return null; } for (Object iO : result) { try { Index idx = (Index) iO; IndexReader r = IndexReader.open(idx.getIndex(), true); // create Read only indexreader. idxReaders[c] = r; c++; } catch (ClassCastException e) { continue; // just skip } } // log.info("Search get multiple index:" + (System.currentTimeMillis() - start)); MultiReader mReader = new MultiReader(idxReaders, true); IndexSearcher searcher = new IndexSearcher(mReader); // log.info("Search get MultiReader:" + (System.currentTimeMillis() - start)); Query query; String q; if (input.containsKey("schedule")) { q = "\\[????\\-??\\-??\\]"; Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_CURRENT); query = new QueryParser(Version.LUCENE_CURRENT, "text", analyzer).parse(q); } else { q = (String) input.get("q"); query = new QueryParser(Version.LUCENE_CURRENT, "text", new NGramAnalyzerForQuery(1, 3)).parse(q); } System.out.println("Query:" + query.toString()); // log.info("Search start:" + (System.currentTimeMillis() - start)); TopDocs td = searcher.search(query, MAX_RESULT_SIZE); // log.info("Search end:" + (System.currentTimeMillis() - start)); // get note keys Document doc; c = 0; String[] keys = new String[td.scoreDocs.length]; System.out.println("result: " + td.scoreDocs.length); for (ScoreDoc sd : td.scoreDocs) { doc = searcher.doc(sd.doc); keys[c] = doc.get("key"); c++; } // log.info("Search get doc:" + (System.currentTimeMillis() - start)); List<Note> notes = findNotes(keys); List<Map<String, String>> retval = new ArrayList<Map<String, String>>(); for (Note note : notes) { Map<String, String> map = new HashMap<String, String>(); map.put("title", note.getTitle()); map.put("text", note.getText()); map.put("created", note.getCreatedDate().toString()); map.put("noteid", note.getNoteId()); retval.add(map); } // log.info("Search create Return:" + (System.currentTimeMillis() - start)); return retval; }