com.foundationdb.lucene.SimpleTest.java Source code

Java tutorial

Introduction

Here is the source code for com.foundationdb.lucene.SimpleTest.java

Source

/**
 * FoundationDB Lucene Layer
 * Copyright (c) 2013 FoundationDB, LLC
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package com.foundationdb.lucene;

import java.io.IOException;
import java.util.Arrays;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.util.Version;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class SimpleTest extends TestBase {
    @Test
    public void indexBasic() throws Exception {
        StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_44);
        IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_44, analyzer);
        // recreate the index on each execution
        config.setOpenMode(IndexWriterConfig.OpenMode.CREATE);
        config.setCodec(new FDBCodec());
        FDBDirectory dir = createDirectoryForMethod();
        IndexWriter writer = new IndexWriter(dir, config);
        try {
            writer.addDocument(Arrays.asList(new TextField("title", "The title of my first document", Store.YES),
                    new TextField("content", "The content of the first document", Store.NO)));

            writer.addDocument(Arrays.asList(new TextField("title", "The title of the second document", Store.YES),
                    new TextField("content", "And this is the content", Store.NO)));
        } finally {
            writer.close();
        }
        assertDocumentsAreThere(dir, 2);
    }

    private void assertDocumentsAreThere(Directory dir, int amount) throws IOException {
        IndexReader reader = DirectoryReader.open(dir);
        try {
            assertEquals(amount, reader.numDocs());
        } finally {
            reader.close();
        }
    }
}