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:aos.lucene.search.msc.PhraseQueryTest.java

License:Apache License

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

    searcher = new IndexSearcher(dir);
}

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

License:Apache License

public void setUp() throws Exception {
    directory = new RAMDirectory();
}

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

License:Apache License

@Override
public void setUp() throws Exception {

    directory = new RAMDirectory();

    IndexWriter writer = new IndexWriter(directory, new WhitespaceAnalyzer(Version.LUCENE_46),
            IndexWriter.MaxFieldLength.UNLIMITED);

    Calendar cal = Calendar.getInstance();
    cal.set(2009, 1, 1, 0, 0);//from w  w  w  .ja  v  a 2  s.  co  m

    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);

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

    writer.close();

    searcher = new IndexSearcher(directory);

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

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

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

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

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

License:Apache License

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

From source file:arena.lucene.LuceneDirectoryBean.java

License:Open Source License

public void afterPropertiesSet() throws Exception {
    boolean initialize = false;
    if (this.location == null) {
        this.directory = new RAMDirectory();
        initialize = true;//  w  ww  . j  ava  2  s. c  o m
    } else {
        File dir = new File(this.servletContext != null ? this.servletContext.getRealPath(location) : location);
        if (!dir.isDirectory()) {
            dir.mkdirs();
        }
        LockFactory lf = (this.disableLocks ? NoLockFactory.getNoLockFactory() : null);
        this.directory = FSDirectory.open(dir, lf);
        log.info("Using lucene directory: file=" + dir + " instance=" + this.directory.toString());
        initialize = (this.initializeIndexIfNew && dir.listFiles().length == 0);
    }

    if (initialize) {
        // create a dummy writer to initialize the index
        IndexWriter writer = null;
        try {
            writer = new IndexWriter(this.directory, new StandardAnalyzer(Version.LUCENE_30), true,
                    MaxFieldLength.LIMITED);
        } finally {
            if (writer != null) {
                writer.close();
            }
        }
    }
}

From source file:at.ac.univie.mminf.luceneSKOS.AbstractTermExpansionTest.java

License:Apache License

/**
 * This test indexes a sample metadata record (=lucene document) having a
 * "title", "description", and "subject" field, which contains plain subject
 * terms.//from   w  ww  .ja  v  a 2  s.com
 * 
 * A search for "arms" doesn't return that record because the term "arms" is
 * not explicitly contained in the record (document).
 * 
 * @throws IOException
 * @throws LockObtainFailedException
 * @throws CorruptIndexException
 */
@Test
public void noExpansion() throws CorruptIndexException, LockObtainFailedException, IOException {

    /* defining the document to be indexed */
    Document doc = new Document();
    doc.add(new Field("title", "Spearhead", Field.Store.YES, Field.Index.ANALYZED));
    doc.add(new Field("description",
            "Roman iron spearhead. The spearhead was attached to one end of a wooden shaft..."
                    + "The spear was mainly a thrusting weapon, but could also be thrown. "
                    + "It was the principal weapon of the auxiliary soldier... "
                    + "(second - fourth century, Arbeia Roman Fort).",
            Field.Store.NO, Field.Index.ANALYZED));
    doc.add(new Field("subject", "weapons", Field.Store.NO, Field.Index.ANALYZED));

    /* setting up a writer with a default (simple) analyzer */
    writer = new IndexWriter(new RAMDirectory(),
            new IndexWriterConfig(Version.LUCENE_36, new SimpleAnalyzer(Version.LUCENE_36)));

    /* adding the document to the index */
    writer.addDocument(doc);

    /* defining a query that searches over all fields */
    BooleanQuery query = new BooleanQuery();
    query.add(new TermQuery(new Term("title", "arms")), BooleanClause.Occur.SHOULD);
    query.add(new TermQuery(new Term("description", "arms")), BooleanClause.Occur.SHOULD);
    query.add(new TermQuery(new Term("subject", "arms")), BooleanClause.Occur.SHOULD);

    /* creating a new searcher */
    searcher = new IndexSearcher(IndexReader.open(writer, false));

    TopDocs results = searcher.search(query, 10);

    /* no results are returned since there is no term match */
    Assert.assertEquals(0, results.totalHits);

}

From source file:at.ac.univie.mminf.luceneSKOS.analysis.AbstractFilterTest.java

License:Apache License

@Before
protected void setUp() throws Exception {

    // adding some test data
    skosEngine = new SKOSEngineMock();

    skosEngine.addEntry("http://example.com/concept/1", SKOSType.PREF, "jumps");
    skosEngine.addEntry("http://example.com/concept/1", SKOSType.ALT, "leaps", "hops");

    skosEngine.addEntry("http://example.com/concept/2", SKOSType.PREF, "quick");
    skosEngine.addEntry("http://example.com/concept/2", SKOSType.ALT, "fast", "speedy");

    skosEngine.addEntry("http://example.com/concept/3", SKOSType.PREF, "over");
    skosEngine.addEntry("http://example.com/concept/3", SKOSType.ALT, "above");

    skosEngine.addEntry("http://example.com/concept/4", SKOSType.PREF, "lazy");
    skosEngine.addEntry("http://example.com/concept/4", SKOSType.ALT, "apathic", "sluggish");

    skosEngine.addEntry("http://example.com/concept/5", SKOSType.PREF, "dog");
    skosEngine.addEntry("http://example.com/concept/5", SKOSType.ALT, "canine", "pooch");

    skosEngine.addEntry("http://example.com/concept/6", SKOSType.PREF, "united nations");
    skosEngine.addEntry("http://example.com/concept/6", SKOSType.ALT, "UN");

    skosEngine.addEntry("http://example.com/concept/7", SKOSType.PREF, "lazy dog");
    skosEngine.addEntry("http://example.com/concept/7", SKOSType.ALT, "Odie");

    directory = new RAMDirectory();

}

