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:GUIFrame.java

public Directory InitializeIndex(Directory Index, PorterStemAnalyzer Analyzer) throws IOException {
    // Create the index
    Index = new RAMDirectory();
    IndexWriterConfig myConfig = new IndexWriterConfig(Version.LATEST, Analyzer);
    try (IndexWriter indoWriter = new IndexWriter(Index, myConfig)) {
        BufferedReader buffR = new BufferedReader(new FileReader("npl\\doc-text"));
        String currLine;/*from   www . j a va 2 s.c o m*/
        StringBuilder idDoc = new StringBuilder();
        StringBuilder sumDoc = new StringBuilder();
        currLine = buffR.readLine();

        while (currLine != null) {
            if (currLine.matches(".*\\d+.*")) { // Check if current line contains a document's id
                // Capture the document's id and summary
                idDoc.append(currLine);
                currLine = buffR.readLine();

                // While the reader holds line of summary
                while (!(currLine.contains("/"))) {
                    sumDoc.append(currLine).append(" ");
                    currLine = buffR.readLine();
                }

                // Add the new document to the index
                AddDocument(indoWriter, idDoc.toString(), sumDoc.toString());

                //System.out.println(idDoc.toString());
                //System.out.println(sumDoc.toString());

                // Clear the string builders
                idDoc.delete(0, idDoc.length());
                sumDoc.delete(0, sumDoc.length());

                // Continue to next line
                currLine = buffR.readLine();
            } else {
                // Continue to next line
                currLine = buffR.readLine();
            }
        }

    }

    return Index;
}

From source file:MyServlet.java

public void init() throws ServletException {
    try {//from ww w .  j a va2  s  .  c  om
        //   Specify the analyzer for tokenizing text.
        //   The same analyzer should be used for indexing and searching
        analyzer = new StandardAnalyzer();
        //   Code to create the index
        index = new RAMDirectory();
        config = new IndexWriterConfig(analyzer);
        w = new IndexWriter(index, config);
        addDoc(w, " Software Engineering 2", "CMPE 133", "Mon.", "Computer Engineering");
        addDoc(w, " Software Engineering 1", "CMPE 131", "Mon.", "Computer Engineering");
        addDoc(w, " Object Oriented Design", "CS 151", "Mon.", "Computer Science");
        addDoc(w, " Advance Data Structures with Java ", "CS 146:", "Mon.", "Computer Science");
        addDoc(w, " System Security with Java", "CS 166:", "Mon.", "Computer Science");
        addDoc(w, "Liner math", "ME 123", "Mon.", "Math");
        w.close();
        log = new searchHistory();
        for (int i = 1; i <= 10; i++) {
            Student std = new Student();
            std.setUserName("std" + i);
            std.setPassword("123");
            stds.add(std);
            Teacher tch = new Teacher();
            tch.setUserName("tch" + i);
            tch.setPassword("123");
            tchs.add(tch);
        }
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
    System.out.println("init");

}

From source file:DocIndexer.java

License:Apache License

private RAMDirectory index() throws IOException, UnsupportedEncodingException, FileNotFoundException {
    RAMDirectory directory = new RAMDirectory();
    IndexWriterConfig config = new IndexWriterConfig(new StandardAnalyzer(CharArraySet.EMPTY_SET));
    config.setOpenMode(OpenMode.CREATE);
    config.setCommitOnClose(true);//from   ww w .  j a  va2 s. c o m
    try (IndexWriter iwriter = new IndexWriter(directory, config)) {
        for (String inputFile : inputFiles) {
            File file = new File(inputFile);
            if (file.length() == 0) {
                continue;
            }

            String title;
            try (BufferedReader titleReader = new BufferedReader(
                    new InputStreamReader(new FileInputStream(file), "UTF-8"))) {
                title = titleReader.readLine();
                if (title != null && title.startsWith("[[")) {
                    // Generally the first line of the txt is the title. In a few cases the
                    // first line is a "[[tag]]" and the second line is the title.
                    title = titleReader.readLine();
                }
            }
            Matcher matcher = SECTION_HEADER.matcher(title);
            if (matcher.matches()) {
                title = matcher.group(1);
            }

            String outputFile = AsciiDoctor.mapInFileToOutFile(inputFile, inExt, outExt);
            try (FileReader reader = new FileReader(file)) {
                Document doc = new Document();
                doc.add(new TextField(Constants.DOC_FIELD, reader));
                doc.add(new StringField(Constants.URL_FIELD, prefix + outputFile, Field.Store.YES));
                doc.add(new TextField(Constants.TITLE_FIELD, title, Field.Store.YES));
                iwriter.addDocument(doc);
            }
        }
    }
    return directory;
}

From source file:action.indexing.Fragments.java

License:Apache License

public void ramDirExample() throws Exception {
    Analyzer analyzer = new WhitespaceAnalyzer();
    // START//from  w w w.  j  a  va2  s.c  om
    Directory ramDir = new RAMDirectory();
    IndexWriter writer = new IndexWriter(ramDir, analyzer, IndexWriter.MaxFieldLength.UNLIMITED);
    // END
}

From source file:action.indexing.Fragments.java

License:Apache License

public void docBoostMethod() throws IOException {

    Directory dir = new RAMDirectory();
    IndexWriter writer = new IndexWriter(dir, new StandardAnalyzer(Version.LUCENE_30),
            IndexWriter.MaxFieldLength.UNLIMITED);

    // START/*  ww w  .j  ava2s. c  o  m*/
    Document doc = new Document();
    String senderEmail = getSenderEmail();
    String senderName = getSenderName();
    String subject = getSubject();
    String body = getBody();
    doc.add(new Field("senderEmail", senderEmail, Field.Store.YES, Field.Index.NOT_ANALYZED));
    doc.add(new Field("senderName", senderName, Field.Store.YES, Field.Index.ANALYZED));
    doc.add(new Field("subject", subject, Field.Store.YES, Field.Index.ANALYZED));
    doc.add(new Field("body", body, Field.Store.NO, Field.Index.ANALYZED));
    String lowerDomain = getSenderDomain().toLowerCase();
    if (isImportant(lowerDomain)) {
        doc.setBoost(1.5F); //1
    } else if (isUnimportant(lowerDomain)) {
        doc.setBoost(0.1F); //2 
    }
    writer.addDocument(doc);
    // END
    writer.close();

    /*
      #1 Good domain boost factor: 1.5
      #2 Bad domain boost factor: 0.1
    */
}

From source file:action.indexing.IndexingTest.java

License:Apache License

protected void setUp() throws Exception { //1
    directory = new RAMDirectory();

    IndexWriter writer = getWriter(); //2

    for (int i = 0; i < ids.length; i++) { //3
        Document doc = new Document();
        doc.add(new Field("id", ids[i], Field.Store.YES, Field.Index.NOT_ANALYZED));
        doc.add(new Field("country", unindexed[i], Field.Store.YES, Field.Index.NO));
        doc.add(new Field("contents", unstored[i], Field.Store.NO, Field.Index.ANALYZED));
        doc.add(new Field("city", text[i], Field.Store.YES, Field.Index.ANALYZED));
        writer.addDocument(doc);//from ww  w  .j  a  v  a  2s  .co  m
    }
    writer.close();
}

From source file:action.indexing.VerboseIndexing.java

License:Apache License

private void index() throws IOException {

    Directory dir = new RAMDirectory();

    IndexWriter writer = new IndexWriter(dir, new WhitespaceAnalyzer(), IndexWriter.MaxFieldLength.UNLIMITED);

    writer.setInfoStream(System.out);

    for (int i = 0; i < 100; i++) {
        Document doc = new Document();
        doc.add(new Field("keyword", "goober", Field.Store.YES, Field.Index.NOT_ANALYZED));
        writer.addDocument(doc);//from  w  ww  .j av  a  2 s .  c o  m
    }
    writer.optimize();
    writer.close();
}

From source file:analysis.SynonymAnalyzerTest.java

License:Apache License

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

    IndexWriter writer = new IndexWriter(directory, synonymAnalyzer, //#1  
            IndexWriter.MaxFieldLength.UNLIMITED);
    Document doc = new Document();
    doc.add(new Field("content", "The quick brown fox jumps over the lazy dog", Field.Store.YES,
            Field.Index.ANALYZED)); //#2
    writer.addDocument(doc);// w w  w  . j av a2  s .  com

    writer.close();

    searcher = new IndexSearcher(directory, true);
}

