List of usage examples for org.apache.lucene.store RAMDirectory RAMDirectory
public RAMDirectory()
From source file:com.zghw.lucene.demo.FormBasedXmlQueryDemo.java
License:Apache License
private void openExampleIndex() throws IOException { //Create a RAM-based index from our test data file RAMDirectory rd = new RAMDirectory(); IndexWriterConfig iwConfig = new IndexWriterConfig(Version.LUCENE_CURRENT, analyzer); IndexWriter writer = new IndexWriter(rd, iwConfig); InputStream dataIn = getServletContext().getResourceAsStream("/WEB-INF/data.tsv"); BufferedReader br = new BufferedReader(new InputStreamReader(dataIn, StandardCharsets.UTF_8)); String line = br.readLine();/*from w w w . j av a 2 s . c o m*/ final FieldType textNoNorms = new FieldType(TextField.TYPE_STORED); textNoNorms.setOmitNorms(true); while (line != null) { line = line.trim(); if (line.length() > 0) { //parse row and create a document StringTokenizer st = new StringTokenizer(line, "\t"); Document doc = new Document(); doc.add(new Field("location", st.nextToken(), textNoNorms)); doc.add(new Field("salary", st.nextToken(), textNoNorms)); doc.add(new Field("type", st.nextToken(), textNoNorms)); doc.add(new Field("description", st.nextToken(), textNoNorms)); writer.addDocument(doc); } line = br.readLine(); } writer.close(); //open searcher // this example never closes it reader! IndexReader reader = DirectoryReader.open(rd); searcher = new IndexSearcher(reader); }
From source file:ddf.catalog.pubsub.criteria.contextual.ContextualEvaluator.java
License:Open Source License
/** * Build one Lucene index for the specified XML Document that contains both case-insensitive and * case-sensitive indexed text. Use the provided XPath selectors to extract the indexable text * from the specified XML document.//from w w w . j av a 2 s .co m * * @param fullDocument * the XML document to be indexed * @param xpathSelectors * the XPath selectors to use to extract the indexable text from the XML document * * @return the Lucene index for the indexed text from the XML document * * @throws IOException */ public static Directory buildIndex(String fullDocument, String[] xpathSelectors) throws IOException { String methodName = "buildIndex"; LOGGER.entry(methodName); // LOGGER.debug( XPathHelper.xmlToString( fullDocument ) ); // 0. Specify the analyzer for tokenizing text. // The same analyzer should be used for indexing and searching ContextualAnalyzer contextualAnalyzer = new ContextualAnalyzer(Version.LUCENE_30); // 1. create the index Directory index = new RAMDirectory(); // Retrieve the text from the document that can be indexed using the specified XPath // selectors String indexableText = getIndexableText(fullDocument, xpathSelectors); // Create an IndexWriter using the case-insensitive StandardAnalyzer // NOTE: the boolean arg in the IndexWriter constructor means to create a new index, // overwriting any existing index IndexWriter indexWriter = new IndexWriter(index, contextualAnalyzer, true, IndexWriter.MaxFieldLength.UNLIMITED); logTokens(indexWriter.getAnalyzer(), FIELD_NAME, fullDocument, "ContextualAnalyzer"); // Add the indexable text to the case-insensitive index writer, assigning it the // "case-insensitive" field name addDoc(indexWriter, FIELD_NAME, indexableText); indexWriter.close(); CaseSensitiveContextualAnalyzer caseSensitiveStandardAnalyzer = new CaseSensitiveContextualAnalyzer( Version.LUCENE_30); // Create a second IndexWriter using the custom case-sensitive StandardAnalyzer // NOTE: set boolean to false to append the case-sensitive indexed text to the existing // index (populated by first IndexWriter) IndexWriter csIndexWriter = new IndexWriter(index, caseSensitiveStandardAnalyzer, false, IndexWriter.MaxFieldLength.UNLIMITED); // Add the indexable text to the case-sensitive index writer, assigning it the // "case-sensitive" field name addDoc(csIndexWriter, CASE_SENSITIVE_FIELD_NAME, indexableText); csIndexWriter.close(); LOGGER.exit(methodName); return index; }
From source file:de.blizzy.documentr.search.AllDocIdsCollectorTest.java
License:Open Source License
@Before public void setUp() throws IOException { directory = new RAMDirectory(); StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_40); IndexWriterConfig writerConfig = new IndexWriterConfig(Version.LUCENE_40, analyzer); writerConfig.setOpenMode(OpenMode.CREATE_OR_APPEND); IndexWriter writer = new IndexWriter(directory, writerConfig); writer.addDocument(createDocument()); writer.addDocument(createDocument()); writer.addDocument(createDocument()); writer.commit();//from w w w.j a va 2 s . co m writer.close(true); reader = DirectoryReader.open(directory); }
From source file:de.blizzy.documentr.search.GetSearchHitTaskTest.java
License:Open Source License
@Before public void setUp() throws IOException { directory = new RAMDirectory(); StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_40); IndexWriterConfig writerConfig = new IndexWriterConfig(Version.LUCENE_40, analyzer); writerConfig.setOpenMode(OpenMode.CREATE_OR_APPEND); IndexWriter writer = new IndexWriter(directory, writerConfig); writer.addDocument(createDocument("project", "branch", "home", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ new String[] { "tag1", "tag2" }, //$NON-NLS-1$ //$NON-NLS-2$ "title", "some text")); //$NON-NLS-1$ //$NON-NLS-2$ writer.commit();/*w ww. j a va 2s . c o m*/ writer.close(true); reader = DirectoryReader.open(directory); Query query = new TermQuery(new Term("text", "some")); //$NON-NLS-1$ //$NON-NLS-2$ task = new GetSearchHitTask(query, reader, 0, analyzer); }
From source file:de.blizzy.documentr.search.InaccessibleDocIdsCollectorTest.java
License:Open Source License
@Before public void setUp() throws IOException { directory = new RAMDirectory(); StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_40); IndexWriterConfig writerConfig = new IndexWriterConfig(Version.LUCENE_40, analyzer); writerConfig.setOpenMode(OpenMode.CREATE_OR_APPEND); IndexWriter writer = new IndexWriter(directory, writerConfig); writer.addDocument(createDocument("project", "branch1", "home")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ writer.addDocument(createDocument("project", "branch2", "home")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ writer.commit();//from w w w.j av a 2s .c o m writer.close(true); reader = DirectoryReader.open(directory); collector = new InaccessibleDocIdsCollector(Permission.VIEW, authentication, permissionEvaluator); }
From source file:de.blizzy.documentr.search.PagePermissionFilterTest.java
License:Open Source License
@Before public void setUp() throws IOException { directory = new RAMDirectory(); StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_40); IndexWriterConfig writerConfig = new IndexWriterConfig(Version.LUCENE_40, analyzer); writerConfig.setOpenMode(OpenMode.CREATE_OR_APPEND); IndexWriter writer = new IndexWriter(directory, writerConfig); writer.addDocument(createDocument("project", "branch1", "home")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ writer.addDocument(createDocument("project", "branch2", "home")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ writer.commit();/*w w w. j a v a 2s. c o m*/ writer.close(true); reader = DirectoryReader.open(directory); BitSet docs = new BitSet(); docs.set(1); Bits docIds = new DocIdBitSet(docs); filter = new PagePermissionFilter(docIds); }
From source file:de.document.jenaspring.TextSearch.java
License:Apache License
public Dataset createCode() { // Build a text dataset by code. // Here , in-memory base data and in-memeory Lucene index // Base data/*w ww . ja va 2 s.c om*/ Dataset ds1 = DatasetFactory.create(); // Define the index mapping Model model = ds1.getDefaultModel(); Property k2 = model.createProperty("http://document/KR/uebersicht/notfall"); Property k3 = model.createProperty("http://document/KR/diagnostik/text"); Property k4 = model.createProperty("http://document/KR/uebersicht/text"); Property k5 = model.createProperty("http://document/KR/therapie/text"); Property k6 = model.createProperty("http://document/KR/beratung/text"); Property k7 = model.createProperty("http://document/KR/diagnostik/notfall"); Property k8 = model.createProperty("http://document/KR/uebersicht/notfall"); Property k9 = model.createProperty("http://document/KR/therapie/notfall"); Property k10 = model.createProperty("http://document/KR/beratung/notfall"); Property k11 = model.createProperty("http://document/KR/notes"); Property p2 = model.createProperty("http://document/PR/uebersicht/notfall"); Property p3 = model.createProperty("http://document/PR/diagnostik/text"); Property p4 = model.createProperty("http://document/PR/uebersicht/text"); Property p5 = model.createProperty("http://document/PR/therapie/text"); Property p6 = model.createProperty("http://document/PR/beratung/text"); Property p7 = model.createProperty("http://document/PR/diagnostik/notfall"); Property p8 = model.createProperty("http://document/PR/uebersicht/notfall"); Property p9 = model.createProperty("http://document/PR/therapie/notfall"); Property p10 = model.createProperty("http://document/PR/beratung/notfall"); Property p11 = model.createProperty("http://document/PR/notes"); Property pr1 = model.createProperty("http://document/PR/"); Property pr2 = model.createProperty("http://document/PR/title"); Property pr3 = model.createProperty("http://document/PR/autor"); Property pr4 = model.createProperty("http://document/PR/label"); Property kr1 = model.createProperty("http://document/KR/"); Property kr2 = model.createProperty("http://document/KR/title"); Property kr3 = model.createProperty("http://document/KR/autor"); Property kr4 = model.createProperty("http://document/KR/label"); EntityDefinition entDef = new EntityDefinition("uri", "text"); entDef.setUidField("uid"); entDef.setPrimaryPredicate(pr1.asNode()); entDef.setPrimaryPredicate(pr2.asNode()); entDef.setPrimaryPredicate(pr3.asNode()); entDef.setPrimaryPredicate(pr4.asNode()); entDef.setPrimaryPredicate(kr1.asNode()); entDef.setPrimaryPredicate(kr2.asNode()); entDef.setPrimaryPredicate(kr3.asNode()); entDef.setPrimaryPredicate(kr4.asNode()); entDef.setPrimaryPredicate(p2.asNode()); entDef.setPrimaryPredicate(p3.asNode()); entDef.setPrimaryPredicate(p4.asNode()); entDef.setPrimaryPredicate(p5.asNode()); entDef.setPrimaryPredicate(p6.asNode()); entDef.setPrimaryPredicate(p7.asNode()); entDef.setPrimaryPredicate(p8.asNode()); entDef.setPrimaryPredicate(p9.asNode()); entDef.setPrimaryPredicate(p10.asNode()); entDef.setPrimaryPredicate(p11.asNode()); entDef.setPrimaryPredicate(k2.asNode()); entDef.setPrimaryPredicate(k3.asNode()); entDef.setPrimaryPredicate(k4.asNode()); entDef.setPrimaryPredicate(k5.asNode()); entDef.setPrimaryPredicate(k6.asNode()); entDef.setPrimaryPredicate(k7.asNode()); entDef.setPrimaryPredicate(k8.asNode()); entDef.setPrimaryPredicate(k9.asNode()); entDef.setPrimaryPredicate(k10.asNode()); entDef.setPrimaryPredicate(k11.asNode()); // Lucene, in memory. Directory dir = new RAMDirectory(); // Join together into a dataset Dataset ds = TextDatasetFactory.createLucene(ds1, dir, new TextIndexConfig(entDef)); return ds; }
From source file:de.hybris.platform.lucenesearch.jalo.LuceneTest.java
License:Open Source License
@Before public void setUp() throws Exception { directory = new RAMDirectory(); final IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LUCENE_40, new StandardAnalyzer(Version.LUCENE_40)).setOpenMode(OpenMode.CREATE_OR_APPEND); final IndexWriter writer = new IndexWriter(directory, indexWriterConfig); docA = new Document(); docA.add(new Field("key", "a", Field.Store.YES, Field.Index.NOT_ANALYZED)); docA.add(new Field("text", "text zum ersten document", Field.Store.YES, Field.Index.ANALYZED)); writer.addDocument(docA);/*from ww w .j a v a 2s . c om*/ docB = new Document(); docB.add(new Field("key", "b", Field.Store.YES, Field.Index.NOT_ANALYZED)); docB.add(new Field("text", "text zum zweiten document", Field.Store.YES, Field.Index.ANALYZED)); writer.addDocument(docB); docC = new Document(); docC.add(new Field("key", "c", Field.Store.YES, Field.Index.NOT_ANALYZED)); docC.add(new Field("text", "text zum dritten document", Field.Store.YES, Field.Index.ANALYZED)); writer.addDocument(docC); //writer.optimize(); writer.close(); }
From source file:de.jetsli.lumeo.util.LuceneHelperTest.java
License:Apache License
@Test public void testTermMatching() throws Exception { RAMDirectory dir = new RAMDirectory(); IndexWriter w = new IndexWriter(dir, new IndexWriterConfig(RawLucene.VERSION, new KeywordAnalyzer())); Document d = new Document(); FieldType ft = Mapping.getLongFieldType(true, true); d.add(new LongField("id", 1234, ft)); d.add(new LongField("tmp", 1111, ft)); w.addDocument(d);//ww w. ja v a2s .c o m d = new Document(); d.add(new LongField("id", 1234, ft)); d.add(new LongField("tmp", 2222, ft)); w.updateDocument(getTerm("id", 1234), d); d = new Document(); d.add(new LongField("id", 0, ft)); w.addDocument(d); w.commit(); IndexReader reader = DirectoryReader.open(w, true); IndexSearcher searcher = new IndexSearcher(reader); BytesRef bytes = new BytesRef(); NumericUtils.longToPrefixCoded(1234, 0, bytes); TopDocs td = searcher.search(new TermQuery(new Term("id", bytes)), 10); assertEquals(1, td.totalHits); assertEquals(1234L, searcher.doc(td.scoreDocs[0].doc).getField("id").numericValue()); assertEquals(2222L, searcher.doc(td.scoreDocs[0].doc).getField("tmp").numericValue()); w.close(); }
From source file:de.jetsli.lumeo.util.LuceneHelperTest.java
License:Apache License
@Test public void testTermMatchingNrt() throws Exception { RAMDirectory dir = new RAMDirectory(); IndexWriterConfig cfg = new IndexWriterConfig(Version.LUCENE_40, new KeywordAnalyzer()); IndexWriter unwrappedWriter = new IndexWriter(dir, cfg); TrackingIndexWriter w = new TrackingIndexWriter(unwrappedWriter); NRTManager nrtManager = new NRTManager(w, new SearcherFactory() { // @Override public void warm(IndexSearcher s) throws IOException { // // TODO do some warming // } });/* w ww .j a v a 2 s . c o m*/ LumeoPerFieldAnalyzer analyzer = new LumeoPerFieldAnalyzer(Mapping.KEYWORD_ANALYZER); // analyzer.putAnalyzer("id", Mapping.KEYWORD_ANALYZER); // analyzer.putAnalyzer("tmp", Mapping.KEYWORD_ANALYZER); // It is required to use a reopen thread otherwise waitForGeneration will block forever! // If there are waiting searchers how long should reopen takes? double incomingSearchesMaximumWaiting = 0.03; // If there are no waiting searchers reopen it less frequent. double ordinaryWaiting = 5.0; // NRTManagerReopenThread reopenThread = new NRTManagerReopenThread(nrtManager, ordinaryWaiting, // incomingSearchesMaximumWaiting); // reopenThread.setName("NRT Reopen Thread"); // reopenThread.setDaemon(true); // reopenThread.start(); FieldType ft = Mapping.getLongFieldType(true, true); Document d = new Document(); d.add(new LongField("id", 1234, ft)); d.add(new LongField("tmp", 1111, ft)); long latestGen = w.updateDocument(getTerm("id", 1234), d, analyzer); d = new Document(); d.add(new LongField("id", 1234, ft)); d.add(new LongField("tmp", 2222, ft)); latestGen = w.updateDocument(getTerm("id", 1234), d, analyzer); d = new Document(); d.add(new LongField("id", 0, ft)); latestGen = w.updateDocument(getTerm("id", 0), d, analyzer); w.getIndexWriter().commit(); nrtManager.maybeRefreshBlocking(); // nrtManager.waitForGeneration(latestGen, true); IndexSearcher searcher = nrtManager.acquire(); try { TopDocs td = searcher.search(new TermQuery(getTerm("id", 1234)), 10); assertEquals(1, td.totalHits); assertEquals(1, td.scoreDocs.length); assertEquals(1234L, searcher.doc(td.scoreDocs[0].doc).getField("id").numericValue()); assertEquals(2222L, searcher.doc(td.scoreDocs[0].doc).getField("tmp").numericValue()); td = searcher.search(new TermQuery(getTerm("id", 0)), 10); assertEquals(1, td.totalHits); } finally { // reopenThread.close(); nrtManager.release(searcher); w.getIndexWriter().close(); } }