List of usage examples for org.apache.lucene.index IndexReader maxDoc
public abstract int maxDoc();
From source file:ChainedFilter.java
License:Apache License
/** * Delegates to each filter in the chain. * @param reader IndexReader/* w w w . ja v a 2 s . c o m*/ * @param logic Logical operation * @return BitSet */ private BitSet bits(IndexReader reader, int logic) throws IOException { BitSet result; int i = 0; /** * First AND operation takes place against a completely false * bitset and will always return zero results. Thanks to * Daniel Armbrust for pointing this out and suggesting workaround. */ if (logic == AND) { result = (BitSet) chain[i].bits(reader).clone(); ++i; } else { result = new BitSet(reader.maxDoc()); } for (; i < chain.length; i++) { doChain(result, reader, logic, chain[i]); } return result; }
From source file:ChainedFilter.java
License:Apache License
/** * Delegates to each filter in the chain. * @param reader IndexReader//from w w w . j a va 2s . c o m * @param logic Logical operation * @return BitSet */ private BitSet bits(IndexReader reader, int[] logic) throws IOException { if (logic.length != chain.length) throw new IllegalArgumentException("Invalid number of elements in logic array"); BitSet result; int i = 0; /** * First AND operation takes place against a completely false * bitset and will always return zero results. Thanks to * Daniel Armbrust for pointing this out and suggesting workaround. */ if (logic[0] == AND) { result = (BitSet) chain[i].bits(reader).clone(); ++i; } else { result = new BitSet(reader.maxDoc()); } for (; i < chain.length; i++) { doChain(result, reader, logic[i], chain[i]); } return result; }
From source file:RangeFilter.java
License:Apache License
/** * Returns a BitSet with true for documents which should be * permitted in search results, and false for those that should * not.// www . j ava2s . c o m */ public BitSet bits(IndexReader reader) throws IOException { BitSet bits = new BitSet(reader.maxDoc()); TermEnum enumerator = (null != lowerTerm ? reader.terms(new Term(fieldName, lowerTerm)) : reader.terms(new Term(fieldName, ""))); try { if (enumerator.term() == null) { return bits; } boolean checkLower = false; if (!includeLower) // make adjustments to set to exclusive checkLower = true; TermDocs termDocs = reader.termDocs(); try { do { Term term = enumerator.term(); if (term != null && term.field().equals(fieldName)) { if (!checkLower || null == lowerTerm || term.text().compareTo(lowerTerm) > 0) { checkLower = false; if (upperTerm != null) { int compare = upperTerm.compareTo(term.text()); /* if beyond the upper term, or is exclusive and * this is equal to the upper term, break out */ if ((compare < 0) || (!includeUpper && compare == 0)) { break; } } /* we have a good term, find the docs */ termDocs.seek(enumerator.term()); while (termDocs.next()) { bits.set(termDocs.doc()); } } } else { break; } } while (enumerator.next()); } finally { termDocs.close(); } } finally { enumerator.close(); } return bits; }
From source file:ReadFiles.java
License:Apache License
public static Result doScan(String path, DIRTYPE type, IndexReader ir) throws IOException { IndexReader reader; Result r = new Result(); long beginTs, endTs; if (ir != null) reader = ir;// w ww. j a v a2 s . co m else { beginTs = System.currentTimeMillis(); switch (type) { default: case MMAP: reader = DirectoryReader.open(MMapDirectory.open(new File(path))); break; case NIO: reader = DirectoryReader.open(NIOFSDirectory.open(new File(path))); break; case SIMPLE: reader = DirectoryReader.open(SimpleFSDirectory.open(new File(path))); break; } endTs = System.currentTimeMillis(); r.initTs += endTs - beginTs; r.initTsNr += 1; } System.out.println("-----Scan it------" + reader.maxDoc()); beginTs = System.currentTimeMillis(); for (int i = 0; i < reader.maxDoc(); i++) { Document doc = reader.document(i); doc.get("foo"); doc.get("bar"); //System.out.println("Key: " + doc.get("foo") + ", Value: " + doc.get("bar") + ", Content: " + doc.get("content")); } endTs = System.currentTimeMillis(); r.fetchTs += endTs - beginTs; r.fetchTsNr += reader.maxDoc(); if (ir == null) { beginTs = System.currentTimeMillis(); reader.close(); endTs = System.currentTimeMillis(); r.closeTs += endTs - beginTs; r.closeTsNr += 1; } return r; }
From source file:ReadFiles.java
License:Apache License
public static Result doRandFetch(String path, DIRTYPE type, IndexReader ir, int randfetchnr) throws IOException { IndexReader reader; Result r = new Result(); long beginTs, endTs; if (ir != null) reader = ir;/*from w w w . j a v a 2 s. c o m*/ else { beginTs = System.currentTimeMillis(); switch (type) { default: case MMAP: reader = DirectoryReader.open(MMapDirectory.open(new File(path))); break; case NIO: reader = DirectoryReader.open(NIOFSDirectory.open(new File(path))); break; case SIMPLE: reader = DirectoryReader.open(SimpleFSDirectory.open(new File(path))); break; } endTs = System.currentTimeMillis(); r.initTs += endTs - beginTs; r.initTsNr += 1; } System.out.println("-----RandFt it------"); try { Thread.sleep(10000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } // Randomized the fetch Random rand = new Random(); beginTs = System.currentTimeMillis(); int maxDoc = reader.maxDoc(); if (randfetchnr > 0) maxDoc = randfetchnr; for (int i = 0; i < maxDoc; i++) { Document doc = reader.document(rand.nextInt(maxDoc)); doc.get("foo"); doc.get("bar"); //System.out.println("Key: " + doc.get("foo") + ", Value: " + doc.get("bar")); } endTs = System.currentTimeMillis(); r.fetchTs += endTs - beginTs; r.fetchTsNr += maxDoc; if (ir == null) { beginTs = System.currentTimeMillis(); reader.close(); endTs = System.currentTimeMillis(); r.closeTs += endTs - beginTs; r.closeTsNr += 1; } return r; }
From source file:IndexAndSearchOpenStreetMaps1D.java
License:Apache License
private static void queryIndex() throws IOException { Directory dir = FSDirectory.open(Paths.get("/l/tmp/1dkd" + (USE_NF ? "_nf" : ""))); System.out.println("DIR: " + dir); IndexReader r = DirectoryReader.open(dir); System.out.println("maxDoc=" + r.maxDoc()); IndexSearcher s = new IndexSearcher(r); //System.out.println("reader MB heap=" + (reader.ramBytesUsed()/1024/1024.)); // London, UK: int STEPS = 5; double MIN_LAT = 51.0919106; double MAX_LAT = 51.6542719; double MIN_LON = -0.3867282; double MAX_LON = 0.8492337; byte[] scratch1 = new byte[4]; byte[] scratch2 = new byte[4]; for (int iter = 0; iter < 100; iter++) { long tStart = System.nanoTime(); long totHits = 0; int queryCount = 0; for (int latStep = 0; latStep < STEPS; latStep++) { double lat = MIN_LAT + latStep * (MAX_LAT - MIN_LAT) / STEPS; for (int lonStep = 0; lonStep < STEPS; lonStep++) { double lon = MIN_LON + lonStep * (MAX_LON - MIN_LON) / STEPS; for (int latStepEnd = latStep + 1; latStepEnd <= STEPS; latStepEnd++) { double latEnd = MIN_LAT + latStepEnd * (MAX_LAT - MIN_LAT) / STEPS; for (int lonStepEnd = lonStep + 1; lonStepEnd <= STEPS; lonStepEnd++) { double lonEnd = MIN_LON + lonStepEnd * (MAX_LON - MIN_LON) / STEPS; Query q;//from ww w . j a v a 2s . c o m if (USE_NF) { q = LegacyNumericRangeQuery.newIntRange("latnum", (int) (1000000. * lat), (int) (1000000. * latEnd), true, true); } else { q = IntPoint.newRangeQuery("lat", (int) (1000000. * lat), (int) (1000000. * latEnd)); } TotalHitCountCollector c = new TotalHitCountCollector(); //long t0 = System.nanoTime(); s.search(q, c); //System.out.println("\nITER: now query lat=" + lat + " latEnd=" + latEnd + " lon=" + lon + " lonEnd=" + lonEnd); //Bits hits = reader.intersect(lat, latEnd, lon, lonEnd); //System.out.println(" total hits: " + hitCount); //totHits += ((FixedBitSet) hits).cardinality(); //System.out.println(" add tot " + c.getTotalHits()); totHits += c.getTotalHits(); queryCount++; } } } } long tEnd = System.nanoTime(); System.out.println("ITER: " + iter + " " + ((tEnd - tStart) / 1000000000.0) + " sec; totHits=" + totHits + "; " + queryCount + " queries"); if (iter == 0) { long bytes = 0; for (LeafReaderContext ctx : r.leaves()) { CodecReader cr = (CodecReader) ctx.reader(); System.out.println(Accountables.toString(cr)); bytes += cr.ramBytesUsed(); } System.out.println("READER MB: " + (bytes / 1024. / 1024.)); System.out.println("RAM: " + Accountables.toString((Accountable) r.leaves().get(0).reader())); } } IOUtils.close(r, dir); }
From source file:TestSearch.java
public static void main(String[] args) throws Exception { String path = "E:\\jar\\product-index-mongo\\product-index-mongo\\index"; List<IndexReader> readers = new ArrayList<IndexReader>(); IndexReader reader1 = IndexReader.open(path); readers.add(reader1);/*ww w. j a va2 s . c om*/ System.out.println("DOC2 TOTAL:\t" + reader1.maxDoc()); MultiReader reader = new MultiReader(readers.toArray(new IndexReader[0])); IndexConfig config = new IndexConfig(); config.initConfig("D:\\mainonecode\\mainonesearch\\mainone-index-product\\config\\index\\index.conf"); QueryFilters.loadFilters("D:\\mainonecode\\mainonesearch\\mainone-index-product\\config\\index\\filter"); IndexSearcher searcher = new IndexSearcher(reader, false, config, null, null); // searcher.delDocByDocnum(new int[]{714575965}); // searcher.unDeleteDocByDocNum(714575965); ScorePluginLoader.load(reader); // ScoreParam scoreParam = new ScoreParam(); // scoreParam.setSearchType((byte)1); // // scoreParam.setSearchType((byte)5); // scoreParam.setSortType((byte)-1); // scoreParam.setLongitude(104.08022f); // scoreParam.setLatitude(30.635338f); // scoreParam.setLbs(3); ProductQueryOptimizer optimizer = new ProductQueryOptimizer(16, 0.05f); BufferedReader strin = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter writer = new BufferedWriter(new FileWriter("e:/result.txt")); ProductQueryParam queryParam = makeParam(); long begin = System.currentTimeMillis(); System.out.println("?\t"); ProductHits hits = (ProductHits) optimizer.optimize(searcher, queryParam); long end = System.currentTimeMillis(); System.out.println("?:\t" + (end - begin)); System.out.println("?:\t" + hits.getTotal()); System.out.println("?:\t" + hits.getGrpHits()); BooleanQuery original = QueryFilters.filter(queryParam.getQuery(), Constants.SLOP_SCORE_CVM_CLASS); ClusterBean[] trades = hits.getIndustryids(); for (int i = 0; i < trades.length; i++) { System.out.println(trades[i].getId() + "\t" + trades[i].getNum()); } Hit[] docs = hits.getHits(); for (int i = 0; i < docs.length; i++) { writer.write(docs[i].getIndexDocNo() + "\n"); Properties prop = searcher.getDetailSummary(docs[i].getIndexDocNo(), "", 1, 0); BitVector haspic = (BitVector) MemoryFieldCache.get("haspic"); // System.out.println(prop); System.out.println(docs[i].getIndexDocNo() + "\t" + docs[i].getScore() + "\t" + prop.getProperty("title") + "\t" + prop.getProperty("keyword") + "haspic:\t" + haspic.get(docs[i].getIndexDocNo()) + "\tindustryid:\t" + prop.getProperty("industryid") + "\t[" + prop.getProperty("spec") + "\t" + prop.getProperty("unit") + "\t" + prop.getProperty("price") + "\t" + prop.getProperty("mincount") + "]" ); // Explanation explain = searcher.explain(original, // docs[i].getIndexDocNo()); // System.out.println(explain.toString()); } writer.flush(); // Thread.currentThread().join(); }
From source file:action.indexing.IndexingTest.java
License:Apache License
public void testIndexReader() throws IOException { IndexReader reader = IndexReader.open(directory); assertEquals(ids.length, reader.maxDoc()); //8 assertEquals(ids.length, reader.numDocs()); //8 reader.close();//from w ww . ja v a 2s . c om }
From source file:alba.solr.core.DynamicScorer.java
License:Apache License
public DynamicScorer(IndexReader reader, CallableFunction function, Object[] functionParams, DynamicQuery query) {//from www. ja va2s .c om super(null); logger.error("created dynamic scorer!"); this.query = query; this.reader = (LeafReader) reader; this.function = function; this.maxDoc = reader.maxDoc(); //this.values = values; setCheckDeletes(true); this.liveDocs = MultiFields.getLiveDocs(reader); this.functionParams = functionParams; }
From source file:aos.lucene.search.advanced.BooksLikeThis.java
License:Apache License
public static void main(String[] args) throws IOException { Directory dir = TestUtil.getBookIndexDirectory(); IndexReader reader = DirectoryReader.open(dir); int numDocs = reader.maxDoc(); BooksLikeThis blt = new BooksLikeThis(reader); for (int i = 0; i < numDocs; i++) { // LOGGER.info();/* w w w . j ava 2 s.com*/ Document doc = reader.document(i); LOGGER.info(doc.get("title")); Document[] docs = blt.docsLike(i, 10); // if (docs.length == 0) { LOGGER.info(" None like this"); } for (Document likeThisDoc : docs) { LOGGER.info(" -> " + likeThisDoc.get("title")); } } reader.close(); dir.close(); }