Example usage for org.apache.lucene.store RAMDirectory RAMDirectory

List of usage examples for org.apache.lucene.store RAMDirectory RAMDirectory

Introduction

In this page you can find the example usage for org.apache.lucene.store RAMDirectory RAMDirectory.

Prototype

public RAMDirectory() 

Source Link

Document

Constructs an empty Directory .

Usage

From source file:com.epimorphics.server.indexers.LuceneIndex.java

License:Apache License

@Override
public void init(Map<String, String> config, ServletContext context) {
    super.init(config, context);
    try {/*from   w ww. ja va 2s  .c o m*/
        String indexLocation = getFileParam(LOCATION_PARAM);
        if (indexLocation == null) {
            // Typically used for testing only
            log.warn("No index location, creating RAM directory");
            indexDir = new RAMDirectory();
            getIndexWriter().commit();
        } else {
            File indexF = new File(indexLocation);
            indexDir = FSDirectory.open(indexF);
            if (!indexF.exists() || (indexF.isDirectory() && indexF.list().length == 0)) {
                log.warn("No existing index files, initializing directory " + indexLocation);
                FileUtil.ensureDir(indexLocation);
                getIndexWriter().commit();
            }
        }

        String commit = config.get(COMMIT_PARAM);
        if (commit != null) {
            try {
                commitWindow = Integer.parseInt(commit) * 1000;
            } catch (NumberFormatException e) {
                log.error("Bad format for commit window config parameter: " + commit + ", using default");
            }
        }

        IndexWriter writer = getIndexWriter();
        searchManager = new SearcherManager(writer, true, null);

        String configLocation = getRequiredFileParam(CONFIG_PARAM);
        Model configModel = FileManager.get().loadModel(configLocation);
        analyseConfigModel(configModel);
    } catch (Exception e) {
        throw new EpiException(e);
    }
}

From source file:com.epimorphics.server.stores.StoreBase.java

License:Apache License

/**
 * Install a jena-text dataset wrapper round this store.
 * This is an alternative to the indexer system.
 *//*from w  w w  .j a  v a2  s . c  o m*/
protected void installJenaText() {
    if (config.containsKey(JENA_TEXT_PARAM)) {
        String dirname = getRequiredFileParam(JENA_TEXT_PARAM);
        Directory dir = null;
        if (dirname.equals("mem")) {
            dir = new RAMDirectory();
        } else {
            try {
                File dirf = new File(dirname);
                dir = FSDirectory.open(dirf);
            } catch (IOException e) {
                throw new EpiException("Failed to create jena-text lucence index area", e);
            }
        }
        EntityDefinition entDef = new EntityDefinition("uri", "text", RDFS.label.asNode());
        dataset = TextDatasetFactory.createLucene(dataset, dir, entDef);
    }
}

From source file:com.example.analyzer.server.database.DbFullTextIndex.java

License:Open Source License

