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(FSDirectory dir, IOContext context) throws IOException 

Source Link

Document

Creates a new RAMDirectory instance from a different Directory implementation.

Usage

From source file:net.semanticmetadata.lire.searchers.TestSearching.java

License:Open Source License

public void testSearch() throws IOException, IllegalAccessException, InstantiationException {
    Cluster[] cvsurf512 = Cluster.readClusters(codebookPath + "CvSURF512");
    Cluster[] simpleceddcvsurf512 = Cluster.readClusters(codebookPath + "SIMPLEdetCVSURFCEDD512");

    ParallelIndexer parallelIndexer = new ParallelIndexer(DocumentBuilder.NUM_OF_THREADS, indexPath,
            testExtensive, numOfClusters, numOfDocsForVocabulary, aggregatorClass);
    parallelIndexer.addExtractor(globalFeatureClass);
    parallelIndexer.addExtractor(localFeatureClass, cvsurf512);
    parallelIndexer.addExtractor(globalFeatureClass, keypointDetector, simpleceddcvsurf512);
    parallelIndexer.run();/* w ww.  ja v  a2 s .  co  m*/

    BufferedImage image = ImageIO.read(new FileInputStream(imageToSearch));

    IndexReader reader = DirectoryReader
            .open(new RAMDirectory(FSDirectory.open(Paths.get(indexPath)), IOContext.READONCE));
    System.out.println("Documents in the reader: " + reader.maxDoc());

    GenericFastImageSearcher ceddSearcher = new GenericFastImageSearcher(10, globalFeatureClass, true, reader);
    ImageSearchHits ceddhits = ceddSearcher.search(image, reader);
    String hitFile;
    for (int y = 0; y < ceddhits.length(); y++) {
        hitFile = reader.document(ceddhits.documentID(y)).getValues(DocumentBuilder.FIELD_NAME_IDENTIFIER)[0];
        hitFile = hitFile.substring(hitFile.lastIndexOf('\\') + 1);
        System.out.println(y + ". " + hitFile + " " + ceddhits.score(y));
    }
    System.out.println();

    GenericFastImageSearcher cvsurfsearcher = new GenericFastImageSearcher(10, localFeatureClass,
            aggregatorClass.newInstance(), 512, true, reader, indexPath + ".config");
    ImageSearchHits cvsurfhits = cvsurfsearcher.search(image, reader);
    for (int y = 0; y < cvsurfhits.length(); y++) {
        hitFile = reader.document(cvsurfhits.documentID(y)).getValues(DocumentBuilder.FIELD_NAME_IDENTIFIER)[0];
        hitFile = hitFile.substring(hitFile.lastIndexOf('\\') + 1);
        System.out.println(y + ". " + hitFile + " " + cvsurfhits.score(y));
    }
    System.out.println();

    GenericFastImageSearcher simpleceddcvsurfsearcher = new GenericFastImageSearcher(10, globalFeatureClass,
            keypointDetector, aggregatorClass.newInstance(), 512, true, reader, indexPath + ".config");
    ImageSearchHits simpleceddcvsurfhits = simpleceddcvsurfsearcher.search(image, reader);
    for (int y = 0; y < simpleceddcvsurfhits.length(); y++) {
        hitFile = reader.document(simpleceddcvsurfhits.documentID(y))
                .getValues(DocumentBuilder.FIELD_NAME_IDENTIFIER)[0];
        hitFile = hitFile.substring(hitFile.lastIndexOf('\\') + 1);
        System.out.println(y + ". " + hitFile + " " + simpleceddcvsurfhits.score(y));
    }
    System.out.println();
}

From source file:net.semanticmetadata.lire.searchers.TestSearching.java

License:Open Source License

