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.leavesfly.lia.analysis.UsingAnalyzersExample.java

License:Apache License

/**
 * This method doesn't do anything, except compile correctly.
 * This is used to show snippets of how Analyzers are used.
 *///from w  ww  . j av  a 2 s .c o  m
public void someMethod() throws IOException, ParseException {
    RAMDirectory directory = new RAMDirectory();

    Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_30);
    IndexWriter writer = new IndexWriter(directory, analyzer, IndexWriter.MaxFieldLength.UNLIMITED);

    Document doc = new Document();
    doc.add(new Field("title", "This is the title", Field.Store.YES, Field.Index.ANALYZED));
    doc.add(new Field("contents", "...document contents...", Field.Store.NO, Field.Index.ANALYZED));
    writer.addDocument(doc);

    writer.addDocument(doc, analyzer);

    String expression = "some query";

    Query query = new QueryParser(Version.LUCENE_30, "contents", analyzer).parse(expression);

    QueryParser parser = new QueryParser(Version.LUCENE_30, "contents", analyzer);
    query = parser.parse(expression);
}

From source file:com.leavesfly.lia.extsearch.sorting.DistanceSortingTest.java

License:Apache License

protected void setUp() throws Exception {
    directory = new RAMDirectory();
    IndexWriter writer = new IndexWriter(directory, new WhitespaceAnalyzer(),
            IndexWriter.MaxFieldLength.UNLIMITED);
    addPoint(writer, "El Charro", "restaurant", 1, 2);
    addPoint(writer, "Cafe Poca Cosa", "restaurant", 5, 9);
    addPoint(writer, "Los Betos", "restaurant", 9, 6);
    addPoint(writer, "Nico's Taco Shop", "restaurant", 3, 8);

    writer.close();//from  w  w w  . j  a  va 2s.c o m

    searcher = new IndexSearcher(directory);

    query = new TermQuery(new Term("type", "restaurant"));
}

From source file:com.leavesfly.lia.searching.NearRealTimeTest.java

License:Apache License

public void testNearRealTime() throws Exception {
    Directory dir = new RAMDirectory();
    IndexWriter writer = new IndexWriter(dir, new StandardAnalyzer(Version.LUCENE_30),
            IndexWriter.MaxFieldLength.UNLIMITED);
    for (int i = 0; i < 10; i++) {
        Document doc = new Document();
        doc.add(new Field("id", "" + i, Field.Store.NO, Field.Index.NOT_ANALYZED_NO_NORMS));
        doc.add(new Field("text", "aaa", Field.Store.NO, Field.Index.ANALYZED));
        writer.addDocument(doc);/*from  w  ww  .  ja  va  2s . c  om*/
    }
    IndexReader reader = writer.getReader(); // #1
    IndexSearcher searcher = new IndexSearcher(reader); // #A

    Query query = new TermQuery(new Term("text", "aaa"));
    TopDocs docs = searcher.search(query, 1);
    assertEquals(10, docs.totalHits); // #B

    writer.deleteDocuments(new Term("id", "7")); // #2

    Document doc = new Document(); // #3
    doc.add(new Field("id", // #3
            "11", // #3
            Field.Store.NO, // #3
            Field.Index.NOT_ANALYZED_NO_NORMS)); // #3
    doc.add(new Field("text", // #3
            "bbb", // #3
            Field.Store.NO, // #3
            Field.Index.ANALYZED)); // #3
    writer.addDocument(doc); // #3

    IndexReader newReader = reader.reopen(); // #4
    assertFalse(reader == newReader); // #5
    reader.close(); // #6
    searcher = new IndexSearcher(newReader);

    TopDocs hits = searcher.search(query, 10); // #7
    assertEquals(9, hits.totalHits); // #7

    query = new TermQuery(new Term("text", "bbb")); // #8
    hits = searcher.search(query, 1); // #8
    assertEquals(1, hits.totalHits); // #8

    newReader.close();
    writer.close();
}

From source file:com.leavesfly.lia.searching.PhraseQueryTest.java

License:Apache License

protected void setUp() throws IOException {
    dir = new RAMDirectory();
    IndexWriter writer = new IndexWriter(dir, new WhitespaceAnalyzer(), IndexWriter.MaxFieldLength.UNLIMITED);
    Document doc = new Document();
    doc.add(new Field("field", // 1
            "the quick brown fox jumped over the lazy dog", // 1
            Field.Store.YES, // 1
            Field.Index.ANALYZED)); // 1
    writer.addDocument(doc);/*w w w  .  j  a  va 2s .  co m*/
    writer.close();

    searcher = new IndexSearcher(dir);
}

From source file:com.leavesfly.lia.tool.ChainedFilterTest.java

License:Apache License

public void setUp() throws Exception {
    directory = new RAMDirectory();
    IndexWriter writer = new IndexWriter(directory, new WhitespaceAnalyzer(),
            IndexWriter.MaxFieldLength.UNLIMITED);

    Calendar cal = Calendar.getInstance();
    cal.set(2009, 1, 1, 0, 0); // A

    for (int i = 0; i < MAX; i++) {
        Document doc = new Document();
        doc.add(new Field("key", "" + (i + 1), Field.Store.YES, Field.Index.NOT_ANALYZED));
        doc.add(new Field("owner", (i < MAX / 2) ? "bob" : "sue", Field.Store.YES, Field.Index.NOT_ANALYZED));
        doc.add(new Field("date", DateTools.timeToString(cal.getTimeInMillis(), DateTools.Resolution.DAY),
                Field.Store.YES, Field.Index.NOT_ANALYZED));
        writer.addDocument(doc);/* w w  w.  jav a 2  s  . co m*/

        cal.add(Calendar.DATE, 1);
    }

    writer.close();

    searcher = new IndexSearcher(directory);

    BooleanQuery bq = new BooleanQuery(); // B
    bq.add(new TermQuery(new Term("owner", "bob")), // B
            BooleanClause.Occur.SHOULD); // B
    bq.add(new TermQuery(new Term("owner", "sue")), // B
            BooleanClause.Occur.SHOULD); // B
    query = bq;

    cal.set(2099, 1, 1, 0, 0);
    dateFilter = TermRangeFilter.Less("date", // C
            DateTools.timeToString( // C
                    cal.getTimeInMillis(), // C
                    DateTools.Resolution.DAY));// C

    bobFilter = new CachingWrapperFilter( // D
            new QueryWrapperFilter( // D
                    new TermQuery(new Term("owner", "bob")))); // D

    sueFilter = new CachingWrapperFilter( // E
            new QueryWrapperFilter( // E
                    new TermQuery(new Term("owner", "sue")))); // E
}

