List of usage examples for org.apache.lucene.index MultiReader MultiReader
public MultiReader(IndexReader[] subReaders, boolean closeSubReaders) throws IOException
Construct a MultiReader aggregating the named set of (sub)readers.
From source file:cn.hbu.cs.esearch.service.impl.EsearchSearchServiceImpl.java
License:Apache License
@Override public SearchResult search(SearchRequest sResquest) throws EsearchException { try {//ww w .ja v a2 s. c o m esearchSystem.flushEvents(2000); } catch (EsearchException e) { LOGGER.error("Esearch flush events error. \n{}", e); } String queryString = sResquest.getQuery(); String queryField = sResquest.getField(); LOGGER.info("The search request coming: queryField:{},queryString:{}", queryField, queryString); Analyzer analyzer = esearchSystem.getAnalyzer(); QueryParser queryParser = new QueryParser(Version.LUCENE_43, queryField, analyzer); SearchResult result = new SearchResult(); List<EsearchMultiReader<R>> readers = null; MultiReader multiReader = null; IndexSearcher searcher = null; try { Query query = null; if (Strings.isNullOrEmpty(queryString)) { query = new MatchAllDocsQuery(); } else { query = queryParser.parse(queryString); } readers = esearchSystem.getIndexReaders(); multiReader = new MultiReader(readers.toArray(new IndexReader[readers.size()]), false); searcher = new IndexSearcher(multiReader); long start = System.currentTimeMillis(); TopDocs docs = searcher.search(query, null, sResquest.getSize()); long end = System.currentTimeMillis(); result.setTime(end - start); result.setTotalDocs(multiReader.numDocs()); result.setTotalHits(docs.totalHits); LOGGER.info("Got {} hits. Cost:{} ms", docs.totalHits, end - start); if (sResquest.getSearchType() == SearchRequest.SearchType.COUNT) { return result; } ScoreDoc[] scoreDocs = docs.scoreDocs; ArrayList<SearchHit> hitList = new ArrayList<SearchHit>(scoreDocs.length); for (ScoreDoc scoreDoc : scoreDocs) { SearchHit hit = new SearchHit(); hit.setScore(scoreDoc.score); int docID = scoreDoc.doc; Document doc = multiReader.document(docID); String content = doc.get(queryField); Scorer qs = new QueryScorer(query); SimpleHTMLFormatter formatter = new SimpleHTMLFormatter("<span class=\"hl\">", "</span>"); Highlighter hl = new Highlighter(formatter, qs); String[] fragments = hl.getBestFragments(analyzer, queryField, content, 1); Map<String, String[]> fields = convert(doc, sResquest.getSearchType()); fields.put("fragment", fragments); hit.setFields(fields); hitList.add(hit); } result.setHits(hitList.toArray(new SearchHit[hitList.size()])); return result; } catch (Exception e) { LOGGER.error(e.getMessage(), e); throw new EsearchException(e.getMessage(), e); } finally { if (multiReader != null) { try { multiReader.close(); } catch (IOException e) { LOGGER.error(e.getMessage(), e); } } esearchSystem.returnIndexReaders(readers); } }
From source file:com.basistech.lucene.tools.LuceneQueryTool.java
License:Apache License
public static void main(String[] args) throws IOException, org.apache.lucene.queryparser.classic.ParseException { String charsetName = Charset.defaultCharset().name(); if (!"UTF-8".equals(charsetName)) { // Really only a problem on mac, where the default charset is MacRoman, // and it cannot be changed via the system Locale. System.err.println(String.format("defaultCharset is %s, but we require UTF-8.", charsetName)); System.err.println("Set -Dfile.encoding=UTF-8 on the Java command line, or"); System.err.println("set JAVA_TOOL_OPTIONS=-Dfile.encoding=UTF-8 in the environment."); System.exit(1);/* www .ja va 2 s . c om*/ } Options options = LuceneQueryTool.createOptions(); CommandLineParser parser = new GnuParser(); CommandLine cmdline = null; try { cmdline = parser.parse(options, args); validateOptions(options, args); } catch (org.apache.commons.cli.ParseException e) { System.err.println(e.getMessage()); usage(options); System.exit(1); } String[] remaining = cmdline.getArgs(); if (remaining != null && remaining.length > 0) { System.err.println("unknown extra args found: " + Lists.newArrayList(remaining)); usage(options); System.exit(1); } String[] indexPaths = cmdline.getOptionValues("index"); IndexReader[] readers = new IndexReader[indexPaths.length]; for (int i = 0; i < indexPaths.length; i++) { Path path = FileSystems.getDefault().getPath(indexPaths[i]); readers[i] = DirectoryReader.open(FSDirectory.open(path)); } IndexReader reader = new MultiReader(readers, true); LuceneQueryTool that = new LuceneQueryTool(reader); String opt; opt = cmdline.getOptionValue("query-limit"); if (opt != null) { that.setQueryLimit(Integer.parseInt(opt)); } opt = cmdline.getOptionValue("output-limit"); if (opt != null) { that.setOutputLimit(Integer.parseInt(opt)); } opt = cmdline.getOptionValue("analyzer"); if (opt != null) { that.setAnalyzer(opt); } opt = cmdline.getOptionValue("query-field"); if (opt != null) { that.setDefaultField(opt); } opt = cmdline.getOptionValue("output"); PrintStream out = null; if (opt != null) { out = new PrintStream(new FileOutputStream(new File(opt)), true); that.setOutputStream(out); } if (cmdline.hasOption("show-id")) { that.setShowId(true); } if (cmdline.hasOption("show-hits")) { that.setShowHits(true); } if (cmdline.hasOption("show-score")) { that.setShowScore(true); } if (cmdline.hasOption("sort-fields")) { that.setSortFields(true); } boolean suppressNames = cmdline.hasOption("suppress-names"); Formatter.Format format = Formatter.Format.MULTILINE; opt = cmdline.getOptionValue("format"); if (opt != null) { format = Formatter.Format.fromName(opt); } if (cmdline.hasOption("tabular")) { // compatibility option format = Formatter.Format.TABULAR; } that.setFormatter(Formatter.newInstance(format, suppressNames)); String[] opts; opts = cmdline.getOptionValues("fields"); if (opts != null) { that.setFieldNames(Lists.newArrayList(opts)); } opt = cmdline.getOptionValue("regex"); if (opt != null) { Pattern p = Pattern.compile("^(.*?):/(.*)/$"); Matcher m = p.matcher(opt); if (m.matches()) { that.setRegex(m.group(1), Pattern.compile(m.group(2))); } else { System.err.println("Invalid regex, should be field:/regex/"); usage(options); System.exit(1); } } opts = cmdline.getOptionValues("query"); that.run(opts); if (out != null) { out.close(); } reader.close(); }
From source file:com.browseengine.bobo.api.BoboIndexReader.java
License:Open Source License
protected BoboIndexReader(IndexReader reader, Collection<FacetHandler<?>> facetHandlers, WorkArea workArea, boolean useSubReaders) throws IOException { super(useSubReaders ? new MultiReader(createSubReaders(reader, workArea), false) : reader); if (useSubReaders) { BoboIndexReader[] subReaders = (BoboIndexReader[]) in.getSequentialSubReaders(); if (subReaders != null && subReaders.length > 0) { _subReaders = subReaders;// w w w . j a va 2s.c om int maxDoc = 0; _starts = new int[_subReaders.length + 1]; for (int i = 0; i < _subReaders.length; i++) { if (facetHandlers != null) _subReaders[i].setFacetHandlers(facetHandlers); _starts[i] = maxDoc; maxDoc += _subReaders[i].maxDoc(); } _starts[_subReaders.length] = maxDoc; } } _facetHandlers = facetHandlers; _workArea = workArea; }
From source file:com.codeReading.core.opengrok.SearchHelper.java
License:Open Source License
/** * Create the searcher to use wrt. to currently set parameters and the given * projects. Does not produce any {@link #redirect} link. It also does * nothing if {@link #redirect} or {@link #errorMsg} have a * none-{@code null} value. <p> Parameters which should be populated/set at * this time: <ul> <li>{@link #builder}</li> <li>{@link #dataRoot}</li> * <li>{@link #order} (falls back to relevance if unset)</li> * <li>{@link #parallel} (default: false)</li> </ul> Populates/sets: <ul> * <li>{@link #query}</li> <li>{@link #searcher}</li> <li>{@link #sort}</li> * <li>{@link #projects}</li> <li>{@link #errorMsg} if an error occurs</li> * </ul>//w w w. j av a 2 s . c om * * @param projects project to use query. If empty, a none-project opengrok * setup is assumed (i.e. DATA_ROOT/index will be used instead of possible * multiple DATA_ROOT/$project/index). * @return this instance */ public SearchHelper prepareExec(SortedSet<String> projects) { if (redirect != null || errorMsg != null) { return this; } // the Query created by the QueryBuilder try { indexDir = new File(dataRoot, "index"); query = builder.build(); if (projects == null) { errorMsg = "No project selected!"; return this; } this.projects = projects; if (projects.isEmpty()) { //no project setup FSDirectory dir = FSDirectory.open(indexDir); searcher = new IndexSearcher(DirectoryReader.open(dir)); } else if (projects.size() == 1) { // just 1 project selected FSDirectory dir = FSDirectory.open(new File(indexDir, projects.first())); searcher = new IndexSearcher(DirectoryReader.open(dir)); } else { //more projects IndexReader[] subreaders = new IndexReader[projects.size()]; int ii = 0; //TODO might need to rewrite to Project instead of // String , need changes in projects.jspf too for (String proj : projects) { FSDirectory dir = FSDirectory.open(new File(indexDir, proj)); subreaders[ii++] = DirectoryReader.open(dir); } MultiReader searchables = new MultiReader(subreaders, true); if (parallel) { int noThreads = 2 + (2 * Runtime.getRuntime().availableProcessors()); //TODO there might be a better way for counting this executor = Executors.newFixedThreadPool(noThreads); } searcher = parallel ? new IndexSearcher(searchables, executor) : new IndexSearcher(searchables); } // TODO check if below is somehow reusing sessions so we don't // requery again and again, I guess 2min timeout sessions could be // usefull, since you click on the next page within 2mins, if not, // then wait ;) switch (order) { case LASTMODIFIED: sort = new Sort(new SortField("date", SortField.Type.STRING, true)); break; case BY_PATH: sort = new Sort(new SortField("fullpath", SortField.Type.STRING)); break; default: sort = Sort.RELEVANCE; break; } checker = new DirectSpellChecker(); } catch (ParseException e) { errorMsg = PARSE_ERROR_MSG + e.getMessage(); } catch (FileNotFoundException e) { // errorMsg = "Index database(s) not found: " + e.getMessage(); errorMsg = "Index database(s) not found."; } catch (Exception e) { errorMsg = e.getMessage(); } return this; }
From source file:com.edgenius.wiki.search.lucene.ParallelSearcherFactory.java
License:Open Source License
@Override public IndexSearcher getSearcher() throws SearchException { try {//from w w w . j ava 2 s.c o m if (container.get() != null) { return container.get(); } IndexReader reader; if (directories.length > 1) { IndexReader[] readers = new IndexReader[directories.length]; for (int idx = 0; idx < directories.length; idx++) { readers[idx] = IndexReader.open(directories[idx], true); } reader = new MultiReader(readers, true); } else { reader = IndexReader.open(directories[0]); } IndexSearcher searcher = new IndexSearcher(reader, exectorService); container.set(searcher); return searcher; } catch (CorruptIndexException e) { throw new SearchException(e); } catch (IOException e) { throw new SearchException(e); } }
From source file:com.epam.catgenome.dao.index.FeatureIndexDao.java
License:Open Source License
private MultiReader openMultiReader(SimpleFSDirectory[] indexes) throws IOException { IndexReader[] readers = new IndexReader[indexes.length]; for (int i = 0; i < indexes.length; i++) { readers[i] = DirectoryReader.open(indexes[i]); }/*from w w w . ja v a2 s . c o m*/ return new MultiReader(readers, true); }
From source file:com.gentics.cr.lucene.indexaccessor.DefaultMultiIndexAccessor.java
License:Apache License
public IndexReader getReader(boolean write) throws IOException { if (write) {// ww w.j a v a 2 s .c o m throw new UnsupportedOperationException(); } IndexReader[] readers = new IndexReader[this.dirs.length]; IndexAccessorFactory factory = IndexAccessorFactory.getInstance(); int i = 0; for (Directory index : this.dirs) { IndexAccessor indexAccessor = factory.getAccessor(index); readers[i] = indexAccessor.getReader(false); multiReaderAccessors.put(readers[i], indexAccessor); i++; } MultiReader multiReader = new MultiReader(readers, true); return multiReader; }
From source file:com.gentics.cr.lucene.indexaccessor.DefaultMultiIndexAccessor.java
License:Apache License
public IndexSearcher getSearcher(final Similarity similarity, final IndexReader indexReader) throws IOException { IndexReader ir = indexReader;//www . j a va 2s .c o m if (ir == null) { IndexReader[] readers = new IndexReader[this.dirs.length]; IndexAccessorFactory factory = IndexAccessorFactory.getInstance(); int i = 0; for (Directory index : this.dirs) { IndexAccessor indexAccessor = factory.getAccessor(index); readers[i] = indexAccessor.getReader(false); multiReaderAccessors.put(readers[i], indexAccessor); i++; } ir = new MultiReader(readers, false); } IndexSearcher multiSearcher = new IndexSearcher(ir); multiSearcher.setSimilarity(similarity); return multiSearcher; }
From source file:com.mathworks.xzheng.advsearching.MultiSearcherTest.java
License:Apache License
public void testMulti() throws Exception { MultiReader multiReader = new MultiReader(searchers[0].getIndexReader(), searchers[1].getIndexReader()); IndexSearcher searcher = new IndexSearcher(multiReader); TermRangeQuery query = new TermRangeQuery("animal", // #3 new BytesRef("h"), // #3 new BytesRef("t"), // #3 true, true);// #3 TopDocs hits = searcher.search(query, 10); assertEquals("tarantula not included", 12, hits.totalHits); }
From source file:com.orientechnologies.lucene.query.OLuceneQueryContext.java
License:Apache License
public IndexSearcher getSearcher() throws IOException { return changes == null ? searcher : new IndexSearcher( new MultiReader(searcher.getIndexReader(), changes.searcher().getIndexReader())); }