at.ac.univie.mminf.luceneSKOS.test.termexpansion.AbstractTermExpansionTest.java Source code

Java tutorial

Introduction

Here is the source code for at.ac.univie.mminf.luceneSKOS.test.termexpansion.AbstractTermExpansionTest.java

Source

package at.ac.univie.mminf.luceneSKOS.test.termexpansion;

/**
 * Copyright 2010 Bernhard Haslhofer 
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import org.apache.lucene.analysis.core.SimpleAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.*;
import org.apache.lucene.search.*;
import org.apache.lucene.store.LockObtainFailedException;
import org.apache.lucene.store.RAMDirectory;
import org.junit.After;
import org.junit.Test;

import java.io.IOException;

import static org.junit.Assert.assertEquals;

/**
 * Common code used for all Use Case (UC) tests
 */
public abstract class AbstractTermExpansionTest {

    protected IndexSearcher searcher;

    protected IndexWriter writer;

    @After
    public void tearDown() throws Exception {
        if (writer != null) {
            writer.close();
        }
        if (searcher != null && searcher.getIndexReader() != null) {
            searcher.getIndexReader().close();
        }
    }

    /**
     * This test indexes a sample metadata record (=lucene document) having a
     * "title", "description", and "subject" field, which contains plain subject
     * terms.
     * <p/>
     * 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 IOException {

        /* defining the document to be indexed */
        Document doc = new Document();
        doc.add(new Field("title", "Spearhead", TextField.TYPE_STORED));
        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).",
                TextField.TYPE_NOT_STORED));
        doc.add(new Field("subject", "weapons", TextField.TYPE_NOT_STORED));

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

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

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

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

        TopDocs results = searcher.search(builder.build(), 10);

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