Example usage for org.apache.lucene.index DirectoryReader open

List of usage examples for org.apache.lucene.index DirectoryReader open

Introduction

In this page you can find the example usage for org.apache.lucene.index DirectoryReader open.

Prototype

public static DirectoryReader open(final IndexCommit commit) throws IOException 

Source Link

Document

Expert: returns an IndexReader reading the index in the given IndexCommit .

Usage

From source file:ai.castor.idf.FetchTermIDF.java

License:Apache License

public FetchTermIDF(FetchTermIDF.Args args) throws Exception {
    Path indexPath = Paths.get(args.index);
    if (!Files.exists(indexPath) || !Files.isDirectory(indexPath) || !Files.isReadable(indexPath)) {
        throw new IllegalArgumentException(
                args.index + " does not exist, is not a directory, or is not readable");
    }//from  w  ww  .j av  a  2 s .  co  m
    this.directory = FSDirectory.open(indexPath);
    this.reader = DirectoryReader.open(this.directory); // #changed
}

From source file:ai.castor.idf.IDFScorer.java

License:Apache License

public IDFScorer(IDFScorer.Args args) throws Exception {
    Path indexPath = Paths.get(args.index);
    if (!Files.exists(indexPath) || !Files.isDirectory(indexPath) || !Files.isReadable(indexPath)) {
        throw new IllegalArgumentException(args.index + " does not exist or is not a directory.");
    }//from   w  w  w.ja v a 2  s  .  co  m
    this.directory = FSDirectory.open(indexPath);
    this.reader = DirectoryReader.open(directory);

    stopWords = new ArrayList<>();

    //Get file from resources folder
    InputStream is = getClass().getResourceAsStream("/ai/castor/qa/english-stoplist.txt");
    BufferedReader bRdr = new BufferedReader(new InputStreamReader(is));
    String line;
    while ((line = bRdr.readLine()) != null) {
        if (!line.contains("#")) {
            stopWords.add(line);
        }
    }
}

From source file:alix.lucene.MoreLikeThis.java

License:Apache License

/**
 * A Cli test /*from   ww  w.  j a  v  a 2 s.  c  om*/
 * @return 
 */
public static void main(String args[]) throws Exception {
    String usage = "java com.github.oeuvres.lucene.MoreLikeThis" + " ../lucene-index \n\n"
            + "Parse the files in corpus, with xsl parser, to be indexed in lucene index directory";
    if (args.length < 1) {
        System.err.println("Usage: " + usage);
        System.exit(1);
    }
    IndexReader ir = DirectoryReader.open(FSDirectory.open(Paths.get(args[0])));
    MoreLikeThis mlt = new MoreLikeThis(ir);
    mlt.setMinTermFreq(0);
    mlt.setMinDocFreq(0);
    // TODO select Field
    String fieldName = "text";
    mlt.setFieldNames(new String[] { fieldName });

    int maxDoc = ir.docFreq(new Term("type", "chapter"));
    System.out.println("<p>Out of " + maxDoc + "</p>");
    /* 
    Should use a doc Freq level to clean grammatical words
    On a Zola chapter
    100%: de, la il, le, et, , l, un, d
    100% - 1: il, et, vous
    99%: on, a, est, vous 
    95%: elles
    90%: chez, a
    50%: noms propres
    */
    mlt.setMaxDocFreq((int) Math.round(maxDoc * 0.50));
    mlt.setLower(true);

    int docnum;
    BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in, StandardCharsets.UTF_8));
    while (true) {
        System.out.println("Doc no:");
        String line = keyboard.readLine().trim();
        if (line == null || "".equals(line))
            System.exit(0);
        docnum = Integer.parseInt(line);
        System.out.println(Arrays.toString(mlt.retrieveInterestingTerms(docnum)));
    }
}

From source file:antnlp.opie.indexsearch.SearchFiles.java

License:Apache License

/** Simple command-line based search demo. */
public static void main(String[] args) throws Exception {
    String usage = "Usage:\tjava org.apache.lucene.demo.SearchFiles [-index dir] [-field f] [-repeat n] [-queries file] [-query string] [-raw] [-paging hitsPerPage]\n\nSee http://lucene.apache.org/core/4_1_0/demo/ for details.";
    if (args.length > 0 && ("-h".equals(args[0]) || "-help".equals(args[0]))) {
        System.out.println(usage);
        System.exit(0);//  w w  w.j a v  a 2  s .  c  om
    }

    String index = "index";
    String field = "contents";
    String queries = null;
    int repeat = 0;
    boolean raw = false;
    String queryString = null;
    int hitsPerPage = 10;

    for (int i = 0; i < args.length; i++) {
        if ("-index".equals(args[i])) {
            index = args[i + 1];
            i++;
        } else if ("-field".equals(args[i])) {
            field = args[i + 1];
            i++;
        } else if ("-queries".equals(args[i])) {
            queries = args[i + 1];
            i++;
        } else if ("-query".equals(args[i])) {
            queryString = args[i + 1];
            i++;
        } else if ("-repeat".equals(args[i])) {
            repeat = Integer.parseInt(args[i + 1]);
            i++;
        } else if ("-raw".equals(args[i])) {
            raw = true;
        } else if ("-paging".equals(args[i])) {
            hitsPerPage = Integer.parseInt(args[i + 1]);
            if (hitsPerPage <= 0) {
                System.err.println("There must be at least 1 hit per page.");
                System.exit(1);
            }
            i++;
        }
    }

    IndexReader reader = DirectoryReader.open(FSDirectory.open(Paths.get(index)));
    IndexSearcher searcher = new IndexSearcher(reader);
    //Analyzer analyzer = new StandardAnalyzer();
    //Analyzer analyzer = new StandardAnalyzer(CharArraySet.EMPTY_SET);
    Analyzer analyzer = new WhitespaceAnalyzer();

    BufferedReader in = null;
    if (queries != null) {
        in = Files.newBufferedReader(Paths.get(queries), StandardCharsets.UTF_8);
    } else {
        in = new BufferedReader(new InputStreamReader(System.in, StandardCharsets.UTF_8));
    }
    //QueryParser parser = new QueryParser(field, analyzer);
    QueryBuilder builder = new QueryBuilder(analyzer);
    while (true) {
        if (queries == null && queryString == null) { // prompt the user
            System.out.println("Enter query: ");
        }

        String line = queryString != null ? queryString : in.readLine();

        if (line == null || line.length() == -1) {
            break;
        }

        line = line.trim();
        if (line.length() == 0) {
            break;
        }

        //Query query = parser.parse(line);
        Query query = builder.createPhraseQuery(field, line);
        System.out.println("Searching for: " + query.toString(field));

        if (repeat > 0) { // repeat & time as benchmark
            Date start = new Date();
            for (int i = 0; i < repeat; i++) {
                searcher.search(query, 100);
            }
            Date end = new Date();
            System.out.println("Time: " + (end.getTime() - start.getTime()) + "ms");
        }

        doPagingSearch(in, searcher, query, hitsPerPage, raw, queries == null && queryString == null);

        if (queryString != null) {
            break;
        }
    }
    reader.close();
}