From source file:aos.lucene.analysis.codec.MetaphoneAnalyzerTest.java

License:Apache License

public void testKoolKat() throws Exception {

    RAMDirectory directory = new RAMDirectory();
    Analyzer analyzer = new MetaphoneReplacementAnalyzer();

    IndexWriter writer = new IndexWriter(directory, analyzer, true, IndexWriter.MaxFieldLength.UNLIMITED);

    Document doc = new Document();
    doc.add(new Field("contents", "cool cat", Field.Store.YES, Field.Index.ANALYZED));
    writer.addDocument(doc);/*from   www  .  j a  va  2  s.co m*/
    writer.close();

    IndexSearcher searcher = new IndexSearcher(directory);

    Query query = new QueryParser(Version.LUCENE_46, "contents", analyzer).parse("kool kat");

    TopDocs hits = searcher.search(query, 1);
    assertEquals(1, hits.totalHits);
    int docID = hits.scoreDocs[0].doc;
    Document storedDoc = searcher.doc(docID);
    assertEquals("cool cat", storedDoc.get("contents"));

    searcher.close();
}

From source file:aos.lucene.analysis.keyword.KeywordAnalyzerTest.java

License:Apache License

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

    IndexWriter writer = new IndexWriter(directory, new SimpleAnalyzer(), IndexWriter.MaxFieldLength.UNLIMITED);

    Document doc = new Document();
    doc.add(new Field("partnum", "Q36", Field.Store.NO, Field.Index.NOT_ANALYZED_NO_NORMS)); //A
    doc.add(new Field("description", "Illidium Space Modulator", Field.Store.YES, Field.Index.ANALYZED));
    writer.addDocument(doc);/*from w ww  .  j a  v a2  s. c  om*/

    writer.close();

    searcher = new IndexSearcher(directory);
}