List of usage examples for org.apache.lucene.search IndexSearcher IndexSearcher
public IndexSearcher(IndexReaderContext context)
From source file:com.dreamerpartner.codereview.lucene.SearchHelper.java
License:Apache License
/** * ?/*from w ww . ja v a 2 s .c o m*/ * @param module ? * @param fields ? * @param queryStr ? * @param sort * @param pageNo * @param pageSize * @return */ @SuppressWarnings("deprecation") public static PageBean<Document> search(String module, String[] fields, String[] queryStr, Sort sort, int pageNo, int pageSize) { if (ArrayUtils.isEmpty(fields) || ArrayUtils.isEmpty(queryStr)) return null; IndexReader reader = null; try { reader = DirectoryReader.open(FSDirectory.open(new File(LuceneUtil.getIndexPath(module)))); IndexSearcher searcher = new IndexSearcher(reader); BooleanClause.Occur[] flags = new BooleanClause.Occur[fields.length]; for (int i = 0; i < fields.length; i++) { flags[i] = BooleanClause.Occur.SHOULD; } Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_4_10_0); Query query = MultiFieldQueryParser.parse(Version.LUCENE_4_10_0, queryStr, fields, flags, analyzer); // return doPagingSearch(searcher, query, sort, pageNo, pageSize); } catch (Exception e) { e.printStackTrace(); } finally { try { if (reader != null) reader.close(); } catch (IOException e) { e.printStackTrace(); } } return null; }
From source file:com.dreamerpartner.codereview.lucene.SearchHelper.java
License:Apache License
/** * /*w w w .ja va 2s .c o 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)); ////from w w w . ja v a2 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.dubture.indexing.core.index.DocumentManager.java
License:Open Source License
protected void init() throws Exception { StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_29); MaxFieldLength length = new MaxFieldLength(255); index = getIndex();//from w w w . j av a2 s .c o m writer = new IndexWriter(index, analyzer, length); writer.commit(); reader = IndexReader.open(index, true); searcher = new IndexSearcher(reader); pendingReferences = new HashMap<IFile, List<ReferenceInfo>>(); }
From source file:com.dubture.indexing.core.index.DocumentManager.java
License:Open Source License
protected void updateReader() { try {/* w ww . ja va 2s. com*/ IndexReader newReader = reader.reopen(); if (newReader != reader) { reader.close(); reader = newReader; searcher = new IndexSearcher(reader); IndexingCorePlugin.debug("Updated reader"); } } catch (Exception e) { IndexingCorePlugin.logException(e); } }
From source file:com.duroty.lucene.bookmark.indexer.BookmarkIndexer.java
License:Open Source License
/** * DOCUMENT ME!//from ww w .j av a2s . c o m * * @param path DOCUMENT ME! * * @return DOCUMENT ME! * * @throws Exception DOCUMENT ME! */ public static Searcher getSearcher(String path) throws Exception { String optimizedPath = null; if (!path.endsWith(File.separator)) { optimizedPath = path + File.separator + OPTIMIZED_PATH_NAME + File.separator; } else { optimizedPath = path + OPTIMIZED_PATH_NAME + File.separator; } File file = new File(optimizedPath); if (!IndexReader.indexExists(file)) { file.mkdirs(); IndexWriter writer = new IndexWriter(file, null, true); writer.close(); } return new IndexSearcher(optimizedPath); }
From source file:com.duroty.service.analyzer.LuceneFiltersAnalysis.java
License:Open Source License
public void service(String repositoryName, String messageName, MimeMessage mime) throws Exception, Throwable, OutOfMemoryError { Session hsession = null;/*from www . j a v a 2s. c o m*/ RAMDirectory auxDir = null; try { hsession = hfactory.openSession(); auxDir = new RAMDirectory(); IndexWriter auxWriter = new IndexWriter(auxDir, analyzer, true); auxWriter.addDocument(luceneMessage.getDocPrincipal()); auxWriter.optimize(); auxWriter.close(); Vector filters = getFilters(hsession, repositoryName); boolean setbox = true; String box = message.getMesBox(); if (box.equals("SPAM")) { setbox = false; } else if (box.equals("DRAFT")) { setbox = false; } if (filters != null) { while (filters.size() > 0) { Filter filter = (Filter) filters.remove(0); IndexSearcher auxSearcher = new IndexSearcher(auxDir); org.apache.lucene.search.Query query = FilterQueryParser.parse(filter, analyzer); Hits hits = auxSearcher.search(query); if (hits.length() > 0) { //he tingut una coincidencia de filtre per tant cal dur a terme les accions assocides //al filtre if (filter.isFilArchive() && setbox) { //Marco un header per a que s'inserti a la carpeta d'archived message.setMesBox("HIDDEN"); } else if (filter.isFilTrash() && setbox) { message.setMesBox("TRASH"); } else { } if (filter.isFilImportant()) { message.setMesFlagged(new Boolean(true)); } if (filter.getLabel() != null) { LabMes labMes = new LabMes(new LabMesId(message, filter.getLabel())); message.addLabMeses(labMes); } if ((filter.getFilForwardTo() != null) && !filter.getFilForwardTo().trim().equals("")) { InternetAddress forwardTo = null; try { forwardTo = new InternetAddress(filter.getFilForwardTo()); } catch (Exception e) { forwardTo = null; } if (forwardTo != null) { try { InternetAddress recipient = (InternetAddress) mime.getFrom()[0]; forwardMailFromLabel(recipient, forwardTo, "FW: ", mime); } catch (Exception ex) { ex.printStackTrace(); } } } } } } } catch (Exception ex) { DLog.log(DLog.DEBUG, this.getClass(), ex); } finally { GeneralOperations.closeHibernateSession(hsession); if (auxDir != null) { auxDir.close(); } } }
From source file:com.duroty.service.BookmarkOptimizerThread.java
License:Open Source License
/** * DOCUMENT ME!/*from w ww .j a v a 2 s . co m*/ * * @param childs DOCUMENT ME! * * @throws Exception DOCUMENT ME! */ private void flush(File[] childs) throws Exception { if ((childs == null) || (childs.length == 0)) { return; } try { Thread.sleep(100); } catch (Exception e) { } File optimized = new File(optimizedPath); boolean create = false; IndexWriter writer = null; try { if (!IndexReader.indexExists(optimized)) { optimized.mkdirs(); create = true; } synchronized (this) { if (IndexReader.isLocked(optimizedPath)) { return; } else { Directory dir = FSDirectory.getDirectory(new File(optimizedPath), create); writer = new IndexWriter(dir, analyzer, create); } for (int i = 0; i < childs.length; i++) { boolean lock = true; File child = childs[i]; File[] faux = child.listFiles(); for (int j = 0; j < faux.length; j++) { if (faux[j].getName().equals("is.unlock")) { faux[j].delete(); lock = false; break; } } if (!lock) { Directory[] aux = new Directory[1]; aux[0] = FSDirectory.getDirectory(child, false); IndexSearcher searcher = null; try { searcher = new IndexSearcher(aux[0]); Document doc = searcher.doc(0); if (doc != null) { BookmarkIndexer.createSpell(userPath + SPELL, Field_title, doc); BookmarkIndexer.createSpell(userPath + SPELL, Field_keywords, doc); BookmarkIndexer.createSpell(userPath + SPELL, Field_contents, doc); BookmarkIndexer.createSpell(userPath + SPELL, Field_comments, doc); } } catch (Exception ex) { if ((ex != null) && !(ex instanceof NullPointerException)) { DLog.log(DLog.INFO, this.getClass(), ex); } } finally { if (searcher != null) { try { searcher.close(); } catch (Exception e) { } } } writer.addIndexes(aux); writer.optimize(); for (int j = 0; j < faux.length; j++) { faux[j].delete(); } child.delete(); } } writer.close(); writer = null; } } finally { if (writer != null) { try { writer.close(); } catch (Exception e) { } } } }
From source file:com.duroty.service.MailOptimizerThread.java
License:Open Source License
/** * DOCUMENT ME!//from w w w. j av a 2s . c o m * * @param childs DOCUMENT ME! * * @throws Exception DOCUMENT ME! */ private void flush(File[] childs) throws Exception { if ((childs == null) || (childs.length == 0)) { return; } try { Thread.sleep(500); } catch (Exception e) { } File optimized = new File(optimizedPath); boolean create = false; IndexWriter writer = null; try { if (!IndexReader.indexExists(optimized)) { optimized.mkdirs(); create = true; } synchronized (this) { if (IndexReader.isLocked(optimizedPath)) { return; } else { Directory dir = FSDirectory.getDirectory(new File(optimizedPath), create); writer = new IndexWriter(dir, analyzer, create); } for (int i = 0; i < childs.length; i++) { boolean lock = true; File child = childs[i]; File[] faux = child.listFiles(); for (int j = 0; j < faux.length; j++) { if (faux[j].getName().equals("is.unlock")) { faux[j].delete(); lock = false; break; } } if (!lock) { Directory[] aux = new Directory[1]; aux[0] = FSDirectory.getDirectory(child, false); IndexSearcher searcher = null; try { searcher = new IndexSearcher(aux[0]); Document doc = searcher.doc(0); if (doc != null) { MailIndexer.createSpell(userPath + SPELL, Field_from, doc); MailIndexer.createSpell(userPath + SPELL, Field_to, doc); MailIndexer.createSpell(userPath + SPELL, Field_subject, doc); MailIndexer.createSpell(userPath + SPELL, Field_body, doc); } } catch (Exception ex) { if ((ex != null) && !(ex instanceof NullPointerException)) { DLog.log(DLog.INFO, this.getClass(), ex); } } finally { if (searcher != null) { try { searcher.close(); } catch (Exception e) { } } } writer.addIndexes(aux); writer.optimize(); for (int j = 0; j < faux.length; j++) { faux[j].delete(); } child.delete(); } } writer.close(); writer = null; } } finally { if (writer != null) { try { writer.close(); } catch (Exception e) { } } } }
From source file:com.eclipsesource.connect.search.Searcher.java
License:Open Source License
private void performSearch(LuceneSearchResult searchResult, String query) { try (DirectoryReader reader = DirectoryReader.open(directory)) { IndexSearcher searcher = new IndexSearcher(reader); FuzzyQuery luceneQuery = new FuzzyQuery(new Term(Indexer.CONTENT_KEY, query)); TopDocs topDocs = searcher.search(luceneQuery, Integer.MAX_VALUE); populateSearchResult(searchResult, reader, topDocs); } catch (IOException shouldNotHappen) { throw new IllegalStateException(shouldNotHappen); }//from ww w. j ava 2s .c o m }