From source file:aos.lucene.admin.SearcherManager.java

License:Apache License

public SearcherManager(Directory dir) throws IOException {
    currentSearcher = new IndexSearcher(DirectoryReader.open(dir));
    warm(currentSearcher);
}

From source file:aos.lucene.admin.SearcherManager.java

License:Apache License

public SearcherManager(IndexWriter writer) throws IOException {

    this.writer = writer;
    IndexReader reader = DirectoryReader.open(writer.getDirectory());
    currentSearcher = new IndexSearcher(reader);
    warm(currentSearcher);/* ww  w .j a  va 2 s.c  o  m*/

    writer.getConfig().setMergedSegmentWarmer(new IndexWriter.IndexReaderWarmer() {
        public void warm(AtomicReader reader) throws IOException {
            SearcherManager.this.warm(new IndexSearcher(reader));
        }
    });
}

From source file:aos.lucene.search.msc.BasicSearchingTest.java

License:Apache License

public void testTerm() throws Exception {

    Directory dir = FSDirectory.open(new File("target/index"));
    IndexReader reader = DirectoryReader.open(dir);
    IndexSearcher searcher = new IndexSearcher(reader);

    Term t = new Term("subject", "ant");
    Query query = new TermQuery(t);
    TopDocs docs = searcher.search(query, 10);
    assertEquals("Ant in Action", 1, docs.totalHits);

    t = new Term("subject", "junit");
    docs = searcher.search(new TermQuery(t), 10);
    assertEquals("Ant in Action, " + "JUnit in Action, Second Edition", 2, docs.totalHits);

    reader.close();/*from ww w  .j a va 2  s  .c o m*/
    dir.close();
}

From source file:aos.lucene.search.msc.Fragments.java

License:Apache License

public void openSearcher() throws Exception {
    Directory dir = FSDirectory.open(new File("/path/to/index"));
    IndexReader reader = DirectoryReader.open(dir);
    IndexSearcher searcher = new IndexSearcher(reader);
}

From source file:aos.lucene.tools.BooksMoreLikeThis.java

License:Apache License

public static void main(String[] args) throws Throwable {

    String indexDir = System.getProperty("index.dir");
    FSDirectory directory = FSDirectory.open(new File(indexDir));
    IndexReader reader = DirectoryReader.open(directory);

    IndexSearcher searcher = new IndexSearcher(reader);

    int numDocs = reader.maxDoc();

    MoreLikeThis mlt = new MoreLikeThis(reader);
    mlt.setFieldNames(new String[] { "title", "author" });
    mlt.setMinTermFreq(1);/*from ww  w  .j a v  a 2  s .  c  o  m*/
    mlt.setMinDocFreq(1);

    for (int docID = 0; docID < numDocs; docID++) {
        LOGGER.info();
        Document doc = reader.document(docID);
        LOGGER.info(doc.get("title"));

        Query query = mlt.like(docID);
        LOGGER.info("  query=" + query);

        TopDocs similarDocs = searcher.search(query, 10);
        if (similarDocs.totalHits == 0)
            LOGGER.info("  None like this");
        for (int i = 0; i < similarDocs.scoreDocs.length; i++) {
            if (similarDocs.scoreDocs[i].doc != docID) {
                doc = reader.document(similarDocs.scoreDocs[i].doc);
                LOGGER.info("  -> " + doc.getField("title").stringValue());
            }
        }
    }

    reader.close();
    directory.close();
}

From source file:api.reader.LuceneIndexReader.java

License:Open Source License

/**
 * This is called as a part of the startup for the startup.
 * When running an application that is on the web application, the api.reader is started by the startup script.
 *
 * @param filename The index directory/*from ww  w  .j ava 2s  .  c  o  m*/
 * @return if the index initialization succeeds return true.
 */
@Override
public boolean initializeIndexReader(String filename) {
    try {
        READER = DirectoryReader.open(FSDirectory.open(FileSystems.getDefault().getPath(filename)));
    } catch (IOException e) {
        READER = null;
        log.error("Failed to instantiate the index api.reader with directory: " + filename);
    }
    return isInitialized();
}