From source file:at.ac.univie.mminf.luceneSKOS.analysis.engine.jena.SKOSEngineImpl.java

License:Apache License

/**
 * This constructor loads the SKOS model from a given InputStream using the
 * given serialization language parameter, which must be either N3, RDF/XML,
 * or TURTLE./*from   w w w  .j a v a2s.c  o m*/
 *
 * @param inputStream the input stream
 * @param lang the serialization language
 * @throws IOException if the model cannot be loaded
 */
public SKOSEngineImpl(InputStream inputStream, String lang) throws IOException {
    if (!("N3".equals(lang) || "RDF/XML".equals(lang) || "TURTLE".equals(lang))) {
        throw new IOException("Invalid RDF serialization format");
    }
    this.analyzer = new SimpleAnalyzer();
    this.skosModel = ModelFactory.createDefaultModel();
    skosModel.read(inputStream, null, lang);
    indexDir = new RAMDirectory();
    entailSKOSModel();
    indexSKOSModel();
    searcher = new IndexSearcher(DirectoryReader.open(indexDir));
}

From source file:at.ac.univie.mminf.luceneSKOS.analysis.engine.jena.SKOSEngineImpl.java

License:Apache License

/**
 * This constructor loads the SKOS model from a given InputStream using the
 * given serialization language parameter, which must be either N3, RDF/XML,
 * or TURTLE.//w w w. ja  va2  s . com
 *
 * @param inputStream the input stream
 * @param format the serialization language
 * @param languages the languages
 * @throws IOException if the model cannot be loaded
 */
public SKOSEngineImpl(InputStream inputStream, String format, List<String> languages) throws IOException {
    if (!("N3".equals(format) || "RDF/XML".equals(format) || "TURTLE".equals(format))) {
        throw new IOException("Invalid RDF serialization format");
    }
    if (languages != null) {
        this.languages = new TreeSet<>(languages);
    }
    analyzer = new SimpleAnalyzer();
    skosModel = ModelFactory.createDefaultModel();
    skosModel.read(inputStream, null, format);
    indexDir = new RAMDirectory();
    entailSKOSModel();
    indexSKOSModel();
    searcher = new IndexSearcher(DirectoryReader.open(indexDir));
}

From source file:at.ac.univie.mminf.luceneSKOS.LabelbasedTermExpansionTest.java

License:Apache License

/**
 * This test indexes a sample metadata record (=lucene document) having a
 * "title", "description", and "subject" field.
 * /*from   www  .ja v  a2  s. c  o  m*/
 * A search for "arms" returns that record as a result because "arms" is
 * defined as an alternative label for "weapons", the term which is contained
 * in the subject field.
 * 
 * @throws IOException
 */
@Test
public void labelBasedTermExpansion() throws IOException {

    /* defining the document to be indexed */
    Document doc = new Document();
    doc.add(new Field("title", "Spearhead", Field.Store.YES, Field.Index.ANALYZED));
    doc.add(new Field("description",
            "Roman iron spearhead. The spearhead was attached to one end of a wooden shaft..."
                    + "The spear was mainly a thrusting weapon, but could also be thrown. "
                    + "It was the principal weapon of the auxiliary soldier... "
                    + "(second - fourth century, Arbeia Roman Fort).",
            Field.Store.NO, Field.Index.ANALYZED));
    doc.add(new Field("subject", "weapons", Field.Store.NO, Field.Index.ANALYZED));

    /* setting up the SKOS analyzer */
    String skosFile = "src/test/resources/skos_samples/ukat_examples.n3";

    /* ExpansionType.URI->the field to be analyzed (expanded) contains URIs */
    Analyzer skosAnalyzer = new SKOSAnalyzer(matchVersion, skosFile, ExpansionType.LABEL);

    /* Define different analyzers for different fields */
    Map<String, Analyzer> analyzerPerField = new HashMap<String, Analyzer>();
    analyzerPerField.put("subject", skosAnalyzer);
    PerFieldAnalyzerWrapper indexAnalyzer = new PerFieldAnalyzerWrapper(new SimpleAnalyzer(matchVersion),
            analyzerPerField);

    /* setting up a writer with a default (simple) analyzer */
    writer = new IndexWriter(new RAMDirectory(), new IndexWriterConfig(matchVersion, indexAnalyzer));

    /* adding the document to the index */
    writer.addDocument(doc);

    /* defining a query that searches over all fields */
    BooleanQuery query1 = new BooleanQuery();
    query1.add(new TermQuery(new Term("title", "arms")), BooleanClause.Occur.SHOULD);
    query1.add(new TermQuery(new Term("description", "arms")), BooleanClause.Occur.SHOULD);
    query1.add(new TermQuery(new Term("subject", "arms")), BooleanClause.Occur.SHOULD);

    /* creating a new searcher */
    searcher = new IndexSearcher(IndexReader.open(writer, false));

    TopDocs results = searcher.search(query1, 10);

    /* the document matches because "arms" is among the expanded terms */
    Assert.assertEquals(1, results.totalHits);

    /* defining a query that searches for a broader concept */
    Query query2 = new TermQuery(new Term("subject", "military equipment"));

    results = searcher.search(query2, 10);

    /* ... also returns the document as result */
    Assert.assertEquals(1, results.totalHits);

}