public void testSearchMulImages() throws IOException, IllegalAccessException, InstantiationException {
    Cluster[] cvsurf512 = Cluster.readClusters(codebookPath + "CvSURF512");
    Cluster[] simpleceddcvsurf512 = Cluster.readClusters(codebookPath + "SIMPLEdetCVSURFCEDD512");

    ParallelIndexer parallelIndexer = new ParallelIndexer(DocumentBuilder.NUM_OF_THREADS, indexPath,
            testExtensive, numOfClusters, numOfDocsForVocabulary, aggregatorClass);
    parallelIndexer.addExtractor(globalFeatureClass);
    parallelIndexer.addExtractor(localFeatureClass, cvsurf512);
    parallelIndexer.addExtractor(globalFeatureClass, keypointDetector, simpleceddcvsurf512);
    parallelIndexer.run();//  w  w w.  j  av  a  2  s  .c o  m

    ArrayList<String> images = FileUtils.readFileLines(new File(testExtensive), true);

    IndexReader reader = DirectoryReader
            .open(new RAMDirectory(FSDirectory.open(Paths.get(indexPath)), IOContext.READONCE));
    System.out.println("Documents in the reader: " + reader.maxDoc());

    GenericFastImageSearcher ceddSearcher = new GenericFastImageSearcher(1, globalFeatureClass, true, reader);
    GenericFastImageSearcher cvsurfsearcher = new GenericFastImageSearcher(1, localFeatureClass,
            aggregatorClass.newInstance(), 512, true, reader, indexPath + ".config");
    GenericFastImageSearcher simpleceddcvsurfsearcher = new GenericFastImageSearcher(1, globalFeatureClass,
            keypointDetector, aggregatorClass.newInstance(), 512, true, reader, indexPath + ".config");

    BufferedImage image;
    ImageSearchHits ceddhits, cvsurfhits, simpleceddcvsurfhits;
    String hitFile;
    int counter = 0;
    for (String next : images) {
        image = ImageIO.read(new FileInputStream(next));
        next = next.substring(next.lastIndexOf('\\') + 1);
        System.out.println(counter + " ~ " + next);

        ceddhits = ceddSearcher.search(image, reader);
        hitFile = reader.document(ceddhits.documentID(0)).getValues(DocumentBuilder.FIELD_NAME_IDENTIFIER)[0];
        hitFile = hitFile.substring(hitFile.lastIndexOf('\\') + 1);
        if (next.equals(hitFile))
            System.out.println(0 + ". " + hitFile + " " + ceddhits.score(0));
        else
            System.err.println("ERROR " + hitFile + " " + ceddhits.score(0) + " ERROR");

        cvsurfhits = cvsurfsearcher.search(image, reader);
        hitFile = reader.document(cvsurfhits.documentID(0)).getValues(DocumentBuilder.FIELD_NAME_IDENTIFIER)[0];
        hitFile = hitFile.substring(hitFile.lastIndexOf('\\') + 1);
        if (next.equals(hitFile))
            System.out.println(0 + ". " + hitFile + " " + cvsurfhits.score(0));
        else
            System.err.println("ERROR " + hitFile + " " + cvsurfhits.score(0) + " ERROR");

        simpleceddcvsurfhits = simpleceddcvsurfsearcher.search(image, reader);
        hitFile = reader.document(simpleceddcvsurfhits.documentID(0))
                .getValues(DocumentBuilder.FIELD_NAME_IDENTIFIER)[0];
        hitFile = hitFile.substring(hitFile.lastIndexOf('\\') + 1);
        if (next.equals(hitFile))
            System.out.println(0 + ". " + hitFile + " " + simpleceddcvsurfhits.score(0));
        else
            System.err.println("ERROR " + hitFile + " " + simpleceddcvsurfhits.score(0) + " ERROR");

        counter++;
        System.out.println();
    }
}

From source file:net.semanticmetadata.lire.searchers.TestSearching.java

License:Open Source License

