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.semantic.util.suggest.NewSuggestionHints.java

public void setIndexReader(IndexReader indexReader) {
    this.indexReader = indexReader;
    /* only build 1 time */
    if (suggester == null) {
        Dictionary directory = new MultiFieldTermLuceneDirectory(indexReader, fields);
        try {/*from ww w  .ja  v a 2s . c o  m*/
            suggester = new AnalyzingInfixSuggester(
                    //                        new File("./suggestidx"),
                    new RAMDirectory(), new StandardAnalyzer());
            suggester.build(directory);
        } catch (Throwable ex) {
            ex.printStackTrace();
        }
    }
}

From source file:com.sensei.indexing.hadoop.keyvalueformat.IntermediateForm.java

License:Apache License

/**
 * Constructor
 * @throws IOException
 */
public IntermediateForm() throws IOException {
    dir = new RAMDirectory();
    writer = null;
    numDocs = 0;
}

From source file:com.sensei.indexing.hadoop.keyvalueformat.IntermediateForm.java

License:Apache License

private void resetForm() throws IOException {
    if (dir.sizeInBytes() > 0) {
        // it's ok if we don't close a ram directory
        dir.close();//from   www  .  j a v a 2  s . com
        // an alternative is to delete all the files and reuse the ram directory
        dir = new RAMDirectory();
    }
    assert (writer == null);
    numDocs = 0;
}

From source file:com.senseidb.search.node.inmemory.InMemorySenseiService.java

License:Apache License