From source file:com.leavesfly.lia.tool.SpatialLuceneExample.java

License:Apache License

SpatialLuceneExample() throws IOException {
    directory = new RAMDirectory();
    writer = new IndexWriter(directory, new WhitespaceAnalyzer(), MaxFieldLength.UNLIMITED);
}

From source file:com.liferay.portal.search.lucene.dump.DumpIndexDeletionPolicyTest.java

License:Open Source License

@Override
public void setUp() throws Exception {
    _sourceDirectory = new RAMDirectory();
    _dumpIndexDeletionPolicy = new DumpIndexDeletionPolicy();

    _indexWriter = new IndexWriter(_sourceDirectory, new StandardAnalyzer(Version.LUCENE_30),
            _dumpIndexDeletionPolicy, IndexWriter.MaxFieldLength.UNLIMITED);
}

From source file:com.liferay.portal.search.lucene.dump.DumpIndexDeletionPolicyTest.java

License:Open Source License

private Directory _dumpToTargetDirectory(IndexWriter indexWriter) throws Exception {

    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

    _dumpIndexDeletionPolicy.dump(byteArrayOutputStream, indexWriter, new ReentrantLock());

    byte[] bytes = byteArrayOutputStream.toByteArray();

    Directory targetDirectory = new RAMDirectory();

    IndexCommitSerializationUtil.deserializeIndex(new ByteArrayInputStream(bytes), targetDirectory);

    return targetDirectory;
}

From source file:com.liferay.portal.search.lucene.IndexAccessorImpl.java

License:Open Source License

private Directory _getLuceneDirRam() {
    String path = _getPath();// www  .  j  a v  a 2s  . c o m

    Directory directory = _ramDirectories.get(path);

    if (directory == null) {
        directory = new RAMDirectory();

        _ramDirectories.put(path, directory);
    }

    return directory;
}

From source file:com.lin.studytest.lucene.IndexFiles.java

License:Apache License

/** Index all text files under a directory. */
public static void main(String[] args) {
    //      String usage = "java org.apache.lucene.demo.IndexFiles"
    //            + " [-index INDEX_PATH] [-docs DOCS_PATH] [-update]\n\n"
    //            + "This indexes the documents in DOCS_PATH, creating a Lucene index"
    //            + "in INDEX_PATH that can be searched with SearchFiles";
    String indexPath = "D:\\software\\lucene\\testdata\\indexpath";
    String docsPath = "D:\\software\\lucene\\testdata\\docpath";
    RAMDirectory ramDirectory = new RAMDirectory();

    boolean create = false;
    //      for(int i=0;i<args.length;i++) {
    //         if ("-index".equals(args[i])) {
    //            indexPath = args[i+1];
    //            i++;
    //         } else if ("-docs".equals(args[i])) {
    //            docsPath = args[i+1];
    //            i++;
    //         } else if ("-update".equals(args[i])) {
    //            create = false;
    //         }/* ww  w.  j av  a2  s  .c  o  m*/
    //      }

    //      if (docsPath == null) {
    //         System.err.println("Usage: " + usage);
    //         System.exit(1);
    //      }

    final Path docDir = Paths.get(docsPath);
    if (!Files.isReadable(docDir)) {
        System.out.println("Document directory '" + docDir.toAbsolutePath()
                + "' does not exist or is not readable, please check the path");
        System.exit(1);
    }

    Date start = new Date();
    try {
        System.out.println("Indexing to directory '" + indexPath + "'...");

        Directory dir = FSDirectory.open(Paths.get(indexPath));
        Analyzer analyzer = new SmartChineseAnalyzer();
        IndexWriterConfig iwc = new IndexWriterConfig(analyzer);

        if (create) {
            // Create a new index in the directory, removing any
            // previously indexed documents:
            iwc.setOpenMode(OpenMode.CREATE);
        } else {
            // Add new documents to an existing index:
            iwc.setOpenMode(OpenMode.CREATE_OR_APPEND);
        }

        // Optional: for better indexing performance, if you
        // are indexing many documents, increase the RAM
        // buffer.  But if you do this, increase the max heap
        // size to the JVM (eg add -Xmx512m or -Xmx1g):
        //
        // iwc.setRAMBufferSizeMB(256.0);

        IndexWriter writer = new IndexWriter(dir, iwc);
        indexDocs(writer, docDir);

        // NOTE: if you want to maximize search performance,
        // you can optionally call forceMerge here.  This can be
        // a terribly costly operation, so generally it's only
        // worth it when your index is relatively static (ie
        // you're done adding documents to it):
        //
        // writer.forceMerge(1);

        writer.close();

        Date end = new Date();
        System.out.println(end.getTime() - start.getTime() + " total milliseconds");

    } catch (IOException e) {
        System.out.println(" caught a " + e.getClass() + "\n with message: " + e.getMessage());
    }
}