public void testSeparateIndex() throws IOException, IllegalAccessException, InstantiationException {
    Cluster[] cvsurf512 = Cluster.readClusters(codebookPath + "CvSURF512");
    Cluster[] simpleceddcvsurf512 = Cluster.readClusters(codebookPath + "SIMPLEdetCVSURFCEDD512");

    ParallelIndexer parallelIndexer = new ParallelIndexer(DocumentBuilder.NUM_OF_THREADS, indexPath,
            testExtensiveRed, numOfClusters, numOfDocsForVocabulary, aggregatorClass);
    parallelIndexer.addExtractor(globalFeatureClass);
    parallelIndexer.addExtractor(localFeatureClass, cvsurf512);
    parallelIndexer.addExtractor(globalFeatureClass, keypointDetector, simpleceddcvsurf512);
    parallelIndexer.run();//w  w  w  .j ava2  s .  c  o  m

    ParallelIndexer parallelIndexerSeparate = new ParallelIndexer(DocumentBuilder.NUM_OF_THREADS,
            indexPathSeparate, testExtensiveBlack, indexPath);
    parallelIndexerSeparate.run();

    IndexReader readerIndex = DirectoryReader
            .open(new RAMDirectory(FSDirectory.open(Paths.get(indexPath)), IOContext.READONCE));
    System.out.println("Documents in the reader: " + readerIndex.maxDoc());

    IndexReader readerQueries = DirectoryReader
            .open(new RAMDirectory(FSDirectory.open(Paths.get(indexPathSeparate)), IOContext.READONCE));
    System.out.println("Documents in the reader: " + readerQueries.maxDoc());

    GenericFastImageSearcher ceddSearcher = new GenericFastImageSearcher(5, globalFeatureClass, true,
            readerIndex);
    GenericFastImageSearcher cvsurfsearcher = new GenericFastImageSearcher(5, localFeatureClass,
            aggregatorClass.newInstance(), 512, true, readerIndex, indexPath + ".config");
    GenericFastImageSearcher simpleceddcvsurfsearcher = new GenericFastImageSearcher(5, globalFeatureClass,
            keypointDetector, aggregatorClass.newInstance(), 512, true, readerIndex, indexPath + ".config");

    Bits liveDocs = MultiFields.getLiveDocs(readerQueries);

    ImageSearchHits ceddhits, cvsurfhits, simpleceddcvsurfhits;
    Document queryDoc;
    String queryfile, hitFile;
    int counter = 0;
    for (int i = 0; i < readerQueries.maxDoc(); i++) {
        if (readerQueries.hasDeletions() && !liveDocs.get(i))
            continue;

        queryDoc = readerQueries.document(i);
        queryfile = queryDoc.getValues(DocumentBuilder.FIELD_NAME_IDENTIFIER)[0];
        queryfile = queryfile.substring(queryfile.lastIndexOf('\\') + 1);
        System.out.println(counter + ". Query image: " + queryfile);
        ceddhits = ceddSearcher.search(queryDoc, readerIndex);
        cvsurfhits = cvsurfsearcher.search(queryDoc, readerIndex);
        simpleceddcvsurfhits = simpleceddcvsurfsearcher.search(queryDoc, readerIndex);

        System.out.println("Global:");
        for (int y = 0; y < ceddhits.length(); y++) {
            hitFile = readerIndex.document(ceddhits.documentID(y))
                    .getValues(DocumentBuilder.FIELD_NAME_IDENTIFIER)[0];
            hitFile = hitFile.substring(hitFile.lastIndexOf('\\') + 1);
            System.out.println(y + ". " + hitFile + " " + ceddhits.score(y));
        }

        System.out.println("Local:");
        for (int y = 0; y < cvsurfhits.length(); y++) {
            hitFile = readerIndex.document(cvsurfhits.documentID(y))
                    .getValues(DocumentBuilder.FIELD_NAME_IDENTIFIER)[0];
            hitFile = hitFile.substring(hitFile.lastIndexOf('\\') + 1);
            System.out.println(y + ". " + hitFile + " " + cvsurfhits.score(y));
        }

        System.out.println("Simple:");
        for (int y = 0; y < simpleceddcvsurfhits.length(); y++) {
            hitFile = readerIndex.document(simpleceddcvsurfhits.documentID(y))
                    .getValues(DocumentBuilder.FIELD_NAME_IDENTIFIER)[0];
            hitFile = hitFile.substring(hitFile.lastIndexOf('\\') + 1);
            System.out.println(y + ". " + hitFile + " " + simpleceddcvsurfhits.score(y));
        }
        System.out.println();
        counter++;
    }
}

From source file:org.apache.ctakes.core.resource.LuceneIndexReaderResourceImpl.java

License:Apache License

/**
 * Loads a Lucene index for reading.//from  ww w.java 2 s .c o  m
 */
public void load(DataResource dr) throws ResourceInitializationException {

    ConfigurationParameterSettings cps = dr.getMetaData().getConfigurationParameterSettings();
    Boolean useMemoryIndex = (Boolean) cps.getParameterValue("UseMemoryIndex");

    String indexDirStr = (String) cps.getParameterValue("IndexDirectory");
    try {

        File indexDir = FileLocator.locateFile(indexDirStr);

        if (!indexDir.exists())
            iv_logger.info("indexDir=" + indexDirStr + "  does not exist!");
        else
            iv_logger.info("indexDir=" + indexDirStr + "  exists.");

        if (useMemoryIndex.booleanValue()) {

            iv_logger.info("Loading Lucene Index into memory: " + indexDir);
            FSDirectory fsd = FSDirectory.open(indexDir);
            Directory d = new RAMDirectory(fsd, IOContext.DEFAULT);
            iv_indexReader = IndexReader.open(d);
        } else {
            iv_logger.info("Loading Lucene Index: " + indexDir);
            FSDirectory fsd = FSDirectory.open(indexDir);
            iv_indexReader = IndexReader.open(fsd);
        }
        iv_logger.info("Loaded Lucene Index, # docs=" + iv_indexReader.numDocs());
    } catch (Exception e) {
        throw new ResourceInitializationException(e);
    }
}