List of usage examples for org.apache.lucene.store RAMDirectory RAMDirectory
public RAMDirectory()
From source file:lucene.security.index.SecureAtomicReaderTestBase.java
License:Apache License
private AtomicReader createReader() throws IOException { IndexWriterConfig conf = new IndexWriterConfig(Version.LUCENE_43, new KeywordAnalyzer()); Directory dir = new RAMDirectory(); IndexWriter writer = new IndexWriter(dir, conf); AccessControlWriter accessControlWriter = getAccessControlFactory().getWriter(); writer.addDocument(accessControlWriter.addDiscoverVisiblity("d1", accessControlWriter.addReadVisiblity("r1", getDoc(0)))); writer.addDocument(accessControlWriter.addDiscoverVisiblity("d1", accessControlWriter.addReadVisiblity("r2", getDoc(1)))); writer.addDocument(accessControlWriter.addDiscoverVisiblity("d2", accessControlWriter.addReadVisiblity("r1", getDoc(2)))); writer.addDocument(accessControlWriter.addDiscoverVisiblity("d2", accessControlWriter.addReadVisiblity("r2", getDoc(3)))); writer.close();/*from w ww . j a va 2s.c o m*/ DirectoryReader reader = DirectoryReader.open(dir); List<AtomicReaderContext> leaves = reader.leaves(); return leaves.get(0).reader(); }
From source file:lucene.security.IndexSearcherTest.java
License:Apache License
private void runTest(int expected, Collection<String> readAuthorizations, Collection<String> discoverAuthorizations, Collection<String> discoverableFields) throws IOException, ParseException { IndexWriterConfig conf = new IndexWriterConfig(Version.LUCENE_43, new StandardAnalyzer(Version.LUCENE_43)); Directory dir = new RAMDirectory(); IndexWriter writer = new IndexWriter(dir, conf); writer.addDocument(getEmpty());/* w w w . ja va 2s.c o m*/ writer.commit(); writer.addDocument(getDoc("(a&b)|d", null, "f1", "f2")); writer.addDocument(getDoc("a&b&c", null, "f1", "f2")); writer.addDocument(getDoc("a&b&c&e", "a&b&c", "f1", "f2")); writer.addDocument(getDoc(null, null, "f1", "f2"));// can't find writer.close(); DirectoryReader reader = DirectoryReader.open(dir); List<AtomicReaderContext> leaves = reader.leaves(); assertEquals(2, leaves.size()); SecureIndexSearcher searcher = new SecureIndexSearcher(reader, getAccessControlFactory(), readAuthorizations, discoverAuthorizations, toSet(discoverableFields)); String queryStr = "text"; Query query = new QueryParser(Version.LUCENE_43, "text", new StandardAnalyzer(Version.LUCENE_43)) .parse(queryStr); TopDocs topDocs = searcher.search(query, 10); assertEquals(expected, topDocs.totalHits); DocumentAuthorizations readDocumentAuthorizations = new DocumentAuthorizations(readAuthorizations); DocumentAuthorizations discoverDocumentAuthorizations = new DocumentAuthorizations(discoverAuthorizations); DocumentVisibilityEvaluator readVisibilityEvaluator = new DocumentVisibilityEvaluator( readDocumentAuthorizations); DocumentVisibilityEvaluator discoverVisibilityEvaluator = new DocumentVisibilityEvaluator( discoverDocumentAuthorizations); for (int i = 0; i < topDocs.totalHits & i < topDocs.scoreDocs.length; i++) { Document doc = searcher.doc(topDocs.scoreDocs[i].doc); String read = doc.get("_read_"); String discover = doc.get("_discover_"); if (read != null && discover != null) { DocumentVisibility readVisibility = new DocumentVisibility(read); DocumentVisibility discoverVisibility = new DocumentVisibility(discover); assertTrue(readVisibilityEvaluator.evaluate(readVisibility) || discoverVisibilityEvaluator.evaluate(discoverVisibility)); } else if (read != null) { DocumentVisibility readVisibility = new DocumentVisibility(read); assertTrue(readVisibilityEvaluator.evaluate(readVisibility)); } else if (discover != null) { DocumentVisibility discoverVisibility = new DocumentVisibility(discover); assertTrue(discoverVisibilityEvaluator.evaluate(discoverVisibility)); // Since this document is only discoverable validate fields that are // being returned. validateDiscoverFields(doc, discoverableFields); } else { fail("Should not fetch empty document."); } } searcher.search(query, new Collector() { @Override public void setScorer(Scorer scorer) throws IOException { } @Override public void setNextReader(AtomicReaderContext context) throws IOException { assertTrue(context.reader() instanceof SecureAtomicReader); } @Override public void collect(int doc) throws IOException { } @Override public boolean acceptsDocsOutOfOrder() { return false; } }); }
From source file:luceneexamples.AddDocument.java
License:Apache License
@Test public void index() throws Exception { RAMDirectory directory = new RAMDirectory(); Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_31); IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_31, analyzer); IndexWriter writer = new IndexWriter(directory, iwc); Document doc = new Document(); doc.add(new Field("str_field", "quick brown fox jumped over the lazy dog.", Field.Store.YES, Field.Index.ANALYZED)); writer.addDocument(doc);//from w w w. j a v a 2 s. co m writer.commit(); IndexReader reader = IndexReader.open(writer, true); IndexSearcher searcher = new IndexSearcher(reader); QueryParser parser = new QueryParser(Version.LUCENE_31, "str_field", analyzer); TopDocs td = searcher.search(parser.parse("fox"), 1000); assertThat(td.totalHits, is(1)); Document doc2 = new Document(); doc2.add(new Field("str_field", "quick brown dog jumped over the lazy fox.", Field.Store.YES, Field.Index.ANALYZED)); writer.addDocument(doc2); writer.commit(); td = searcher.search(parser.parse("fox"), 1000); assertThat(td.totalHits, is(1)); searcher.close(); reader = reader.reopen(); searcher = new IndexSearcher(reader); td = searcher.search(parser.parse("fox"), 1000); assertThat(td.totalHits, is(2)); writer.close(); searcher.close(); directory.close(); }
From source file:luceneexamples.GoSenSearch.java
License:Apache License
@Test public void index() throws Exception { System.setProperty("org.apache.lucene.ja.config.file", "japanese-gosen-analyzer.xml"); System.setProperty("sen.home", "dictionary"); Directory directory = new RAMDirectory(); // Directory directory = FSDirectory.open(new File("gosenindex")); Analyzer analyzer = new GoSenAnalyzer(); IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_31, analyzer); IndexWriter writer = new IndexWriter(directory, iwc); Document doc = new Document(); doc.add(new Field("str_field", "quick brown fox jumped over the lazy dog.", Field.Store.YES, Field.Index.ANALYZED)); writer.addDocument(doc);/*w w w . jav a 2s. c o m*/ Document doc2 = new Document(); doc2.add(new Field("str_field", "?????", Field.Store.YES, Field.Index.ANALYZED)); writer.addDocument(doc2); writer.close(); IndexSearcher searcher = new IndexSearcher(directory, true); QueryParser parser = new QueryParser(Version.LUCENE_31, "str_field", analyzer); TopDocs td = searcher.search(parser.parse(""), 1000); assertThat(td.totalHits, is(1)); searcher.close(); directory.close(); }
From source file:luceneexamples.IndexAndSearch.java
License:Apache License
@Test public void index() throws Exception { RAMDirectory directory = new RAMDirectory(); Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_31); IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_31, analyzer); IndexWriter writer = new IndexWriter(directory, iwc); Document doc = new Document(); doc.add(new Field("str_field", "quick brown fox jumped over the lazy dog.", Field.Store.YES, Field.Index.ANALYZED)); writer.addDocument(doc);/* ww w . ja va2 s .com*/ Document doc2 = new Document(); doc2.add(new Field("str_field", "?????", Field.Store.YES, Field.Index.ANALYZED)); writer.addDocument(doc2); writer.close(); IndexSearcher searcher = new IndexSearcher(directory, true); QueryParser parser = new QueryParser(Version.LUCENE_31, "str_field", analyzer); TopDocs td = searcher.search(parser.parse("fox"), 1000); assertThat(td.totalHits, is(1)); Document doc3 = searcher.doc(td.scoreDocs[0].doc); assertEquals("quick brown fox jumped over the lazy dog.", doc3.get("str_field")); searcher.close(); directory.close(); }
From source file:luceneexamples.JapaneseSearch.java
License:Apache License
@Test public void index() throws Exception { Directory directory = new RAMDirectory(); // Directory directory = FSDirectory.open(new File("cjkindex")); Analyzer analyzer = new CJKAnalyzer(Version.LUCENE_31); IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_31, analyzer); IndexWriter writer = new IndexWriter(directory, iwc); Document doc = new Document(); doc.add(new Field("str_field", "quick brown fox jumped over the lazy dog.", Field.Store.YES, Field.Index.ANALYZED)); writer.addDocument(doc);/*w ww . j ava 2s .co m*/ Document doc2 = new Document(); doc2.add(new Field("str_field", "?????", Field.Store.YES, Field.Index.ANALYZED)); writer.addDocument(doc2); writer.close(); IndexSearcher searcher = new IndexSearcher(directory, true); QueryParser parser = new QueryParser(Version.LUCENE_31, "str_field", analyzer); TopDocs td = searcher.search(parser.parse(""), 1000); assertThat(td.totalHits, is(1)); searcher.close(); directory.close(); }
From source file:luceneexamples.NumericFieldDocument.java
License:Apache License
@Test public void index() throws Exception { RAMDirectory directory = new RAMDirectory(); Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_31); IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_31, analyzer); IndexWriter writer = new IndexWriter(directory, iwc); for (int i = 8; i < 12; i++) { Document doc = new Document(); doc.add(new NumericField("int_field", Field.Store.YES, true).setIntValue(i)); System.out.println(doc);/*from w ww .ja v a2 s . c om*/ writer.addDocument(doc); } writer.commit(); IndexReader reader = IndexReader.open(writer, true); IndexSearcher searcher = new IndexSearcher(reader); TopDocs td = searcher.search(new MatchAllDocsQuery(), 1000, new Sort(new SortField("int_field", SortField.INT))); assertThat(td.totalHits, is(4)); assertThat(searcher.doc(td.scoreDocs[0].doc).get("int_field"), equalTo("8")); assertThat(searcher.doc(td.scoreDocs[1].doc).get("int_field"), equalTo("9")); assertThat(searcher.doc(td.scoreDocs[2].doc).get("int_field"), equalTo("10")); assertThat(searcher.doc(td.scoreDocs[3].doc).get("int_field"), equalTo("11")); reader.close(); writer.close(); searcher.close(); directory.close(); }
From source file:luceneexamples.SortDocuments.java
License:Apache License
@Test public void index() throws Exception { RAMDirectory directory = new RAMDirectory(); Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_31); IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_31, analyzer); IndexWriter writer = new IndexWriter(directory, iwc); Document doc = new Document(); doc.add(new Field("str_field", "abc", Field.Store.YES, Field.Index.ANALYZED)); writer.addDocument(doc);// w w w . ja v a 2s.c o m Document doc2 = new Document(); doc2.add(new Field("str_field", "def", Field.Store.YES, Field.Index.ANALYZED)); writer.addDocument(doc2); Document doc3 = new Document(); doc3.add(new Field("str_field", "hij", Field.Store.YES, Field.Index.ANALYZED)); writer.addDocument(doc3); writer.commit(); IndexReader reader = IndexReader.open(writer, true); IndexSearcher searcher = new IndexSearcher(reader); TopDocs td = searcher.search(new MatchAllDocsQuery(), 1000, new Sort(new SortField("str_field", SortField.STRING))); assertThat(td.totalHits, is(3)); assertThat(searcher.doc(td.scoreDocs[0].doc).get("str_field"), equalTo("abc")); assertThat(searcher.doc(td.scoreDocs[1].doc).get("str_field"), equalTo("def")); assertThat(searcher.doc(td.scoreDocs[2].doc).get("str_field"), equalTo("hij")); td = searcher.search(new MatchAllDocsQuery(), 1000, new Sort(new SortField("str_field", SortField.STRING, true))); assertThat(td.totalHits, is(3)); assertThat(searcher.doc(td.scoreDocs[0].doc).get("str_field"), equalTo("hij")); assertThat(searcher.doc(td.scoreDocs[1].doc).get("str_field"), equalTo("def")); assertThat(searcher.doc(td.scoreDocs[2].doc).get("str_field"), equalTo("abc")); reader.close(); writer.close(); searcher.close(); directory.close(); }
From source file:luceneexamples.UpdateDocument.java
License:Apache License
@Test public void index() throws Exception { RAMDirectory directory = new RAMDirectory(); Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_31); IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_31, analyzer); IndexWriter writer = new IndexWriter(directory, iwc); Document doc = new Document(); doc.add(new Field("id", "001", Field.Store.YES, Field.Index.NOT_ANALYZED)); doc.add(new Field("str_field", "quick brown fox jumped over the lazy dog.", Field.Store.YES, Field.Index.ANALYZED)); writer.addDocument(doc);/* ww w . j ava2 s . c o m*/ writer.commit(); IndexReader reader = IndexReader.open(writer, true); IndexSearcher searcher = new IndexSearcher(reader); QueryParser parser = new QueryParser(Version.LUCENE_31, "str_field", analyzer); TopDocs td = searcher.search(parser.parse("fox"), 1000); assertThat(td.totalHits, is(1)); Document doc2 = new Document(); doc.add(new Field("id", "001", Field.Store.YES, Field.Index.NOT_ANALYZED)); doc2.add(new Field("str_field", "quick brown fox jumped over the lazy whale.", Field.Store.YES, Field.Index.ANALYZED)); writer.updateDocument(new Term("id", "001"), doc2); writer.commit(); searcher.close(); reader = reader.reopen(); searcher = new IndexSearcher(reader); td = searcher.search(parser.parse("dog"), 1000); assertThat(td.totalHits, is(0)); td = searcher.search(parser.parse("whale"), 1000); assertThat(td.totalHits, is(1)); writer.close(); searcher.close(); directory.close(); }
From source file:lux.BaseSearchTest.java
License:Mozilla Public License
public static void setup(String... xmlfile) throws Exception { XmlIndexer indexer = new XmlIndexer( INDEX_QNAMES | INDEX_PATHS | STORE_DOCUMENT | INDEX_FULLTEXT | STORE_TINY_BINARY); IndexConfiguration config = indexer.getConfiguration(); config.addField(new XPathField("doctype", "name(/*)", null, Store.YES, Type.STRING)); config.addField(new XPathField("actnum", "/*/@act", null, Store.YES, Type.INT)); config.addField(new XPathField("scnlong", "/*/@scene", null, Store.YES, Type.LONG)); config.addField(new XPathField("actstr", "/*/@act", null, Store.YES, Type.STRING)); config.setElementVisibility("hidden", ElementVisibility.HIDDEN); config.setElementVisibility("name", ElementVisibility.TRANSPARENT); config.setElementVisibility("LINE", ElementVisibility.TRANSPARENT); config.setElementVisibility("SCENE", ElementVisibility.CONTAINER); index = new IndexTestSupport(xmlfile, indexer, new RAMDirectory()); totalDocs = index.totalDocs;/*from w w w . j a va 2 s .co m*/ }