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.querydsl.lucene3.LuceneSerializerNotTokenizedTest.java

License:Apache License

@Before
public void Before() throws Exception {
    serializer = new LuceneSerializer(false, false);
    idx = new RAMDirectory();
    IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_31, new StandardAnalyzer(Version.LUCENE_30))
            .setOpenMode(IndexWriterConfig.OpenMode.CREATE);
    writer = new IndexWriter(idx, config);

    writer.addDocument(createDocument(clooney));
    writer.addDocument(createDocument(pitt));

    Document document = new Document();
    for (String movie : Arrays.asList("Interview with the Vampire", "Up in the Air")) {
        document.add(new Field("movie", movie, Store.YES, Index.NOT_ANALYZED));
    }//from   w ww. ja va 2s.  co m
    writer.addDocument(document);

    writer.close();

    IndexReader reader = IndexReader.open(idx);
    searcher = new IndexSearcher(reader);
}

From source file:com.querydsl.lucene3.LuceneSerializerTest.java

License:Apache License

@Before
public void setUp() throws Exception {
    serializer = new LuceneSerializer(true, true);
    entityPath = new PathBuilder<Object>(Object.class, "obj");
    title = entityPath.getString("title");
    author = entityPath.getString("author");
    text = entityPath.getString("text");
    publisher = entityPath.getString("publisher");
    year = entityPath.getNumber("year", Integer.class);
    rating = entityPath.getString("rating");
    gross = entityPath.getNumber("gross", Double.class);
    titles = entityPath.getCollection("title", String.class, StringPath.class);

    longField = entityPath.getNumber("longField", Long.class);
    shortField = entityPath.getNumber("shortField", Short.class);
    byteField = entityPath.getNumber("byteField", Byte.class);
    floatField = entityPath.getNumber("floatField", Float.class);

    idx = new RAMDirectory();
    config = new IndexWriterConfig(Version.LUCENE_31, new StandardAnalyzer(Version.LUCENE_30))
            .setOpenMode(IndexWriterConfig.OpenMode.CREATE);
    writer = new IndexWriter(idx, config);

    writer.addDocument(createDocument());

    writer.close();/*  w  w w . j  a  v  a  2  s . c o m*/

    IndexReader reader = IndexReader.open(idx);
    searcher = new IndexSearcher(reader);
}

From source file:com.querydsl.lucene4.LuceneSerializerTest.java

License:Apache License

@Before
public void setUp() throws Exception {
    serializer = new LuceneSerializer(true, true);
    entityPath = new PathBuilder<Object>(Object.class, "obj");
    title = entityPath.getString("title");
    author = entityPath.getString("author");
    text = entityPath.getString("text");
    publisher = entityPath.getString("publisher");
    year = entityPath.getNumber("year", Integer.class);
    rating = entityPath.getString("rating");
    gross = entityPath.getNumber("gross", Double.class);
    titles = entityPath.getCollection("title", String.class, StringPath.class);

    longField = entityPath.getNumber("longField", Long.class);
    shortField = entityPath.getNumber("shortField", Short.class);
    byteField = entityPath.getNumber("byteField", Byte.class);
    floatField = entityPath.getNumber("floatField", Float.class);

    idx = new RAMDirectory();
    config = new IndexWriterConfig(Version.LUCENE_42, new StandardAnalyzer(Version.LUCENE_42))
            .setOpenMode(IndexWriterConfig.OpenMode.CREATE);
    writer = new IndexWriter(idx, config);

    writer.addDocument(createDocument());

    writer.close();/*  w ww.  j  av a 2  s .  c o  m*/

    IndexReader reader = IndexReader.open(idx);
    searcher = new IndexSearcher(reader);
}

From source file:com.querydsl.lucene5.LuceneQueryTest.java

License:Apache License

@Before
public void setUp() throws Exception {
    final QDocument entityPath = new QDocument("doc");
    title = entityPath.title;/*  w ww. j av  a2s  .  com*/
    year = entityPath.year;
    gross = entityPath.gross;

    idx = new RAMDirectory();
    writer = createWriter(idx);

    writer.addDocument(createDocument("Jurassic Park", "Michael Crichton", "It's a UNIX system! I know this!",
            1990, 90.00));
    writer.addDocument(
            createDocument("Nummisuutarit", "Aleksis Kivi", "ESKO. Ja iloitset ja riemuitset?", 1864, 10.00));
    writer.addDocument(createDocument("The Lord of the Rings", "John R. R. Tolkien",
            "One Ring to rule them all, One Ring to find them, One Ring to bring them all and in the darkness bind them",
            1954, 89.00));
    writer.addDocument(createDocument("Introduction to Algorithms",
            "Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein", "Bubble sort", 1990,
            30.50));

    writer.close();

    IndexReader reader = DirectoryReader.open(idx);
    searcher = new IndexSearcher(reader);
    query = new LuceneQuery(new LuceneSerializer(true, true), searcher);
}

From source file:com.querydsl.lucene5.LuceneQueryTest.java

License:Apache License

@Test
public void Empty_Index_Should_Return_Empty_List() throws Exception {
    idx = new RAMDirectory();

    writer = createWriter(idx);/*from  w  w w.  jav a  2s  .  c  om*/
    writer.close();
    IndexReader reader = DirectoryReader.open(idx);
    searcher = new IndexSearcher(reader);
    query = new LuceneQuery(new LuceneSerializer(true, true), searcher);
    assertTrue(query.fetch().isEmpty());
}

From source file:com.querydsl.lucene5.LuceneSerializerNotTokenizedTest.java

License:Apache License

