aos.lucene.analysis.positional.PositionalPorterStopAnalyzerTest.java Source code

Java tutorial

Introduction

Here is the source code for aos.lucene.analysis.positional.PositionalPorterStopAnalyzerTest.java

Source

/****************************************************************
 * Licensed to the AOS Community (AOS) under one or more        *
 * contributor license agreements.  See the NOTICE file         *
 * distributed with this work for additional information        *
 * regarding copyright ownership.  The AOS licenses this file   *
 * to you 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.                                           *
 ****************************************************************/
package aos.lucene.analysis.positional;

import junit.framework.TestCase;

import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.Version;

import io.aos.lucene.util.TestUtil;

// From chapter 4
public class PositionalPorterStopAnalyzerTest extends TestCase {
    private static PositionalPorterStopAnalyzer porterAnalyzer = new PositionalPorterStopAnalyzer();

    private IndexSearcher searcher;
    private QueryParser parser;

    public void setUp() throws Exception {

        RAMDirectory directory = new RAMDirectory();

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

        Document doc = new Document();
        doc.add(new Field("contents", "The quick brown fox jumps over the lazy dog", Field.Store.YES,
                Field.Index.ANALYZED));
        writer.addDocument(doc);
        writer.close();
        searcher = new IndexSearcher(directory, true);
        parser = new QueryParser(Version.LUCENE_46, "contents", porterAnalyzer);
    }

    public void testWithSlop() throws Exception {
        parser.setPhraseSlop(1);

        Query query = parser.parse("\"over the lazy\"");

        assertEquals("hole accounted for", 1, TestUtil.hitCount(searcher, query));
    }

    public void testStems() throws Exception {
        Query query = new QueryParser(Version.LUCENE_46, "contents", porterAnalyzer).parse("laziness");
        assertEquals("lazi", 1, TestUtil.hitCount(searcher, query));

        query = parser.parse("\"fox jumped\"");
        assertEquals("jump jumps jumped jumping", 1, TestUtil.hitCount(searcher, query));
    }

}