Example usage for org.apache.lucene.store Directory toString

List of usage examples for org.apache.lucene.store Directory toString

Introduction

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

Prototype

@Override
    public String toString() 

Source Link

Usage

From source file:axiom.objectmodel.dom.IndexOptimizingRunner.java

License:Open Source License

public IndexOptimizingRunner(Directory d, Application app, LuceneManager lmgr) throws Exception {
    this.app = app;
    this.directory = d;
    this.indexPath = d.toString();
    this.optimizer = new IndexOptimizer(d);
    this.optimizer.setUseCompoundFile(true);
    this.optimizer.setMergeFactor(Integer.MAX_VALUE);
    this.tmgr = TransactionManager.newInstance(app.getTransSource());
    this.lmgr = lmgr;
}

From source file:axiom.objectmodel.dom.LuceneOptimizer.java

License:Open Source License

public LuceneOptimizer(Directory d, Application app, LuceneManager lmgr) throws Exception {
    this.app = app;
    this.directory = d;
    this.indexPath = d.toString();
    this.optimizer = new IndexOptimizer(d);
    this.optimizer.setUseCompoundFile(true);
    this.optimizer.setMergeFactor(Integer.MAX_VALUE);
    this.tmgr = TransactionManager.newInstance(app.getTransSource());
    this.lmgr = lmgr;
}

From source file:collene.Freedb.java

License:Apache License

public static void BuildIndex(Directory directory) throws Exception {
    String freedbPath = "/Users/gdusbabek/Downloads/freedb-complete-20140701.tar.bz2";

    if (directory == null) {
        System.out.println("Need to specify: { memory | file | cassandra }. Did you misspell something?");
        System.exit(-1);/*from  w  w  w  . ja v a  2 s .c  om*/
    }

    FreeDbReader reader = new FreeDbReader(new File(freedbPath), 50000);
    reader.start();

    long indexStart = System.currentTimeMillis();
    Collection<Document> documents = new ArrayList<Document>(BATCH_SIZE);
    Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_4_9);
    IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_4_9, analyzer);
    config.setOpenMode(IndexWriterConfig.OpenMode.CREATE);
    IndexWriter writer = new IndexWriter(directory, config);

    // stop after this many documents.
    final int maxDocuments = 400000; //Integer.MAX_VALUE;

    FreeDbEntry entry = reader.next();
    int count = 0;
    while (entry != null && count < maxDocuments) {
        Document doc = new Document();
        String any = entry.toString();
        doc.add(new Field("any", any, TextField.TYPE_STORED));
        doc.add(new Field("artist", entry.getArtist(), TextField.TYPE_NOT_STORED));
        doc.add(new Field("album", entry.getAlbum(), TextField.TYPE_NOT_STORED));
        doc.add(new Field("title", entry.getTitle(), TextField.TYPE_NOT_STORED));
        doc.add(new Field("genre", entry.getGenre(), TextField.TYPE_NOT_STORED));
        doc.add(new Field("year", entry.getYear(), TextField.TYPE_NOT_STORED));
        for (int i = 0; i < entry.getTrackCount(); i++) {
            doc.add(new Field("track", entry.getTrack(i), TextField.TYPE_STORED));
        }
        documents.add(doc);
        if (VERBOSE) {
            out.println(any);
        }

        if (documents.size() == BATCH_SIZE) {
            //out.println(String.format("Adding batch at count %d", count));
            writer.addDocuments(documents);
            //out.println("done");
            documents.clear();
        }

        count += 1;
        if (count >= MAX_ENTRIES) {
            // done indexing.
            break;
        }
        entry = reader.next();

        if (count % 100000 == 0) {
            out.println(String.format("Indexed %d documents", count));

            // do a quick morrissey search for fun.
            //                IndexSearcher searcher = new IndexSearcher(DirectoryReader.open(ColDirectory.open(
            //                                new CassandraIO(8192, "collene", "cindex").start("127.0.0.1:9042"),
            //                                new CassandraIO(8192, "collene", "cmeta").start("127.0.0.1:9042"),
            //                                new CassandraIO(8192, "collene", "clock").start("127.0.0.1:9042")
            //                )));
            IndexSearcher searcher = new IndexSearcher(DirectoryReader.open(writer, false));
            QueryParser parser = new QueryParser(Version.LUCENE_4_9, "any", analyzer);
            long searchStart = System.currentTimeMillis();
            Query query = parser.parse("morrissey");
            TopDocs docs = searcher.search(query, 10);
            long searchEnd = System.currentTimeMillis();
            out.println(String.format("%s %d total hits in %d", directory.getClass().getSimpleName(),
                    docs.totalHits, searchEnd - searchStart));
            for (ScoreDoc d : docs.scoreDocs) {
                out.println(String.format("%d %.2f %d", d.doc, d.score, d.shardIndex));
            }
        }
    }

    if (documents.size() > 0) {
        out.println(String.format("Adding batch at count %d", count));
        writer.addDocuments(documents);
        out.println("done");
        documents.clear();

        // do a quick morrissey search for fun.
        IndexSearcher searcher = new IndexSearcher(DirectoryReader.open(writer, false));
        QueryParser parser = new QueryParser(Version.LUCENE_4_9, "any", analyzer);
        long searchStart = System.currentTimeMillis();
        Query query = parser.parse("morrissey");
        TopDocs docs = searcher.search(query, 10);
        long searchEnd = System.currentTimeMillis();
        out.println(String.format("%s %d total hits in %d", directory.getClass().getSimpleName(),
                docs.totalHits, searchEnd - searchStart));
        for (ScoreDoc d : docs.scoreDocs) {
            out.println(String.format("%d %.2f %d", d.doc, d.score, d.shardIndex));
        }
    }

    long indexTime = System.currentTimeMillis() - indexStart;
    out.println(String.format("Indexed %d things in %d ms (%s)", count, indexTime, directory.toString()));

    //        long startMerge = System.currentTimeMillis();
    //        writer.forceMerge(1, true);
    //        long endMerge = System.currentTimeMillis();
    //        out.println(String.format("merge took %d ms", endMerge-startMerge));
    out.println("I think these are the files:");
    for (String s : directory.listAll()) {
        out.println(s);
    }

    writer.close(true);
    directory.close();
}