public DbFullTextIndex(DbTable dbTable, int columnOffset) {
    try {//from w  w  w.  java 2 s .  co  m
        long beginTime = System.currentTimeMillis();
        ramDirectory = new RAMDirectory();
        IndexWriter writer = new IndexWriter(ramDirectory, new StandardAnalyzer(Version.LUCENE_30),
                new MaxFieldLength(50));
        int rowCount = dbTable.getRowCount();
        for (int rowOffset = 0; rowOffset < rowCount; rowOffset++) {
            String value = dbTable.coalesce(rowOffset, columnOffset, "").toString();
            byte[] idArray = getBytes(rowOffset);
            Document document = new Document();
            document.add(new Field(ID, idArray, Field.Store.YES));
            document.add(new Field(VALUE, value, Store.YES, Index.ANALYZED)); // TODO: Determine whether we need to store value
            writer.addDocument(document);
        }
        writer.optimize();
        writer.close();
        long endTime = System.currentTimeMillis();
        long elapsedTime = endTime - beginTime;
        System.out.println("created index in " + elapsedTime + " ms");
    } catch (CorruptIndexException e) {
        throw new RuntimeException(e);
    } catch (LockObtainFailedException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.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_40, analyzer);
    IndexWriter writer = new IndexWriter(rd, iwConfig);
    InputStream dataIn = getServletContext().getResourceAsStream("/WEB-INF/data.tsv");
    BufferedReader br = new BufferedReader(new InputStreamReader(dataIn, IOUtils.CHARSET_UTF_8));
    String line = br.readLine();/*w w  w.j  a v 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:com.fuerve.villageelder.actions.results.SearchResultItemTest.java

License:Apache License

/**
 * Test method for {@link com.fuerve.villageelder.actions.results.SearchResultItem#SearchResultItem(org.apache.lucene.search.TopDocs, java.util.List)}.
 *//*from   w w  w  .j a v  a  2 s  . co  m*/
@Test
public final void testSearchResultItem() throws Exception {
    Directory indexDirectoryExpected = new RAMDirectory();
    Directory taxonomyDirectoryExpected = new RAMDirectory();

    buildDummyIndex(indexDirectoryExpected, taxonomyDirectoryExpected);

    IndexReader reader = DirectoryReader.open(indexDirectoryExpected);
    IndexSearcher searcher = new IndexSearcher(reader);
    TaxonomyReader taxo = new DirectoryTaxonomyReader(taxonomyDirectoryExpected);

    QueryParser parser = new SearchQueryParser(Lucene.LUCENE_VERSION, Lucene.DEFAULT_QUERY_FIELD,
            Lucene.getPerFieldAnalyzer());

    TopFieldCollector indexCollector = getDummyCollector();
    FacetsCollector facetsCollector = getDummyFacetsCollector((DirectoryReader) reader, taxo);
    Collector collector = MultiCollector.wrap(indexCollector, facetsCollector);
    searcher.search(parser.parse("Revision:5*"), collector);
    facetsCollector.getFacetResults();
    SearchResultItem target = new SearchResultItem(indexCollector.topDocs(), facetsCollector.getFacetResults());

    assertEquals(2, target.getTopDocs().totalHits);
    assertEquals(1, target.getFacetResults().size());
}

From source file:com.fuerve.villageelder.actions.results.SearchResultTest.java

License:Apache License

/**
 * Test method for {@link com.fuerve.villageelder.actions.results.SearchResult#aggregate(com.fuerve.villageelder.actions.results.SearchResultItem)}.
 *//*from  www.j ava 2 s  .  co m*/
@Test
public final void testAggregateSearchResultItem() throws Exception {
    Directory indexDirectoryExpected = new RAMDirectory();
    Directory taxonomyDirectoryExpected = new RAMDirectory();

    buildDummyIndex(indexDirectoryExpected, taxonomyDirectoryExpected);

    IndexReader reader = DirectoryReader.open(indexDirectoryExpected);
    IndexSearcher searcher = new IndexSearcher(reader);
    TaxonomyReader taxo = new DirectoryTaxonomyReader(taxonomyDirectoryExpected);

    QueryParser parser = new SearchQueryParser(Lucene.LUCENE_VERSION, Lucene.DEFAULT_QUERY_FIELD,
            Lucene.getPerFieldAnalyzer());

    TopFieldCollector indexCollector = getDummyCollector();
    FacetsCollector facetsCollector = getDummyFacetsCollector((DirectoryReader) reader, taxo);
    Collector collector = MultiCollector.wrap(indexCollector, facetsCollector);
    searcher.search(parser.parse("Revision:5*"), collector);
    facetsCollector.getFacetResults();

    SearchResult target = new SearchResult();
    target.aggregate(new SearchResultItem(indexCollector.topDocs(), facetsCollector.getFacetResults()));

    assertEquals(2, target.getTopDocs().totalHits);
    assertEquals(1, target.getFacetResults().size());
}

From source file:com.fuerve.villageelder.indexing.IndexerTest.java

License:Apache License

/**
 * Test method for {@link com.fuerve.villageelder.indexing.Indexer#Indexer(org.apache.lucene.store.Directory, org.apache.lucene.store.Directory)}.
 *//*from  w  w  w. j  a  v a  2 s .c o m*/
@Test
public final void testIndexerDirectoryDirectory() throws Exception {
    RAMDirectory indexDirectory = new RAMDirectory();
    RAMDirectory taxonomyDirectory = new RAMDirectory();

    Field idField = IndexManager.class.getDeclaredField("indexDirectory");
    Field tdField = IndexManager.class.getDeclaredField("taxonomyDirectory");
    Field iwField = IndexManager.class.getDeclaredField("indexWriter");
    Field twField = IndexManager.class.getDeclaredField("taxonomyWriter");
    Field stField = IndexManager.class.getDeclaredField("stringDirectories");
    Field initField = IndexManager.class.getDeclaredField("initialized");
    Field imField = Indexer.class.getDeclaredField("indexManager");

    idField.setAccessible(true);
    tdField.setAccessible(true);
    iwField.setAccessible(true);
    twField.setAccessible(true);
    stField.setAccessible(true);
    initField.setAccessible(true);
    imField.setAccessible(true);

    Indexer target = new Indexer(indexDirectory, taxonomyDirectory);

    IndexManager testManager = (IndexManager) imField.get(target);

    // TEST 1: A newly constructed Indexer believes itself
    // to be uninitialized, as indicated by the 'initialized'
    // field.
    boolean initActual = initField.getBoolean(testManager);
    assertFalse(initActual);

    target.initializeIndex();

    Directory idActual = (Directory) idField.get(testManager);
    Directory tdActual = (Directory) tdField.get(testManager);
    IndexWriter iwActual = (IndexWriter) iwField.get(testManager);
    TaxonomyWriter twActual = (TaxonomyWriter) twField.get(testManager);
    boolean stActual = (Boolean) stField.get(testManager);
    initActual = initField.getBoolean(testManager);

    // TEST 2: The Indexer's index directory is what was passed in.
    assertEquals(indexDirectory, idActual);
    // TEST 3: The Indexer's taxonomy directory is what was passed in.
    assertEquals(taxonomyDirectory, tdActual);
    // TEST 4: The IndexWriter's directory is what was passed in.
    assertEquals(indexDirectory, iwActual.getDirectory());
    // TEST 5: The taxonomy index is initialized afresh with no categories
    // in it.
    assertEquals(1, twActual.getSize());
    // TEST 6: An Indexer constructed with Directories does not
    // believe that it needs to construct new Directories from string
    // pathnames.
    assertEquals(false, stActual);
    // TEST 7: The Indexer's initialized field is true after it
    // has been initialized.
    assertEquals(true, initActual);

    target.dispose();

    // TEST 8: The Indexer's index writer is null after it has
    // been disposed.
    iwActual = (IndexWriter) iwField.get(testManager);
    assertEquals(null, iwActual);

    // TEST 9: The Indexer's taxonomy writer is null after it
    // has been disposed.
    twActual = (TaxonomyWriter) twField.get(testManager);
    assertEquals(null, twActual);

    // TEST 10: The Indexer's initialized flag is false after
    // it has been disposed.
    initActual = initField.getBoolean(testManager);
    assertEquals(false, initActual);
}

From source file:com.fuerve.villageelder.indexing.IndexerTest.java

License:Apache License

/**
 * Test method for {@link com.fuerve.villageelder.indexing.Indexer#Indexer(org.apache.lucene.store.Directory, org.apache.lucene.store.Directory, org.apache.lucene.index.IndexWriterConfig.OpenMode)}.
 *//*from w  ww . java 2s  .com*/
@Test
public final void testIndexerDirectoryDirectoryOpenMode() throws Exception {
    RAMDirectory indexDirectory = new RAMDirectory();
    RAMDirectory taxonomyDirectory = new RAMDirectory();

    Field idField = IndexManager.class.getDeclaredField("indexDirectory");
    Field tdField = IndexManager.class.getDeclaredField("taxonomyDirectory");
    Field iwField = IndexManager.class.getDeclaredField("indexWriter");
    Field twField = IndexManager.class.getDeclaredField("taxonomyWriter");
    Field stField = IndexManager.class.getDeclaredField("stringDirectories");
    Field initField = IndexManager.class.getDeclaredField("initialized");
    Field imField = Indexer.class.getDeclaredField("indexManager");

    idField.setAccessible(true);
    tdField.setAccessible(true);
    iwField.setAccessible(true);
    twField.setAccessible(true);
    stField.setAccessible(true);
    initField.setAccessible(true);
    imField.setAccessible(true);

    Indexer target = new Indexer(indexDirectory, taxonomyDirectory, OpenMode.CREATE);
    target.initializeIndex();
    IndexManager testManager = (IndexManager) imField.get(target);

    TaxonomyWriter tw = (TaxonomyWriter) twField.get(testManager);
    IndexWriter iw = (IndexWriter) iwField.get(testManager);
    tw.addCategory(new CategoryPath("test/stuff", '/'));
    Document doc = new Document();
    doc.add(new LongField("testfield", 1000L, Store.YES));
    iw.addDocument(doc);
    target.dispose();

    // TEST: Initializing an index, disposing it and initializing another
    // index instance on the same Directories results in loading the same
    // index.
    Indexer target2 = new Indexer(indexDirectory, taxonomyDirectory, OpenMode.APPEND);
    target2.initializeIndex();
    testManager = (IndexManager) imField.get(target2);
    iw = (IndexWriter) iwField.get(testManager);
    tw = (TaxonomyWriter) twField.get(testManager);
    assertEquals(1, iw.numDocs());
    assertEquals(3, tw.getSize());
    target2.dispose();
}

From source file:com.fuerve.villageelder.indexing.IndexerTest.java

License:Apache License

/**
 * Test method for {@link com.fuerve.villageelder.indexing.Indexer#indexRevision(com.fuerve.villageelder.sourcecontrol.RevisionInfo)}.
 * @throws Exception /*from w w w  .j av a 2 s  .  com*/
 */
@Test
public final void testIndexRevision() throws Exception {
    RAMDirectory indexDirectory = new RAMDirectory();
    RAMDirectory taxonomyDirectory = new RAMDirectory();

    Field iwField = IndexManager.class.getDeclaredField("indexWriter");
    Field twField = IndexManager.class.getDeclaredField("taxonomyWriter");
    Field imField = Indexer.class.getDeclaredField("indexManager");

    iwField.setAccessible(true);
    twField.setAccessible(true);
    imField.setAccessible(true);

    Indexer target = new Indexer(indexDirectory, taxonomyDirectory, OpenMode.CREATE);
    target.initializeIndex();
    IndexManager testManager = (IndexManager) imField.get(target);

    target.indexRevision(buildDumbRevisionInfo());

    TaxonomyWriter tw = (TaxonomyWriter) twField.get(testManager);
    IndexWriter iw = (IndexWriter) iwField.get(testManager);

    assertEquals(1, iw.numDocs());
    assertEquals(8, tw.getSize());

    target.dispose();
}

From source file:com.fuerve.villageelder.indexing.IndexerTest.java

License:Apache License

/**
 * Test method for {@link com.fuerve.villageelder.indexing.Indexer#indexRevisions(java.lang.Iterable)}.
 *//*from w  w w  .  j av a  2  s.c  o m*/
@Test
public final void testIndexRevisions() throws Exception {
    RAMDirectory indexDirectory = new RAMDirectory();
    RAMDirectory taxonomyDirectory = new RAMDirectory();

    Field iwField = IndexManager.class.getDeclaredField("indexWriter");
    Field twField = IndexManager.class.getDeclaredField("taxonomyWriter");
    Field imField = Indexer.class.getDeclaredField("indexManager");

    iwField.setAccessible(true);
    twField.setAccessible(true);
    imField.setAccessible(true);

    Indexer target = new Indexer(indexDirectory, taxonomyDirectory, OpenMode.CREATE);
    target.initializeIndex();
    IndexManager testManager = (IndexManager) imField.get(target);

    List<RevisionInfo> revisions = new ArrayList<RevisionInfo>();
    revisions.add(buildDumbRevisionInfo());
    revisions.add(buildDumbRevisionInfo());
    target.indexRevisions(revisions);

    TaxonomyWriter tw = (TaxonomyWriter) twField.get(testManager);
    IndexWriter iw = (IndexWriter) iwField.get(testManager);

    assertEquals(2, iw.numDocs());
    assertEquals(8, tw.getSize());

    target.dispose();
}