List of usage examples for org.apache.lucene.search Sort Sort
public Sort(SortField... fields)
From source file:com.dreamerpartner.codereview.lucene.SearchHelper.java
License:Apache License
/** * /* w w w .j a v a 2 s . co m*/ * @param groupField * @param searchField * @param searchStr * @param pageNo * @param pageSize * @param orderField ? * @param orderFieldType ? * @param desc ? ?? * @return */ @SuppressWarnings("deprecation") public static Map<String, List<Document>> group(String module, String groupField, String searchField, String searchStr, int pageNo, int pageSize, String orderField, Type orderFieldType, boolean desc) { Map<String, List<Document>> result = new LinkedHashMap<String, List<Document>>(10); IndexReader reader = null; try { reader = DirectoryReader.open(FSDirectory.open(new File(LuceneUtil.getIndexPath(module)))); IndexSearcher indexSearcher = new IndexSearcher(reader); GroupingSearch groupingSearch = new GroupingSearch(groupField); Sort sort = new Sort(new SortField(orderField, orderFieldType, desc)); groupingSearch.setGroupSort(sort); groupingSearch.setSortWithinGroup(sort); groupingSearch.setFillSortFields(true); groupingSearch.setCachingInMB(4.0, true); groupingSearch.setAllGroups(true); //groupingSearch.setAllGroupHeads(true); groupingSearch.setGroupDocsLimit(pageSize); QueryParser parser = new QueryParser(Version.LUCENE_4_10_0, searchField, new StandardAnalyzer(Version.LUCENE_4_10_0)); Query query = parser.parse(searchStr); TopGroups<BytesRef> groupResult = groupingSearch.search(indexSearcher, query, (pageNo - 1) * pageSize, pageSize); System.out.println("?" + groupResult.totalHitCount + ", ?" + groupResult.groups.length); List<Document> groupData = null; for (GroupDocs<BytesRef> groupDocs : groupResult.groups) { groupData = new ArrayList<Document>(pageSize); String groupName = groupDocs.groupValue.utf8ToString(); for (ScoreDoc scoreDoc : groupDocs.scoreDocs) { groupData.add(indexSearcher.doc(scoreDoc.doc)); } result.put(groupName, groupData); groupData = null; } } catch (Exception e) { e.printStackTrace(); } finally { try { if (reader != null) reader.close(); } catch (IOException e) { e.printStackTrace(); } } return result; }
From source file:com.dreamerpartner.codereview.lucene.SearchHelper.java
License:Apache License
/** Simple command-line based search demo. */ @SuppressWarnings("deprecation") public static void main(String[] args) throws Exception { String module = "test"; String field = "contents"; String queryString = "produce"; int pageNo = 1; int pageSize = 10; Sort sort = new Sort(new SortField("createTime", Type.LONG)); IndexReader reader = DirectoryReader.open(FSDirectory.open(new File(LuceneUtil.getIndexPath(module)))); IndexSearcher searcher = new IndexSearcher(reader); // :Post-Release-Update-Version.LUCENE_XY: Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_4_10_0); // :Post-Release-Update-Version.LUCENE_XY: QueryParser parser = new QueryParser(Version.LUCENE_4_10_0, field, analyzer); Query query = parser.parse(queryString); System.out.println("Searching for: " + query.toString(field)); ///*w w w . j av a 2 s. c o m*/ PageBean<Document> docs = doPagingSearch(searcher, query, sort, pageNo, pageSize); System.out.println(JsonUtil.toString(docs)); reader.close(); }
From source file:com.dreamerpartner.codereview.lucene.SearchHelper.java
License:Apache License
/** * //from ww w.j a va 2 s. c o m * @param searcher * @param query * @param sort * @param pageNo * @param pageSize * @return * @throws IOException */ public static PageBean<Document> doPagingSearch(IndexSearcher searcher, Query query, Sort sort, int pageNo, int pageSize) throws IOException { if (sort == null) sort = new Sort(new SortField("id", Type.STRING)); int start = (pageNo - 1) * pageSize; int numHits = pageNo * pageSize; TopFieldCollector collector = TopFieldCollector.create(sort, numHits, false, false, false, false); searcher.search(query, collector); ScoreDoc[] hits = collector.topDocs(start, pageSize).scoreDocs; if (hits == null || hits.length < 1) return null; List<Document> docs = new ArrayList<Document>(pageSize); for (int i = 0; i < hits.length; i++) { Document doc = searcher.doc(hits[i].doc); docs.add(doc); } return new PageBean<Document>(pageSize, pageNo, collector.getTotalHits(), docs); }
From source file:com.duroty.application.bookmark.manager.BookmarkManager.java
License:Open Source License
/** * DOCUMENT ME!/*w w w .j av a 2 s.c o m*/ * * @param repositoryName DOCUMENT ME! * @param token DOCUMENT ME! * @param page DOCUMENT ME! * @param messagesByPage DOCUMENT ME! * @param order DOCUMENT ME! * @param orderType DOCUMENT ME! * * @return DOCUMENT ME! * * @throws BookmarkException DOCUMENT ME! * @throws SearchException DOCUMENT ME! */ public SearchObj search(String repositoryName, String token, int page, int messagesByPage, int order, String orderType, boolean isNotebook) throws BookmarkException { String lucenePath = ""; if (!defaultLucenePath.endsWith(File.separator)) { lucenePath = defaultLucenePath + File.separator + repositoryName + File.separator + Constants.BOOKMARK_LUCENE_BOOKMARK; } else { lucenePath = defaultLucenePath + repositoryName + File.separator + Constants.BOOKMARK_LUCENE_BOOKMARK; } Searcher searcher = null; SearchObj searchObj = new SearchObj(); Highlighter highlighter = null; try { searcher = BookmarkIndexer.getSearcher(lucenePath); Query query = null; Hits hits = null; if (StringUtils.isBlank(token)) { if (isNotebook) { query = SimpleQueryParser.parse("notebook:true", new KeywordAnalyzer()); } else { query = new MatchAllDocsQuery(); } hits = searcher.search(query, new Sort(new SortField[] { SortField.FIELD_SCORE, new SortField(Field_insert_date, SortField.STRING, true) })); } else { query = SimpleQueryParser.parse(token, analyzer); StringBuffer buffer = new StringBuffer(); if (isNotebook) { buffer.append("(" + query.toString() + ") AND "); QueryParser parser = new QueryParser(Field_notebook, new KeywordAnalyzer()); parser.setDefaultOperator(Operator.AND); Query aux = parser.parse(String.valueOf(true)); buffer.append("(" + aux.toString() + ") "); } if (buffer.length() > 0) { QueryParser parser = new QueryParser("", new WhitespaceAnalyzer()); query = parser.parse(buffer.toString()); } hits = searcher.search(query); } Date searchStart = new Date(); Date searchEnd = new Date(); //time in seconds double time = ((double) (searchEnd.getTime() - searchStart.getTime())) / (double) 1000; int hitsLength = hits.length(); if (hitsLength <= 0) { return null; } int start = page * messagesByPage; int end = start + messagesByPage; if (end > 0) { end = Math.min(hitsLength, end); } else { end = hitsLength; } if (start > end) { throw new SearchException("Search index of bound. start > end"); } Vector bookmarks = new Vector(); for (int j = start; j < end; j++) { Document doc = hits.doc(j); if (doc != null) { LuceneBookmark luceneBookmark = new LuceneBookmark(doc); SimpleHTMLFormatter formatter = new SimpleHTMLFormatter("<b>", "</b>"); highlighter = new Highlighter(formatter, new QueryScorer(query)); highlighter.setTextFragmenter(new SimpleFragmenter(150)); BookmarkObj bookmarkObj = new BookmarkObj(); bookmarkObj.setCacheDate(luceneBookmark.getCacheDate()); bookmarkObj.setComments(luceneBookmark.getComments()); bookmarkObj.setContents(luceneBookmark.getCotents()); bookmarkObj.setDepth(luceneBookmark.getDepth()); bookmarkObj.setFlagged(luceneBookmark.isFlagged()); bookmarkObj.setIdint(luceneBookmark.getIdint()); bookmarkObj.setInsertDate(luceneBookmark.getInsertDate()); bookmarkObj.setKeywords(luceneBookmark.getKeywords()); bookmarkObj.setNotebook(luceneBookmark.isNotebook()); bookmarkObj.setParent(Long.parseLong(luceneBookmark.getParent())); bookmarkObj.setTitle(luceneBookmark.getTitle()); bookmarkObj.setTitleHighlight(luceneBookmark.getTitle()); bookmarkObj.setUrl(luceneBookmark.getUrl()); String contents = luceneBookmark.getCotents(); String hcontents = null; if ((contents != null) && (!contents.trim().equals(""))) { contents = contents.replaceAll("\\s+", " "); TokenStream tokenStream = analyzer.tokenStream(Field_contents, new StringReader(contents)); hcontents = highlighter.getBestFragment(tokenStream, contents); if (hcontents != null) { contents = hcontents; } else { contents = null; } } bookmarkObj.setContentsHighlight(contents); String title = luceneBookmark.getTitle(); String htitle = null; if ((title != null) && (!title.trim().equals(""))) { title = title.replaceAll("\\s+", " "); TokenStream tokenStream = analyzer.tokenStream(Field_title, new StringReader(title)); htitle = highlighter.getBestFragment(tokenStream, title); if (htitle != null) { title = htitle; } } bookmarkObj.setTitleHighlight(title); bookmarks.addElement(bookmarkObj); } } searchObj.setHits(hitsLength); searchObj.setTime(time); searchObj.setBookmarks(bookmarks); } catch (Exception ex) { throw new SearchException(ex); } finally { } return searchObj; }
From source file:com.duroty.application.files.manager.SearchManager.java
License:Open Source License
/** * DOCUMENT ME!//from w w w .ja v a 2 s . c om * * @param hsession DOCUMENT ME! * @param repositoryName DOCUMENT ME! * @param token DOCUMENT ME! * @param page DOCUMENT ME! * @param messagesByPage DOCUMENT ME! * @param order DOCUMENT ME! * @param orderType DOCUMENT ME! * * @return DOCUMENT ME! * * @throws FilesException DOCUMENT ME! */ public SearchObj simple(Session hsession, String repositoryName, String token, String folderName, int label, int page, int messagesByPage, int order, String orderType) throws FilesException { String lucenePathMessages = null; if (!defaultLucenePath.endsWith(File.separator)) { lucenePathMessages = defaultLucenePath + File.separator + repositoryName + File.separator + Constants.MAIL_LUCENE_MESSAGES; } else { lucenePathMessages = defaultLucenePath + repositoryName + File.separator + Constants.MAIL_LUCENE_MESSAGES; } Searcher searcher = null; SearchObj searchObj = new SearchObj(); Sort sort = null; try { Users user = getUser(hsession, repositoryName); Locale locale = new Locale(user.getUseLanguage()); TimeZone timeZone = TimeZone.getDefault(); Date now = new Date(); Calendar calendar = Calendar.getInstance(timeZone, locale); calendar.setTime(now); SimpleDateFormat formatter1 = new SimpleDateFormat("MMM dd", locale); SimpleDateFormat formatter2 = new SimpleDateFormat("HH:mm:ss", locale); SimpleDateFormat formatter3 = new SimpleDateFormat("MM/yy", locale); searcher = MailIndexer.getSearcher(lucenePathMessages); Query query = null; if (!StringUtils.isBlank(folderName) && folderName.equals(this.folderFiles)) { BooleanQuery booleanQuery = new BooleanQuery(); QueryParser parser = new QueryParser(Field_subject, analyzer); parser.setDefaultOperator(Operator.AND); Query aux1 = parser.parse("Files-System"); booleanQuery.add(aux1, BooleanClause.Occur.MUST); Query aux2 = SimpleQueryParser.parse(token, analyzer); booleanQuery.add(aux2, BooleanClause.Occur.MUST); /*QueryParser parser2 = new QueryParser(Field_attachments, analyzer); parser2.setDefaultOperator(Operator.AND); Query aux3 = parser2.parse("Message Text"); booleanQuery.add(aux3, BooleanClause.Occur.MUST_NOT);*/ query = booleanQuery; } else { /*BooleanQuery booleanQuery = new BooleanQuery(); QueryParser parser = new QueryParser(Field_attachments, analyzer); parser.setDefaultOperator(Operator.AND); Query aux1 = parser.parse("Message Text"); booleanQuery.add(aux1, BooleanClause.Occur.MUST_NOT); Query aux2 = SimpleQueryParser.parse(token, analyzer); booleanQuery.add(aux2, BooleanClause.Occur.MUST); query = booleanQuery;*/ query = SimpleQueryParser.parse(token, analyzer); } Hits hits = null; Date searchStart = new Date(); boolean reverse = true; if (orderType.equals("ASC")) { reverse = false; } else { reverse = true; } switch (order) { case ORDER_BY_SIZE: sort = new Sort(new SortField[] { new SortField(Field_size, SortField.STRING, reverse), SortField.FIELD_SCORE }); break; case ORDER_BY_DATE: sort = new Sort(new SortField[] { new SortField(Field_lastDate, SortField.STRING, reverse), SortField.FIELD_SCORE }); break; default: sort = new Sort(new SortField[] { new SortField(null, SortField.SCORE, !reverse) }); break; } ChainedFilter chained = null; if (label > 0) { chained = getChainedFilter(hsession, repositoryName, label); } if (chained != null) { hits = searcher.search(query, chained, sort); } else { hits = searcher.search(query, sort); } Date searchEnd = new Date(); //time in seconds double time = ((double) (searchEnd.getTime() - searchStart.getTime())) / (double) 1000; int hitsLength = hits.length(); if (hitsLength <= 0) { return null; } int start = page * messagesByPage; int end = start + messagesByPage; if (end > 0) { end = Math.min(hitsLength, end); } else { end = hitsLength; } if (start > end) { throw new SearchException("Search index of bound. start > end"); } Vector files = new Vector(); for (int j = start; j < end; j++) { Document doc = hits.doc(j); if (doc != null) { LuceneMessage lmsg = new LuceneMessage(doc); Message message = getMessage(hsession, repositoryName, lmsg.getIdint()); if (message != null) { Set set = message.getAttachments(); if (set != null) { Iterator it = set.iterator(); while (it.hasNext()) { Attachment attachment = (Attachment) it.next(); String name = attachment.getAttName(); if (!StringUtils.isBlank(name) && !name.equals("Message Text")) { AttachmentObj obj = new AttachmentObj(); obj.setContentType(attachment.getAttContentType()); obj.setScore((int) (hits.score(j) * 100)); Date date = message.getMesDate(); if (date != null) { Calendar calendar2 = Calendar.getInstance(timeZone, locale); calendar2.setTime(date); if ((calendar.get(Calendar.YEAR) == calendar2.get(Calendar.YEAR)) && (calendar.get(Calendar.MONTH) == calendar2.get(Calendar.MONTH)) && (calendar.get(Calendar.DATE) == calendar2.get(Calendar.DATE))) { obj.setDateStr(formatter2.format(calendar2.getTime())); } else if (calendar.get(Calendar.YEAR) == calendar2.get(Calendar.YEAR)) { obj.setDateStr(formatter1.format(calendar2.getTime())); } else { obj.setDateStr(formatter3.format(calendar2.getTime())); } } obj.setDate(date); obj.setDate(date); obj.setIdint(attachment.getAttIdint()); obj.setName(attachment.getAttName()); obj.setPart(attachment.getAttPart()); int size = attachment.getAttSize(); size /= 1024; if (size > 1024) { size /= 1024; obj.setSize(size + " MB"); } else { obj.setSize(((size > 0) ? (size + "") : "<1") + " kB"); } String extension = (String) this.extensions.get(attachment.getAttContentType()); if (StringUtils.isBlank(extension)) { extension = "generic"; } obj.setExtension(extension); if (message.isMesFlagged()) { obj.setFlagged(true); } else { obj.setFlagged(false); } if (message.getLabMeses() != null) { Iterator it2 = message.getLabMeses().iterator(); StringBuffer lab = new StringBuffer(); while (it2.hasNext()) { if (lab.length() > 0) { lab.append(", "); } LabMes labMes = (LabMes) it2.next(); lab.append(labMes.getId().getLabel().getLabName()); } if (lab.length() > 0) { obj.setLabel(lab.toString()); } else { } } obj.setBox(message.getMesBox()); obj.setMid(message.getMesName()); files.addElement(obj); } } } } } } searchObj.setHits(hitsLength); searchObj.setTime(time); searchObj.setFiles(files); } catch (Exception ex) { throw new FilesException(ex); } finally { GeneralOperations.closeHibernateSession(hsession); } return searchObj; }
From source file:com.duroty.application.files.manager.SearchManager.java
License:Open Source License
public SearchObj advanced(Session hsession, String repositoryName, AdvancedObj advancedObj, int page, int messagesByPage, int order, String orderType) throws FilesException { String lucenePathMessages = null; if (!defaultLucenePath.endsWith(File.separator)) { lucenePathMessages = defaultLucenePath + File.separator + repositoryName + File.separator + Constants.MAIL_LUCENE_MESSAGES; } else {/*from w ww.j a v a 2s. com*/ lucenePathMessages = defaultLucenePath + repositoryName + File.separator + Constants.MAIL_LUCENE_MESSAGES; } Searcher searcher = null; SearchObj searchObj = new SearchObj(); Sort sort = null; try { Users user = getUser(hsession, repositoryName); Locale locale = new Locale(user.getUseLanguage()); TimeZone timeZone = TimeZone.getDefault(); Date now = new Date(); Calendar calendar = Calendar.getInstance(timeZone, locale); calendar.setTime(now); SimpleDateFormat formatter1 = new SimpleDateFormat("MMM dd", locale); SimpleDateFormat formatter2 = new SimpleDateFormat("HH:mm:ss", locale); SimpleDateFormat formatter3 = new SimpleDateFormat("MM/yy", locale); searcher = MailIndexer.getSearcher(lucenePathMessages); if (!StringUtils.isBlank(advancedObj.getSubject())) { advancedObj.setSubject("Files-System"); } Query query = AdvancedQueryParser.parseMessages(advancedObj, analyzer); ; /*BooleanQuery booleanQuery = new BooleanQuery(); QueryParser parser = new QueryParser(Field_attachments, analyzer); parser.setDefaultOperator(Operator.AND); Query aux1 = parser.parse("\"Message Text\""); booleanQuery.add(aux1, BooleanClause.Occur.MUST_NOT); Query aux2 = AdvancedQueryParser.parseMessages(advancedObj, analyzer); booleanQuery.add(aux2, BooleanClause.Occur.MUST); query = booleanQuery;*/ Hits hits = null; Date searchStart = new Date(); boolean reverse = true; if (orderType.equals("ASC")) { reverse = false; } else { reverse = true; } switch (order) { case ORDER_BY_SIZE: sort = new Sort(new SortField[] { new SortField(Field_size, SortField.STRING, reverse), SortField.FIELD_SCORE }); break; case ORDER_BY_DATE: sort = new Sort(new SortField[] { new SortField(Field_lastDate, SortField.STRING, reverse), SortField.FIELD_SCORE }); break; default: sort = new Sort(new SortField[] { new SortField(null, SortField.SCORE, !reverse) }); break; } int label = 0; if (advancedObj.getLabel() != null) { label = advancedObj.getLabel().getIdint(); } ChainedFilter chained = null; if (label > 0) { chained = getChainedFilter(hsession, repositoryName, label); } if (chained != null) { hits = searcher.search(query, chained, sort); } else { hits = searcher.search(query, sort); } Date searchEnd = new Date(); //time in seconds double time = ((double) (searchEnd.getTime() - searchStart.getTime())) / (double) 1000; int hitsLength = hits.length(); if (hitsLength <= 0) { return null; } int start = page * messagesByPage; int end = start + messagesByPage; if (end > 0) { end = Math.min(hitsLength, end); } else { end = hitsLength; } if (start > end) { throw new SearchException("Search index of bound. start > end"); } Vector files = new Vector(); for (int j = start; j < end; j++) { Document doc = hits.doc(j); if (doc != null) { LuceneMessage lmsg = new LuceneMessage(doc); Message message = getMessage(hsession, repositoryName, lmsg.getIdint()); if (message != null) { Set set = message.getAttachments(); if (set != null) { Iterator it = set.iterator(); while (it.hasNext()) { Attachment attachment = (Attachment) it.next(); String name = attachment.getAttName(); if (!StringUtils.isBlank(name) && !name.equals("Message Text")) { AttachmentObj obj = new AttachmentObj(); obj.setContentType(attachment.getAttContentType()); obj.setScore((int) (hits.score(j) * 100)); Date date = message.getMesDate(); if (date != null) { Calendar calendar2 = Calendar.getInstance(timeZone, locale); calendar2.setTime(date); if ((calendar.get(Calendar.YEAR) == calendar2.get(Calendar.YEAR)) && (calendar.get(Calendar.MONTH) == calendar2.get(Calendar.MONTH)) && (calendar.get(Calendar.DATE) == calendar2.get(Calendar.DATE))) { obj.setDateStr(formatter2.format(calendar2.getTime())); } else if (calendar.get(Calendar.YEAR) == calendar2.get(Calendar.YEAR)) { obj.setDateStr(formatter1.format(calendar2.getTime())); } else { obj.setDateStr(formatter3.format(calendar2.getTime())); } } obj.setDate(date); obj.setDate(date); obj.setIdint(attachment.getAttIdint()); obj.setName(attachment.getAttName()); obj.setPart(attachment.getAttPart()); int size = attachment.getAttSize(); size /= 1024; if (size > 1024) { size /= 1024; obj.setSize(size + " MB"); } else { obj.setSize(((size > 0) ? (size + "") : "<1") + " kB"); } String extension = (String) this.extensions.get(attachment.getAttContentType()); if (StringUtils.isBlank(extension)) { extension = "generic"; } obj.setExtension(extension); if (message.isMesFlagged()) { obj.setFlagged(true); } else { obj.setFlagged(false); } if (message.getLabMeses() != null) { Iterator it2 = message.getLabMeses().iterator(); StringBuffer lab = new StringBuffer(); while (it2.hasNext()) { if (lab.length() > 0) { lab.append(", "); } LabMes labMes = (LabMes) it2.next(); lab.append(labMes.getId().getLabel().getLabName()); } if (lab.length() > 0) { obj.setLabel(lab.toString()); } else { } } obj.setBox(message.getMesBox()); obj.setMid(message.getMesName()); files.addElement(obj); } } } } } } searchObj.setHits(hitsLength); searchObj.setTime(time); searchObj.setFiles(files); } catch (Exception ex) { throw new FilesException(ex); } finally { GeneralOperations.closeHibernateSession(hsession); } return searchObj; }
From source file:com.duroty.application.mail.manager.SearchManager.java
License:Open Source License
/** * DOCUMENT ME!/*from ww w . j av a2s . c o m*/ * * @param hsession DOCUMENT ME! * @param repositoryName DOCUMENT ME! * @param token DOCUMENT ME! * @param page DOCUMENT ME! * @param messagesByPage DOCUMENT ME! * @param order DOCUMENT ME! * @param orderType DOCUMENT ME! * * @return DOCUMENT ME! * * @throws MailException DOCUMENT ME! */ public SearchObj simple(Session hsession, String repositoryName, String token, int page, int messagesByPage, int order, String orderType, boolean excludeTrash) throws MailException { String lucenePathMessages = null; if (!defaultLucenePath.endsWith(File.separator)) { lucenePathMessages = defaultLucenePath + File.separator + repositoryName + File.separator + Constants.MAIL_LUCENE_MESSAGES; } else { lucenePathMessages = defaultLucenePath + repositoryName + File.separator + Constants.MAIL_LUCENE_MESSAGES; } Searcher searcher = null; SearchObj searchObj = new SearchObj(); Sort sort = null; try { Users user = getUser(hsession, repositoryName); Locale locale = new Locale(user.getUseLanguage()); TimeZone timeZone = TimeZone.getDefault(); Date now = new Date(); Calendar calendar = Calendar.getInstance(timeZone, locale); calendar.setTime(now); SimpleDateFormat formatter1 = new SimpleDateFormat("MMM dd", locale); SimpleDateFormat formatter2 = new SimpleDateFormat("HH:mm:ss", locale); SimpleDateFormat formatter3 = new SimpleDateFormat("MM/yy", locale); searcher = MailIndexer.getSearcher(lucenePathMessages); Query query = SimpleQueryParser.parse(token, analyzer); Hits hits = null; Date searchStart = new Date(); boolean reverse = true; if (orderType.equals("ASC")) { reverse = false; } else { reverse = true; } switch (order) { case ORDER_BY_ADDRESS: sort = new Sort(new SortField[] { new SortField(Field_from, SortField.STRING, reverse), SortField.FIELD_SCORE }); break; case ORDER_BY_SIZE: sort = new Sort(new SortField[] { new SortField(Field_size, SortField.STRING, reverse), SortField.FIELD_SCORE }); break; case ORDER_BY_SUBJECT: sort = new Sort(new SortField[] { new SortField(Field_subject, SortField.STRING, reverse), SortField.FIELD_SCORE }); break; case ORDER_BY_DATE: sort = new Sort(new SortField[] { new SortField(Field_lastDate, SortField.STRING, reverse), SortField.FIELD_SCORE }); break; case ORDER_BY_UNREAD: sort = new Sort(new SortField[] { new SortField(Field_recent, SortField.STRING, reverse), SortField.FIELD_SCORE }); break; case ORDER_BY_IMPORTANT: sort = new Sort(new SortField[] { new SortField(Field_flagged, SortField.STRING, reverse), SortField.FIELD_SCORE }); break; default: sort = new Sort(new SortField[] { SortField.FIELD_SCORE, new SortField(Field_lastDate, SortField.STRING, true) }); break; } ChainedFilter chained = null; if (excludeTrash) { chained = getChainedFilter(hsession, repositoryName, 0, null); } if (chained != null) { hits = searcher.search(query, chained, sort); } else { hits = searcher.search(query, sort); } Date searchEnd = new Date(); //time in seconds double time = ((double) (searchEnd.getTime() - searchStart.getTime())) / (double) 1000; int hitsLength = hits.length(); if (hitsLength <= 0) { return null; } int start = page * messagesByPage; int end = start + messagesByPage; if (end > 0) { end = Math.min(hitsLength, end); } else { end = hitsLength; } if (start > end) { throw new SearchException("Search index of bound. start > end"); } Vector messages = new Vector(); for (int j = start; j < end; j++) { Document doc = hits.doc(j); if (doc != null) { LuceneMessage lmsg = new LuceneMessage(doc); Message message = getMessage(hsession, repositoryName, lmsg.getIdint()); if (message != null) { MessageObj obj = new MessageObj(message.getMesName()); obj.setBox(message.getMesBox()); obj.setFrom(message.getMesFrom()); if ((message.getAttachments() != null) && (message.getAttachments().size() > 0)) { obj.setHasAttachment(true); } else { obj.setHasAttachment(false); } int size = message.getMesSize(); size /= 1024; if (size > 1024) { size /= 1024; obj.setSize(size + " MB"); } else { obj.setSize(((size > 0) ? (size + "") : "<1") + " kB"); } if (message.getMesBox().equals(folderSent)) { try { obj.setEmail(message.getMesTo()); } catch (Exception e) { obj.setEmail("unknown to"); } } else { obj.setEmail(message.getMesFrom()); } Date date = message.getMesDate(); if (date != null) { Calendar calendar2 = Calendar.getInstance(timeZone, locale); calendar2.setTime(date); if ((calendar.get(Calendar.YEAR) == calendar2.get(Calendar.YEAR)) && (calendar.get(Calendar.MONTH) == calendar2.get(Calendar.MONTH)) && (calendar.get(Calendar.DATE) == calendar2.get(Calendar.DATE))) { obj.setDateStr(formatter2.format(calendar2.getTime())); } else if ((calendar.get(Calendar.YEAR) == calendar2.get(Calendar.YEAR)) && (calendar.get(Calendar.MONTH) == calendar2.get(Calendar.MONTH))) { obj.setDateStr(formatter1.format(calendar2.getTime())); } else { obj.setDateStr(formatter3.format(calendar2.getTime())); } } obj.setDate(date); if (message.getLabMeses() != null) { Iterator it = message.getLabMeses().iterator(); StringBuffer buff = new StringBuffer(); while (it.hasNext()) { if (buff.length() > 0) { buff.append(", "); } LabMes labMes = (LabMes) it.next(); buff.append(labMes.getId().getLabel().getLabName()); } obj.setLabel(buff.toString()); } try { obj.setSubject(message.getMesSubject()); } catch (Exception ex) { obj.setSubject("no subject"); } if (message.isMesFlagged()) { obj.setFlagged(true); } else { obj.setFlagged(false); } if (message.isMesRecent()) { obj.setRecent(true); } else { obj.setRecent(false); } String priority = "normal"; if (MessageUtilities.isHighPriority(message.getMesHeaders())) { priority = "high"; } else if (MessageUtilities.isLowPriority(message.getMesHeaders())) { priority = "low"; } obj.setPriority(priority); messages.addElement(obj); } } } searchObj.setHits(hitsLength); searchObj.setTime(time); searchObj.setMessages(messages); } catch (Exception ex) { throw new MailException(ex); } finally { GeneralOperations.closeHibernateSession(hsession); } return searchObj; }
From source file:com.duroty.application.mail.manager.SearchManager.java
License:Open Source License
/** * DOCUMENT ME!/*from w w w . ja v a2s . c o m*/ * * @param hsession DOCUMENT ME! * @param repositoryName DOCUMENT ME! * @param advancedObj DOCUMENT ME! * @param page DOCUMENT ME! * @param messagesByPage DOCUMENT ME! * @param order DOCUMENT ME! * @param orderType DOCUMENT ME! * * @return DOCUMENT ME! * * @throws MailException DOCUMENT ME! */ public SearchObj advanced(Session hsession, String repositoryName, AdvancedObj advancedObj, int page, int messagesByPage, int order, String orderType) throws MailException { String lucenePathMessages = null; if (!defaultLucenePath.endsWith(File.separator)) { lucenePathMessages = defaultLucenePath + File.separator + repositoryName + File.separator + Constants.MAIL_LUCENE_MESSAGES; } else { lucenePathMessages = defaultLucenePath + repositoryName + File.separator + Constants.MAIL_LUCENE_MESSAGES; } Searcher searcher = null; SearchObj searchObj = new SearchObj(); Sort sort = null; try { Users user = getUser(hsession, repositoryName); Locale locale = new Locale(user.getUseLanguage()); TimeZone timeZone = TimeZone.getDefault(); Date now = new Date(); Calendar calendar = Calendar.getInstance(timeZone, locale); calendar.setTime(now); SimpleDateFormat formatter1 = new SimpleDateFormat("MMM dd", locale); SimpleDateFormat formatter2 = new SimpleDateFormat("HH:mm:ss", locale); SimpleDateFormat formatter3 = new SimpleDateFormat("MM/yy", locale); searcher = MailIndexer.getSearcher(lucenePathMessages); Query query = AdvancedQueryParser.parseMessages(advancedObj, analyzer); Hits hits = null; Date searchStart = new Date(); boolean reverse = true; if (orderType.equals("ASC")) { reverse = false; } else { reverse = true; } switch (order) { case ORDER_BY_ADDRESS: sort = new Sort(new SortField[] { new SortField(Field_from, SortField.STRING, reverse), SortField.FIELD_SCORE }); break; case ORDER_BY_SIZE: sort = new Sort(new SortField[] { new SortField(Field_size, SortField.STRING, reverse), SortField.FIELD_SCORE }); break; case ORDER_BY_SUBJECT: sort = new Sort(new SortField[] { new SortField(Field_subject, SortField.STRING, reverse), SortField.FIELD_SCORE }); break; case ORDER_BY_DATE: sort = new Sort(new SortField[] { new SortField(Field_lastDate, SortField.STRING, reverse), SortField.FIELD_SCORE }); break; case ORDER_BY_UNREAD: sort = new Sort(new SortField[] { new SortField(Field_recent, SortField.STRING, reverse), SortField.FIELD_SCORE }); break; case ORDER_BY_IMPORTANT: sort = new Sort(new SortField[] { new SortField(Field_flagged, SortField.STRING, reverse), SortField.FIELD_SCORE }); break; default: sort = new Sort(new SortField[] { SortField.FIELD_SCORE, new SortField(Field_lastDate, SortField.STRING, true) }); break; } int label = 0; if (advancedObj.getLabel() != null) { label = advancedObj.getLabel().getIdint(); } String box = advancedObj.getBox(); if ((box == null) || box.equals("0")) { box = this.folderAll; } ChainedFilter chained = null; if ((label > 0) || (box != null)) { chained = getChainedFilter(hsession, repositoryName, label, box); if (chained == null) { return null; } } if ((query == null) && (chained == null)) { return null; } if (query == null) { query = new MatchAllDocsQuery(); } else { } if (chained != null) { hits = searcher.search(query, chained, sort); } else { hits = searcher.search(query, sort); } Date searchEnd = new Date(); //time in seconds double time = ((double) (searchEnd.getTime() - searchStart.getTime())) / (double) 1000; int hitsLength = hits.length(); if (hitsLength <= 0) { return null; } int start = page * messagesByPage; int end = start + messagesByPage; if (end > 0) { end = Math.min(hitsLength, end); } else { end = hitsLength; } if (start > end) { throw new SearchException("Search index of bound. start > end"); } Vector messages = new Vector(); for (int j = start; j < end; j++) { Document doc = hits.doc(j); if (doc != null) { LuceneMessage lmsg = new LuceneMessage(doc); Message message = getMessage(hsession, repositoryName, lmsg.getIdint()); if (message != null) { MessageObj obj = new MessageObj(message.getMesName()); obj.setBox(message.getMesBox()); obj.setFrom(message.getMesFrom()); if ((message.getAttachments() != null) && (message.getAttachments().size() > 0)) { obj.setHasAttachment(true); } else { obj.setHasAttachment(false); } int size = message.getMesSize(); size /= 1024; if (size > 1024) { size /= 1024; obj.setSize(size + " MB"); } else { obj.setSize(((size > 0) ? (size + "") : "<1") + " kB"); } if (message.getMesBox().equals(folderSent)) { try { obj.setEmail(message.getMesTo()); } catch (Exception e) { obj.setEmail("unknown to"); } } else { obj.setEmail(message.getMesFrom()); } Date date = message.getMesDate(); if (date != null) { Calendar calendar2 = Calendar.getInstance(timeZone, locale); calendar2.setTime(date); if ((calendar.get(Calendar.YEAR) == calendar2.get(Calendar.YEAR)) && (calendar.get(Calendar.MONTH) == calendar2.get(Calendar.MONTH)) && (calendar.get(Calendar.DATE) == calendar2.get(Calendar.DATE))) { obj.setDateStr(formatter2.format(calendar2.getTime())); } else if ((calendar.get(Calendar.YEAR) == calendar2.get(Calendar.YEAR)) && (calendar.get(Calendar.MONTH) == calendar2.get(Calendar.MONTH))) { obj.setDateStr(formatter1.format(calendar2.getTime())); } else { obj.setDateStr(formatter3.format(calendar2.getTime())); } } obj.setDate(date); if (message.getLabMeses() != null) { Iterator it = message.getLabMeses().iterator(); StringBuffer buff = new StringBuffer(); while (it.hasNext()) { if (buff.length() > 0) { buff.append(", "); } LabMes labMes = (LabMes) it.next(); buff.append(labMes.getId().getLabel().getLabName()); } obj.setLabel(buff.toString()); } try { obj.setSubject(message.getMesSubject()); } catch (Exception ex) { obj.setSubject("no subject"); } if (message.isMesFlagged()) { obj.setFlagged(true); } else { obj.setFlagged(false); } if (message.isMesRecent()) { obj.setRecent(true); } else { obj.setRecent(false); } String priority = "normal"; if (MessageUtilities.isHighPriority(message.getMesHeaders())) { priority = "high"; } else if (MessageUtilities.isLowPriority(message.getMesHeaders())) { priority = "low"; } obj.setPriority(priority); messages.addElement(obj); } } } searchObj.setHits(hitsLength); searchObj.setTime(time); searchObj.setMessages(messages); } catch (Exception ex) { throw new MailException(ex); } finally { GeneralOperations.closeHibernateSession(hsession); } return searchObj; }
From source file:com.duroty.application.mail.manager.SearchManager.java
License:Open Source License
/** * DOCUMENT ME!// w w w. ja va 2s . c om * * @param hsession DOCUMENT ME! * @param repositoryName DOCUMENT ME! * @param filterObj DOCUMENT ME! * @param page DOCUMENT ME! * @param messagesByPage DOCUMENT ME! * @param order DOCUMENT ME! * @param orderType DOCUMENT ME! * * @return DOCUMENT ME! * * @throws MailException DOCUMENT ME! */ public SearchObj testFilter(Session hsession, String repositoryName, FilterObj filter, int page, int messagesByPage, int order, String orderType) throws MailException { String lucenePathMessages = null; if (!defaultLucenePath.endsWith(File.separator)) { lucenePathMessages = defaultLucenePath + File.separator + repositoryName + File.separator + Constants.MAIL_LUCENE_MESSAGES; } else { lucenePathMessages = defaultLucenePath + repositoryName + File.separator + Constants.MAIL_LUCENE_MESSAGES; } Searcher searcher = null; SearchObj searchObj = new SearchObj(); Sort sort = null; try { Users user = getUser(hsession, repositoryName); Locale locale = new Locale(user.getUseLanguage()); TimeZone timeZone = TimeZone.getDefault(); Date now = new Date(); Calendar calendar = Calendar.getInstance(timeZone, locale); calendar.setTime(now); SimpleDateFormat formatter1 = new SimpleDateFormat("MMM dd", locale); SimpleDateFormat formatter2 = new SimpleDateFormat("HH:mm:ss", locale); SimpleDateFormat formatter3 = new SimpleDateFormat("MM/yy", locale); searcher = MailIndexer.getSearcher(lucenePathMessages); Filter hfilter = new Filter(); hfilter.setFilArchive(filter.isArchive()); hfilter.setFilDoesntHaveWords(filter.getDoesntHaveWords()); hfilter.setFilForwardTo(filter.getForward()); hfilter.setFilFrom(filter.getFrom()); hfilter.setFilHasAttacment(filter.isHasAttachment()); hfilter.setFilHasWords(filter.getHasWords()); hfilter.setFilImportant(filter.isImportant()); hfilter.setFilSubject(filter.getSubject()); hfilter.setFilTo(filter.getTo()); hfilter.setFilTrash(filter.isTrash()); hfilter.setFilOrOperator(!filter.isOperator()); Query query = FilterQueryParser.parse(hfilter, analyzer); Hits hits = null; Date searchStart = new Date(); boolean reverse = true; if (orderType.equals("ASC")) { reverse = false; } else { reverse = true; } switch (order) { case ORDER_BY_ADDRESS: sort = new Sort(new SortField[] { new SortField(Field_from, SortField.STRING, reverse), SortField.FIELD_SCORE }); break; case ORDER_BY_SIZE: sort = new Sort(new SortField[] { new SortField(Field_size, SortField.STRING, reverse), SortField.FIELD_SCORE }); break; case ORDER_BY_SUBJECT: sort = new Sort(new SortField[] { new SortField(Field_subject, SortField.STRING, reverse), SortField.FIELD_SCORE }); break; case ORDER_BY_DATE: sort = new Sort(new SortField[] { new SortField(Field_lastDate, SortField.STRING, reverse), SortField.FIELD_SCORE }); break; default: sort = new Sort(new SortField[] { SortField.FIELD_SCORE, new SortField(Field_lastDate, SortField.STRING, true) }); break; } hits = searcher.search(query, sort); Date searchEnd = new Date(); //time in seconds double time = ((double) (searchEnd.getTime() - searchStart.getTime())) / (double) 1000; int hitsLength = hits.length(); if (hitsLength <= 0) { return null; } int start = page * messagesByPage; int end = start + messagesByPage; if (end > 0) { end = Math.min(hitsLength, end); } else { end = hitsLength; } if (start > end) { throw new SearchException("Search index of bound. start > end"); } Vector messages = new Vector(); for (int j = start; j < end; j++) { Document doc = hits.doc(j); if (doc != null) { LuceneMessage lmsg = new LuceneMessage(doc); Message message = getMessage(hsession, repositoryName, lmsg.getIdint()); if (message != null) { MessageObj obj = new MessageObj(message.getMesName()); obj.setBox(message.getMesBox()); obj.setFrom(message.getMesFrom()); if ((message.getAttachments() != null) && (message.getAttachments().size() > 0)) { obj.setHasAttachment(true); } else { obj.setHasAttachment(false); } int size = message.getMesSize(); size /= 1024; if (size > 1024) { size /= 1024; obj.setSize(size + " MB"); } else { obj.setSize(((size > 0) ? (size + "") : "<1") + " kB"); } if (message.getMesBox().equals(folderSent)) { try { obj.setEmail(message.getMesTo()); } catch (Exception e) { obj.setEmail("unknown to"); } } else { obj.setEmail(message.getMesFrom()); } Date date = message.getMesDate(); if (date != null) { Calendar calendar2 = Calendar.getInstance(timeZone, locale); calendar2.setTime(date); if ((calendar.get(Calendar.YEAR) == calendar2.get(Calendar.YEAR)) && (calendar.get(Calendar.MONTH) == calendar2.get(Calendar.MONTH)) && (calendar.get(Calendar.DATE) == calendar2.get(Calendar.DATE))) { obj.setDateStr(formatter2.format(calendar2.getTime())); } else if ((calendar.get(Calendar.YEAR) == calendar2.get(Calendar.YEAR)) && (calendar.get(Calendar.MONTH) == calendar2.get(Calendar.MONTH))) { obj.setDateStr(formatter1.format(calendar2.getTime())); } else { obj.setDateStr(formatter3.format(calendar2.getTime())); } } obj.setDate(date); if (message.getLabMeses() != null) { Iterator it = message.getLabMeses().iterator(); StringBuffer buff = new StringBuffer(); while (it.hasNext()) { if (buff.length() > 0) { buff.append(", "); } LabMes labMes = (LabMes) it.next(); buff.append(labMes.getId().getLabel().getLabName()); } obj.setLabel(buff.toString()); } try { obj.setSubject(message.getMesSubject()); } catch (Exception ex) { obj.setSubject("no subject"); } if (message.isMesFlagged()) { obj.setFlagged(true); } else { obj.setFlagged(false); } if (message.isMesRecent()) { obj.setRecent(true); } else { obj.setRecent(false); } String priority = "normal"; if (MessageUtilities.isHighPriority(message.getMesHeaders())) { priority = "high"; } else if (MessageUtilities.isLowPriority(message.getMesHeaders())) { priority = "low"; } obj.setPriority(priority); messages.addElement(obj); } } } searchObj.setHits(hitsLength); searchObj.setTime(time); searchObj.setMessages(messages); } catch (Exception ex) { throw new MailException(ex); } finally { GeneralOperations.closeHibernateSession(hsession); } return searchObj; }
From source file:com.edgenius.test.lucene.TestLucene.java
License:Open Source License
public void testSort() throws ParseException, IOException { QueryParser p = new QueryParser(LuceneConfig.VERSION, "dsc", new StandardAnalyzer(LuceneConfig.VERSION)); Query q = p.parse("hello"); TopFieldDocs hits = searcher.search(q, LuceneConfig.MAX_RETURN, new Sort(new SortField("ds", SortField.STRING))); assertEquals(4, hits.totalHits);/*from w w w .ja v a 2 s. c om*/ assertEquals("123", searcher.doc(hits.scoreDocs[0].doc).get("ds")); assertEquals("abc", searcher.doc(hits.scoreDocs[1].doc).get("ds")); assertEquals("def", searcher.doc(hits.scoreDocs[2].doc).get("ds")); assertEquals("xyz", searcher.doc(hits.scoreDocs[3].doc).get("ds")); }