From source file:com.fuerve.villageelder.client.commandline.commands.Index.java

License:Apache License

/**
 * Given a Lucene {@link Directory}, extracts the pathname
 * from it as a string./*w w w.  j a v a2  s.  co  m*/
 * @param directory The {@link Directory} from which to extract
 * the pathname.
 * @return The string pathname of the {@link Directory}
 */
private String extractPathFromDirectory(final Directory directory) {
    String dirStr = directory.toString();
    int strBegin = dirStr.indexOf('@') + 1;
    int strEnd = dirStr.indexOf(" ", strBegin);
    return dirStr.substring(strBegin, strEnd);
}

From source file:com.ifountain.compass.MemoryMirrorDirectoryWrapper.java

License:Apache License

public MemoryMirrorDirectoryWrapper(Directory dir, long awaitTermination, long maxNumberOfUnProcessedBytes,
        long minNumberOfUnProcessedBytes, ExecutorService executorService) throws IOException {
    log.info("Initializing FileAndMemory storage type for directory " + dir.toString() + " awaitTermination :"
            + awaitTermination + " maxNumberOfUnProcessedBytes:" + maxNumberOfUnProcessedBytes
            + " minNumberOfUnProcessedBytes:" + minNumberOfUnProcessedBytes);
    this.dir = dir;
    this.maxNumberOfUnProcessedBytes = maxNumberOfUnProcessedBytes;
    this.minNumberOfUnProcessedBytes = minNumberOfUnProcessedBytes;
    this.ramDir = new RAMDirectory(dir);
    this.ramDir.setLockFactory(dir.getLockFactory());
    this.executorService = executorService;
    this.awaitTermination = awaitTermination;
}

From source file:com.impetus.kundera.index.LuceneIndexer.java

License:Apache License

private void copy(Directory src, Directory to) throws IOException {
    for (String file : src.listAll()) {
        src.copyFrom(src, file, to.toString(), IOContext.DEFAULT);
    }/*from   w w  w. j a  va2 s.  c om*/
}

From source file:org.elasticsearch.index.store.FsDirectoryServiceTests.java

License:Apache License

private void doTestPreload(String... preload) throws IOException {
    Settings build = Settings.builder().put(IndexModule.INDEX_STORE_TYPE_SETTING.getKey(), "mmapfs")
            .putArray(IndexModule.INDEX_STORE_PRE_LOAD_SETTING.getKey(), preload).build();
    IndexSettings settings = IndexSettingsModule.newIndexSettings("foo", build);
    IndexStoreConfig config = new IndexStoreConfig(settings.getSettings());
    IndexStore store = new IndexStore(settings, config);
    Path tempDir = createTempDir().resolve(settings.getUUID()).resolve("0");
    Files.createDirectories(tempDir);
    ShardPath path = new ShardPath(false, tempDir, tempDir, new ShardId(settings.getIndex(), 0));
    FsDirectoryService fsDirectoryService = new FsDirectoryService(settings, store, path);
    Directory directory = fsDirectoryService.newDirectory();
    assertTrue(directory instanceof RateLimitedFSDirectory);
    RateLimitedFSDirectory rateLimitingDirectory = (RateLimitedFSDirectory) directory;
    Directory delegate = rateLimitingDirectory.getDelegate();
    assertFalse(delegate instanceof SleepingLockWrapper);
    if (preload.length == 0) {
        assertTrue(delegate.toString(), delegate instanceof MMapDirectory);
        assertFalse(((MMapDirectory) delegate).getPreload());
    } else if (Arrays.asList(preload).contains("*")) {
        assertTrue(delegate.toString(), delegate instanceof MMapDirectory);
        assertTrue(((MMapDirectory) delegate).getPreload());
    } else {//from   www .  j a  v  a  2  s  .co  m
        assertTrue(delegate.toString(), delegate instanceof FileSwitchDirectory);
        FileSwitchDirectory fsd = (FileSwitchDirectory) delegate;
        assertTrue(fsd.getPrimaryDir() instanceof MMapDirectory);
        assertTrue(((MMapDirectory) fsd.getPrimaryDir()).getPreload());
        assertTrue(fsd.getSecondaryDir() instanceof MMapDirectory);
        assertFalse(((MMapDirectory) fsd.getSecondaryDir()).getPreload());
    }
}

