List of usage examples for org.apache.lucene.store Directory close
@Override public abstract void close() throws IOException;
From source file:DVBench.java
License:Apache License
static void doBench(int bpv) throws Exception { File file = new File("/data/indices/dvbench"); file.mkdirs();/*from w w w .jav a 2 s. c om*/ Directory dir = FSDirectory.open(file); IndexWriterConfig config = new IndexWriterConfig(null); config.setOpenMode(OpenMode.CREATE); config.setMergeScheduler(new SerialMergeScheduler()); config.setMergePolicy(new LogDocMergePolicy()); config.setMaxBufferedDocs(25000); IndexWriter writer = new IndexWriter(dir, config); MyRandom r = new MyRandom(); int numdocs = 400000; Document doc = new Document(); Field dv = new NumericDocValuesField("dv", 0); Field inv = new LongField("inv", 0, Field.Store.NO); Field boxed = new BinaryDocValuesField("boxed", new BytesRef(8)); Field boxed2 = new BinaryDocValuesField("boxed2", new BytesRef(8)); doc.add(dv); doc.add(inv); doc.add(boxed); doc.add(boxed2); for (int i = 0; i < numdocs; i++) { // defeat blockpackedwriter final long value; if (i % 8192 == 0) { value = bpv == 64 ? Long.MIN_VALUE : 0; } else if (i % 8192 == 1) { value = bpv == 64 ? Long.MAX_VALUE : (1L << bpv) - 1; } else { value = r.nextLong(bpv); } dv.setLongValue(value); inv.setLongValue(value); box(value, boxed.binaryValue()); box(value, boxed2.binaryValue()); boxed2.binaryValue().length = (bpv + 7) / 8; // fixed length writer.addDocument(doc); } writer.close(); // run dv search tests String description = "dv (bpv=" + bpv + ")"; DirectoryReader reader = DirectoryReader.open(dir); IndexSearcher searcher = new IndexSearcher(reader); searcher.setQueryCache(null); // don't bench the cache int hash = 0; // warmup hash += search(description, searcher, "dv", 300, true); hash += search(description, searcher, "dv", 300, false); // Uninverting Map<String, UninvertingReader.Type> mapping = Collections.singletonMap("inv", UninvertingReader.Type.LONG); DirectoryReader uninv = UninvertingReader.wrap(reader, mapping); IndexSearcher searcher2 = new IndexSearcher(uninv); searcher2.setQueryCache(null); // don't bench the cache description = "fc (bpv=" + bpv + ")"; // warmup hash += search(description, searcher2, "inv", 300, true); hash += search(description, searcher2, "inv", 300, false); // Boxed inside binary DirectoryReader boxedReader = new BinaryAsVLongReader(reader); IndexSearcher searcher3 = new IndexSearcher(boxedReader); searcher3.setQueryCache(null); // don't bench the cache description = "boxed (bpv=" + bpv + ")"; // warmup hash += search(description, searcher3, "boxed", 300, true); hash += search(description, searcher3, "boxed", 300, false); description = "boxed fixed-length (bpv=" + bpv + ")"; // warmup hash += search(description, searcher3, "boxed2", 300, true); hash += search(description, searcher3, "boxed2", 300, false); if (hash == 3) { // wont happen System.out.println("hash=" + hash); } reader.close(); dir.close(); }
From source file:action.searching.BasicSearchingTest.java
License:Apache License
public void testTerm() throws Exception { Directory dir = FSDirectory.open(new File(System.getProperty("index.dir"))); //A IndexSearcher searcher = new IndexSearcher(dir); //B Term t = new Term("subject", "ant"); Query query = new TermQuery(t); TopDocs docs = searcher.search(query, 10); assertEquals("Ant in Action", //C 1, docs.totalHits); //C t = new Term("subject", "junit"); docs = searcher.search(new TermQuery(t), 10); assertEquals("Ant in Action, " + //D "JUnit in Action, Second Edition", //D 2, docs.totalHits); //D searcher.close();//from w w w .j a v a 2 s .c o m dir.close(); }
From source file:action.searching.BasicSearchingTest.java
License:Apache License
public void testKeyword() throws Exception { Directory dir = FSDirectory.open(new File(System.getProperty("index.dir"))); IndexSearcher searcher = new IndexSearcher(dir); Term t = new Term("isbn", "9781935182023"); Query query = new TermQuery(t); TopDocs docs = searcher.search(query, 10); assertEquals("JUnit in Action, Second Edition", 1, docs.totalHits); searcher.close();/* ww w .jav a 2s . c om*/ dir.close(); }
From source file:action.searching.BasicSearchingTest.java
License:Apache License
public void testQueryParser() throws Exception { Directory dir = FSDirectory.open(new File(System.getProperty("index.dir"))); IndexSearcher searcher = new IndexSearcher(dir); QueryParser parser = new QueryParser(Version.LUCENE_30, //A "contents", //A new SimpleAnalyzer()); //A Query query = parser.parse("+JUNIT +ANT -MOCK"); //B TopDocs docs = searcher.search(query, 10); assertEquals(1, docs.totalHits);/*from w w w .j a v a 2 s . c o m*/ Document d = searcher.doc(docs.scoreDocs[0].doc); assertEquals("Ant in Action", d.get("title")); query = parser.parse("mock OR junit"); //B docs = searcher.search(query, 10); assertEquals("Ant in Action, " + "JUnit in Action, Second Edition", 2, docs.totalHits); searcher.close(); dir.close(); }
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 a v a 2 s .c o m 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(); }
From source file:aos.lucene.search.advanced.FunctionQueryTest.java
License:Apache License
public void testRecency() throws Throwable { Directory dir = TestUtil.getBookIndexDirectory(); IndexReader r = DirectoryReader.open(dir); IndexSearcher s = new IndexSearcher(r); s.setDefaultFieldSortScoring(true, true); QueryParser parser = new QueryParser(Version.LUCENE_46, "contents", new StandardAnalyzer(Version.LUCENE_46)); Query q = parser.parse("java in action"); // #A Query q2 = new RecencyBoostingQuery(q, // #B 2.0, 2 * 365, "pubmonthAsDay"); Sort sort = new Sort(new SortField[] { SortField.FIELD_SCORE, new SortField("title2", SortField.STRING) }); TopDocs hits = s.search(q2, null, 5, sort); for (int i = 0; i < hits.scoreDocs.length; i++) { Document doc = r.document(hits.scoreDocs[i].doc); LOGGER.info((1 + i) + ": " + doc.get("title") + ": pubmonth=" + doc.get("pubmonth") + " score=" + hits.scoreDocs[i].score); }// w w w . j av a 2s . co m s.close(); r.close(); dir.close(); }
From source file:aos.lucene.search.advanced.MultiFieldQueryParserTest.java
License:Apache License
public void testDefaultOperator() throws Exception { Query query = new MultiFieldQueryParser(Version.LUCENE_46, new String[] { "title", "subject" }, new SimpleAnalyzer()).parse("development"); Directory dir = TestUtil.getBookIndexDirectory(); IndexSearcher searcher = new IndexSearcher(dir, true); TopDocs hits = searcher.search(query, 10); assertTrue(TestUtil.hitsIncludeTitle(searcher, hits, "Ant in Action")); assertTrue(TestUtil.hitsIncludeTitle( //A searcher, //A hits, //A "Extreme Programming Explained")); //A searcher.close();// w ww.ja v a 2s. c o m dir.close(); }
From source file:aos.lucene.search.advanced.MultiFieldQueryParserTest.java
License:Apache License
public void testSpecifiedOperator() throws Exception { Query query = MultiFieldQueryParser.parse(Version.LUCENE_46, "lucene", new String[] { "title", "subject" }, new BooleanClause.Occur[] { BooleanClause.Occur.MUST, BooleanClause.Occur.MUST }, new SimpleAnalyzer()); Directory dir = TestUtil.getBookIndexDirectory(); IndexSearcher searcher = new IndexSearcher(dir, true); TopDocs hits = searcher.search(query, 10); assertTrue(TestUtil.hitsIncludeTitle(searcher, hits, "Lucene in Action, Second Edition")); assertEquals("one and only one", 1, hits.scoreDocs.length); searcher.close();/*from w w w. ja v a 2s . c o m*/ dir.close(); }
From source file:aos.lucene.search.advanced.SortingExample.java
License:Apache License
public static void main(String[] args) throws Exception { Query allBooks = new MatchAllDocsQuery(); QueryParser parser = new QueryParser(Version.LUCENE_46, // "contents", // new StandardAnalyzer( // Version.LUCENE_46)); // BooleanQuery query = new BooleanQuery(); // query.add(allBooks, BooleanClause.Occur.SHOULD); // query.add(parser.parse("java OR action"), BooleanClause.Occur.SHOULD); // Directory directory = TestUtil.getBookIndexDirectory(); // SortingExample example = new SortingExample(directory); // example.displayResults(query, Sort.RELEVANCE); example.displayResults(query, Sort.INDEXORDER); example.displayResults(query, new Sort(new SortField("category", SortField.STRING))); example.displayResults(query, new Sort(new SortField("pubmonth", SortField.INT, true))); example.displayResults(query, new Sort(new SortField("category", SortField.STRING), SortField.FIELD_SCORE, new SortField("pubmonth", SortField.INT, true))); example.displayResults(query,/* w ww . ja va 2 s . c o m*/ new Sort(new SortField[] { SortField.FIELD_SCORE, new SortField("category", SortField.STRING) })); directory.close(); }
From source file:aos.lucene.search.advanced.TimeLimitingCollectorTest.java
License:Apache License
public void testTimeLimitingCollector() throws Exception { Directory dir = TestUtil.getBookIndexDirectory(); IndexSearcher searcher = new IndexSearcher(dir); Query q = new MatchAllDocsQuery(); int numAllBooks = TestUtil.hitCount(searcher, q); TopScoreDocCollector topDocs = TopScoreDocCollector.create(10, false); Collector collector = new TimeLimitingCollector(topDocs, // #A 1000); // #A try {/*from w ww. ja v a 2 s . c o m*/ searcher.search(q, collector); assertEquals(numAllBooks, topDocs.getTotalHits()); // #B } catch (TimeExceededException tee) { // #C LOGGER.info("Too much time taken."); // #C } // #C searcher.close(); dir.close(); }