@Before
public void Before() throws Exception {
    serializer = new LuceneSerializer(false, false);
    idx = new RAMDirectory();
    IndexWriterConfig config = new IndexWriterConfig(new StandardAnalyzer())
            .setOpenMode(IndexWriterConfig.OpenMode.CREATE);
    writer = new IndexWriter(idx, config);

    writer.addDocument(createDocument(clooney));
    writer.addDocument(createDocument(pitt));

    Document document = new Document();
    for (String movie : Arrays.asList("Interview with the Vampire", "Up in the Air")) {
        document.add(new Field("movie", movie, Store.YES, Index.NOT_ANALYZED));
    }//  ww  w .ja  va 2  s  . c  o m
    writer.addDocument(document);

    writer.close();

    IndexReader reader = DirectoryReader.open(idx);
    searcher = new IndexSearcher(reader);
}

From source file:com.querydsl.lucene5.LuceneSerializerTest.java

License:Apache License

@Before
public void setUp() throws Exception {
    serializer = new LuceneSerializer(true, true);
    entityPath = new PathBuilder<Object>(Object.class, "obj");
    title = entityPath.getString("title");
    author = entityPath.getString("author");
    text = entityPath.getString("text");
    publisher = entityPath.getString("publisher");
    year = entityPath.getNumber("year", Integer.class);
    rating = entityPath.getString("rating");
    gross = entityPath.getNumber("gross", Double.class);
    titles = entityPath.getCollection("title", String.class, StringPath.class);

    longField = entityPath.getNumber("longField", Long.class);
    shortField = entityPath.getNumber("shortField", Short.class);
    byteField = entityPath.getNumber("byteField", Byte.class);
    floatField = entityPath.getNumber("floatField", Float.class);

    idx = new RAMDirectory();
    config = new IndexWriterConfig(new StandardAnalyzer()).setOpenMode(IndexWriterConfig.OpenMode.CREATE);
    writer = new IndexWriter(idx, config);

    writer.addDocument(createDocument());

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

    IndexReader reader = DirectoryReader.open(idx);
    searcher = new IndexSearcher(reader);
}

From source file:com.redhat.satellite.search.index.ngram.tests.NGramTestSetup.java

License:Open Source License

/**
 * Creates an index in RAM//  w  ww.j  a va  2 s .c  o  m
 * */
public void setUp() throws Exception {
    super.setUp();
    initItems();
    this.stanDir = new RAMDirectory();
    IndexWriter stanWriter = new IndexWriter(this.stanDir, new StandardAnalyzer(), true);

    this.ngramDir = new RAMDirectory();
    IndexWriter ngramWriter = new IndexWriter(this.ngramDir, new NGramAnalyzer(min_ngram, max_ngram), true);

    for (Map<String, String> item : items) {
        String name = item.get("name");
        String descp = item.get("description");
        Document doc = new Document();
        doc.add(new Field("name", name, Field.Store.YES, Field.Index.TOKENIZED));
        doc.add(new Field("description", descp, Field.Store.YES, Field.Index.TOKENIZED));
        stanWriter.addDocument(doc);
        ngramWriter.addDocument(doc);
    }
    stanWriter.close();
    ngramWriter.close();
}

From source file:com.ricky.codelab.lucene.LuceneIndexAndSearchDemo.java

License:Apache License

/**
 * //from  ww w.j av  a2 s  . c  o m
 * ???
 * @param args
 */
public static void main(String[] args) {
    //Lucene Document??
    String fieldName = "text";
    //
    String text = "IK Analyzer???????";

    //IKAnalyzer?
    Analyzer analyzer = new IKAnalyzer(true);

    Directory directory = null;
    IndexWriter iwriter = null;
    IndexReader ireader = null;
    IndexSearcher isearcher = null;
    try {
        //
        directory = new RAMDirectory();

        //?IndexWriterConfig
        IndexWriterConfig iwConfig = new IndexWriterConfig(analyzer);
        iwConfig.setOpenMode(OpenMode.CREATE_OR_APPEND);
        iwriter = new IndexWriter(directory, iwConfig);
        //
        Document doc = new Document();
        doc.add(new StringField("ID", "10000", Field.Store.YES));
        doc.add(new TextField(fieldName, text, Field.Store.YES));
        iwriter.addDocument(doc);
        iwriter.close();

        //?**********************************
        //?   
        ireader = DirectoryReader.open(directory);
        isearcher = new IndexSearcher(ireader);

        String keyword = "?";
        //QueryParser?Query
        QueryParser qp = new QueryParser(fieldName, analyzer);
        qp.setDefaultOperator(QueryParser.AND_OPERATOR);
        Query query = qp.parse(keyword);
        System.out.println("Query = " + query);

        //?5?
        TopDocs topDocs = isearcher.search(query, 5);
        System.out.println("" + topDocs.totalHits);
        //
        ScoreDoc[] scoreDocs = topDocs.scoreDocs;
        for (int i = 0; i < topDocs.totalHits; i++) {
            Document targetDoc = isearcher.doc(scoreDocs[i].doc);
            System.out.println("" + targetDoc.toString());
        }

    } catch (CorruptIndexException e) {
        e.printStackTrace();
    } catch (LockObtainFailedException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ParseException e) {
        e.printStackTrace();
    } finally {
        if (ireader != null) {
            try {
                ireader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (directory != null) {
            try {
                directory.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

From source file:com.scaleunlimited.classify.analyzer.LuceneAnalyzer.java

License:Apache License

private synchronized void init() {
    if (_ramDir == null) {
        _ramDir = new RAMDirectory();
    }/*from  ww  w  .ja v  a 2 s .com*/

    if (_analyzer == null) {
        _analyzer = createAnalyzer();
    }
}