From source file:org.encuestame.business.search.SearchUtils.java

License:Apache License

/**
 * Open Index Writer//from  w  ww .java  2s.  c o  m
 * @param directoryStore
 * @param indexWriter
 * @throws CorruptIndexException
 * @throws LockObtainFailedException
 * @throws IOException
 */
public static IndexWriter openIndexWriter(final DirectoryIndexStore directoryStore, IndexWriter indexWriter)
        throws CorruptIndexException, LockObtainFailedException, IOException {
    final Directory directory = directoryStore.getDirectory();
    log.debug("Get Directory ----------" + directory.toString());
    if (indexWriter != null) {
        indexWriter.close();
    }
    //log.debug("Index Directory is locked?  ----------> " + indexWriter.isLocked(directory));
    IndexWriterConfig config = new IndexWriterConfig(new StandardAnalyzer());
    indexWriter = new IndexWriter(directory, config);
    Assert.notNull(indexWriter);
    return indexWriter;
}

From source file:org.hibernate.search.store.spi.DirectoryHelper.java

License:LGPL

/**
 * Initialize the Lucene Directory if it isn't already.
 *
 * @param directory the Directory to initialize
 * @throws SearchException in case of lock acquisition timeouts, IOException, or if a corrupt index is found
 *//*w w w .j  a va 2 s  . c  om*/
public static void initializeIndexIfNeeded(Directory directory) {
    SimpleAnalyzer analyzer = new SimpleAnalyzer();
    try {
        if (!DirectoryReader.indexExists(directory)) {
            try {
                IndexWriterConfig iwriterConfig = new IndexWriterConfig(analyzer)
                        .setOpenMode(OpenMode.CREATE_OR_APPEND);
                //Needs to have a timeout higher than zero to prevent race conditions over (network) RPCs
                //for distributed indexes (Infinispan but probably also NFS and similar)
                iwriterConfig.setWriteLockTimeout(2000);
                IndexWriter iw = new IndexWriter(directory, iwriterConfig);
                iw.close();
            } catch (LockObtainFailedException lofe) {
                log.lockingFailureDuringInitialization(directory.toString());
            }
        }
    } catch (IOException e) {
        throw new SearchException("Could not initialize index", e);
    } finally {
        analyzer.close();
    }
}

From source file:org.hibernate.search.test.performance.util.CheckerLuceneIndex.java

License:LGPL

@SuppressWarnings("deprecation") // performance tests can be run against older hsearch versions, where isn't getIndexManagerHolder yet
public static void printIndexReport(TestContext ctx, PrintStream out) throws IOException {
    if (!CHECK_INDEX_STATE) {
        return;/*from  w ww.  j a  v  a2  s.  com*/
    }

    out.println("INDEX CHECK...");
    out.println("");

    Session s = ctx.sf.openSession();
    FullTextSession fts = Search.getFullTextSession(s);
    ExtendedSearchIntegrator integrator = fts.getSearchFactory().unwrap(ExtendedSearchIntegrator.class);
    Collection<IndexManager> indexManagers = integrator.getIndexManagerHolder().getIndexManagers();

    for (IndexManager indexManager : indexManagers) {
        DirectoryBasedIndexManager directoryBasedIndexManager = (DirectoryBasedIndexManager) indexManager;
        stopBackend(directoryBasedIndexManager);
        DirectoryProvider<?> directoryProvider = directoryBasedIndexManager.getDirectoryProvider();
        Directory directory = directoryProvider.getDirectory();

        out.println("directory : " + directory.toString());
        out.println("");

        CheckIndex checkIndex = new CheckIndex(directory);
        checkIndex.setInfoStream(out);
        Status status;
        try {
            status = checkIndex.checkIndex();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        assertTrue(status.clean);
    }

    out.println("==================================================================");
    out.flush();
}