@SuppressWarnings("unchecked")
public SenseiResult doQuery(SenseiRequest senseiRequest, List<JSONObject> documents) {
    Directory directory = null;/*www  . j a  v a  2s .  c o  m*/
    IndexWriter writer = null;
    try {
        directory = new RAMDirectory();
        writer = new IndexWriter(directory,
                new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35)));
        addDocuments(directory, writer, documents);
        ZoieIndexReader<BoboIndexReader> zoieMultiReader = new ZoieMultiReader<BoboIndexReader>(
                IndexReader.open(directory), new SenseiIndexReaderDecorator(facets, runtimeFacets));
        MockIndexReaderFactory mockIndexReaderFactory = new MockIndexReaderFactory<ZoieIndexReader<BoboIndexReader>>(
                Arrays.asList(zoieMultiReader));
        mockSenseiCore.setIndexReaderFactory(mockIndexReaderFactory);
        SenseiResult result = coreSenseiServiceImpl.execute(senseiRequest);
        mockSenseiCore.setIndexReaderFactory(null);
        return result;
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        try {
            if (writer != null) {
                writer.close();
            }
            if (directory != null) {
                directory.close();
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

From source file:com.shaie.annots.AnnotationSearchExample.java

License:Apache License

public static void main(String[] args) throws Exception {
    Directory dir = new RAMDirectory();
    IndexWriterConfig conf = new IndexWriterConfig(new WhitespaceAnalyzer());
    IndexWriter writer = new IndexWriter(dir, conf);

    // we need to add the annotation as a TokenStream field, therefore cannot use an Analyzer passed in the
    // IndexWriterConfig.
    Tokenizer tokenizer = new WhitespaceTokenizer();
    tokenizer.setReader(new StringReader("quick brown fox ate the blue red chicken"));
    TeeSinkTokenFilter textStream = new TeeSinkTokenFilter(tokenizer);
    TokenStream colorAnnotationStream = new AnnotatingTokenFilter(
            textStream.newSinkTokenStream(new ColorsSinkFilter()), COLOR_ANNOT_TERM);

    Document doc = new Document();
    doc.add(new TextField("text", textStream));
    doc.add(new TextField("annot", colorAnnotationStream));
    writer.addDocument(doc);/*www .ja  v  a2  s  .c om*/

    writer.close();

    DirectoryReader reader = DirectoryReader.open(dir);
    LeafReader ar = reader.leaves().get(0).reader(); // we only have one segment
    printFieldTerms(ar, "text");
    System.out.println();

    final ByteArrayDataInput in = new ByteArrayDataInput();
    PostingsEnum dape = ar.postings(new Term("annot", COLOR_ANNOT_TERM));
    int docID = dape.nextDoc();
    int freq = dape.freq();
    System.out.println("Color annotation spans: doc=" + docID + ", freq=" + freq);
    for (int i = 0; i < freq; i++) {
        dape.nextPosition();
        BytesRef payload = dape.getPayload();
        in.reset(payload.bytes, payload.offset, payload.length);
        System.out.println("  start=" + in.readVInt() + ", length=" + in.readVInt());
    }

    IndexSearcher searcher = new IndexSearcher(reader);

    System.out.println("\nsearching for 'red WITHIN color':");
    Query q = new SpanWithinQuery(new SpanAnnotationTermQuery(new Term("annot", COLOR_ANNOT_TERM)),
            new SpanInclusivePositionTermQuery(new Term("text", "red")));
    TopDocs td = searcher.search(q, 10);
    System.out.println("  num results: " + td.scoreDocs.length);

    System.out.println("\nsearching for 'ate WITHIN color':");
    q = new SpanWithinQuery(new SpanAnnotationTermQuery(new Term("annot", COLOR_ANNOT_TERM)),
            new SpanInclusivePositionTermQuery(new Term("text", "ate")));
    td = searcher.search(q, 10);
    System.out.println("  num results: " + td.scoreDocs.length);

    reader.close();
    dir.close();
}

From source file:com.shaie.annots.example.AnnotatorAnyExample.java

License:Apache License

@SuppressWarnings("resource")
public static void main(String[] args) throws Exception {
    final Directory dir = new RAMDirectory();
    final Analyzer analyzer = new WhitespaceAnalyzer();
    final IndexWriterConfig conf = new IndexWriterConfig(analyzer);
    final IndexWriter writer = new IndexWriter(dir, conf);

    addDocument(writer, "brown fox and a red dog");
    addDocument(writer, "only red dog");
    addDocument(writer, "no red animals here");
    writer.close();/*  w  w  w .j av a 2 s .c  o  m*/

    final QueryParser qp = new QueryParser(TEXT_FIELD, analyzer);
    qp.setAllowLeadingWildcard(true);

    final DirectoryReader reader = DirectoryReader.open(dir);
    final LeafReader leaf = reader.leaves().get(0).reader(); // We only have one segment
    IndexUtils.printFieldTerms(leaf, TEXT_FIELD, COLOR_FIELD, ANIMAL_FIELD);
    IndexUtils.printFieldTermsWithInfo(leaf, COLOR_FIELD, ANIMAL_FIELD);
    System.out.println();

    final IndexSearcher searcher = new IndexSearcher(reader);

    search(searcher, qp.parse("animal:" + AnyAnnotationTokenFilter.ANY_ANNOTATION_TERM + " AND color:"
            + AnyAnnotationTokenFilter.ANY_ANNOTATION_TERM));
    System.out.println();

    search(searcher, qp.parse("animal:" + AnyAnnotationTokenFilter.ANY_ANNOTATION_TERM + " AND color:red"));
    System.out.println();

    searchForRedAnimal(searcher);
    System.out.println();

    reader.close();
}

From source file:com.shaie.annots.example.AnnotatorTeeSinkFilterExample.java

License:Apache License

@SuppressWarnings("resource")
public static void main(String[] args) throws Exception {
    final Directory dir = new RAMDirectory();
    final Analyzer analyzer = new WhitespaceAnalyzer();
    final IndexWriterConfig conf = new IndexWriterConfig(analyzer);
    final IndexWriter writer = new IndexWriter(dir, conf);

    addDocument(writer, "brown fox and a red dog");
    addDocument(writer, "only red dog");
    addDocument(writer, "no red animals here");
    writer.close();/*from ww w.  ja va2s.c  o  m*/

    final QueryParser qp = new QueryParser(TEXT_FIELD, analyzer);
    qp.setAllowLeadingWildcard(true);

    final DirectoryReader reader = DirectoryReader.open(dir);
    final LeafReader leaf = reader.leaves().get(0).reader(); // We only have one segment
    IndexUtils.printFieldTerms(leaf, TEXT_FIELD, COLOR_FIELD, ANIMAL_FIELD);
    IndexUtils.printFieldTermsWithInfo(leaf, COLOR_FIELD, ANIMAL_FIELD);

    final IndexSearcher searcher = new IndexSearcher(reader);

    search(searcher, qp.parse("color:red"));
    System.out.println();

    search(searcher, qp.parse("animal:fox"));
    System.out.println();

    searchForBrownFox(searcher);
    System.out.println();

    search(searcher, qp.parse("animal:* AND color:*"));
    System.out.println();

    search(searcher, qp.parse("animal:* AND color:red"));
    System.out.println();

    reader.close();
}

From source file:com.shaie.annots.example.AnnotatorTokenFilterExample.java

License:Apache License

@SuppressWarnings("resource")
public static void main(String[] args) throws Exception {
    final Directory dir = new RAMDirectory();
    final Analyzer analyzer = createAnalyzer();
    final IndexWriterConfig conf = new IndexWriterConfig(analyzer);
    final IndexWriter writer = new IndexWriter(dir, conf);

    addDocument(writer, "brown fox and a red dog");
    addDocument(writer, "only red dog");
    addDocument(writer, "no red animals here");
    writer.close();// www . ja va 2 s  .co  m

    final QueryParser qp = new QueryParser(TEXT_FIELD, analyzer);
    qp.setAllowLeadingWildcard(true);

    final DirectoryReader reader = DirectoryReader.open(dir);
    final LeafReader leaf = reader.leaves().get(0).reader(); // We only have one segment
    IndexUtils.printFieldTerms(leaf, TEXT_FIELD, COLOR_FIELD, ANIMAL_FIELD);
    IndexUtils.printFieldTermsWithInfo(leaf, COLOR_FIELD, ANIMAL_FIELD);

    final IndexSearcher searcher = new IndexSearcher(reader);

    search(searcher, qp.parse("color:red"));
    System.out.println();

    search(searcher, qp.parse("animal:fox"));
    System.out.println();

    searchForBrownFox(searcher);
    System.out.println();

    search(searcher, qp.parse("animal:* AND color:*"));
    System.out.println();

    search(searcher, qp.parse("animal:* AND color:red"));
    System.out.println();

    reader.close();
}

From source file:com.shaie.annots.example.PreAnnotatedTokenFilterExample.java

License:Apache License

@SuppressWarnings("resource")
public static void main(String[] args) throws Exception {
    final Directory dir = new RAMDirectory();
    final Analyzer analyzer = new WhitespaceAnalyzer();
    final IndexWriterConfig conf = new IndexWriterConfig(analyzer);
    final IndexWriter writer = new IndexWriter(dir, conf);

    addDocument(writer, "quick rosy brown fox and a pale violet red dog", 1, 2, 2, 1, 6, 3, 7, 1, 8, 1);
    addDocument(writer, "only red dog", 1, 1);
    addDocument(writer, "man with red pale face", 2, 1);
    writer.close();/*from   ww  w  . j ava2s. c o m*/

    final QueryParser qp = new QueryParser(TEXT_FIELD, analyzer);
    qp.setAllowLeadingWildcard(true);

    final DirectoryReader reader = DirectoryReader.open(dir);
    final LeafReader leaf = reader.leaves().get(0).reader(); // We only have one segment
    IndexUtils.printFieldTerms(leaf, TEXT_FIELD, COLOR_FIELD);
    IndexUtils.printFieldTermsWithInfo(leaf, COLOR_FIELD);
    System.out.println();

    final IndexSearcher searcher = new IndexSearcher(reader);

    search(searcher, qp.parse("color:" + ANY_ANNOTATION_TERM));
    System.out.println();

    search(searcher, qp.parse("color:pale"));
    System.out.println();

    searchForColoredFox(searcher);
    System.out.println();

    reader.close();
}

From source file:com.shaie.PhraseVsSpanQuery.java

License:Apache License

@SuppressWarnings("resource")
public static void main(String[] args) throws Exception {
    final Directory dir = new RAMDirectory();
    final IndexWriterConfig conf = new IndexWriterConfig(new WhitespaceAnalyzer());
    final IndexWriter writer = new IndexWriter(dir, conf);

    final Document doc = new Document();
    doc.add(new TextField("f", new TokenStream() {
        final PositionIncrementAttribute pos = addAttribute(PositionIncrementAttribute.class);
        final CharTermAttribute term = addAttribute(CharTermAttribute.class);
        boolean first = true, done = false;

        @Override//from w w  w  .ja  v a  2s  .  c o  m
        public boolean incrementToken() throws IOException {
            if (done) {
                return false;
            }
            if (first) {
                term.setEmpty().append("a");
                pos.setPositionIncrement(1);
                first = false;
            } else {
                term.setEmpty().append("b");
                pos.setPositionIncrement(0);
                done = true;
            }
            return true;
        }
    }));
    writer.addDocument(doc);
    writer.close();

    final DirectoryReader reader = DirectoryReader.open(dir);
    final IndexSearcher searcher = new IndexSearcher(reader);
    final LeafReader ar = reader.leaves().get(0).reader();
    final TermsEnum te = ar.terms("f").iterator();
    BytesRef scratch = new BytesRef();
    while ((scratch = te.next()) != null) {
        System.out.println(scratch.utf8ToString());
        final PostingsEnum dape = ar.postings(new Term("f", scratch.utf8ToString()));
        System.out.println("  doc=" + dape.nextDoc() + ", pos=" + dape.nextPosition());
    }

    System.out.println();

    // try a phrase query with a slop
    final PhraseQuery pqNoSlop = buildPhraseQuery(0);
    System.out.println("searching for \"a b\"; num results = " + searcher.search(pqNoSlop, 10).totalHits);

    final PhraseQuery pqSlop1 = buildPhraseQuery(1);
    System.out.println("searching for \"a b\"~1; num results = " + searcher.search(pqSlop1, 10).totalHits);

    final PhraseQuery pqSlop3 = buildPhraseQuery(3);
    System.out.println("searching for \"a b\"~3; num results = " + searcher.search(pqSlop3, 10).totalHits);

    final SpanNearQuery snqUnOrdered = new SpanNearQuery(
            new SpanQuery[] { new SpanTermQuery(new Term("f", "a")), new SpanTermQuery(new Term("f", "b")) }, 1,
            false);
    System.out.println("searching for SpanNearUnordered('a', 'b'), slop=1; num results = "
            + searcher.search(snqUnOrdered, 10).totalHits);

    final SpanNearQuery snqOrdered = new SpanNearQuery(
            new SpanQuery[] { new SpanTermQuery(new Term("f", "a")), new SpanTermQuery(new Term("f", "b")) }, 1,
            true);
    System.out.println("searching for SpanNearOrdered('a', 'b'), slop=1; num results = "
            + searcher.search(snqOrdered, 10).totalHits);

    reader.close();
}