com.mathworks.xzheng.advsearching.MultiSearcherTest.java Source code

Java tutorial

Introduction

Here is the source code for com.mathworks.xzheng.advsearching.MultiSearcherTest.java

Source

package com.mathworks.xzheng.advsearching;

/**
 * Copyright Manning Publications Co.
 *
 * 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 lan      
*/

import junit.framework.TestCase;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.core.WhitespaceAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.MultiReader;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.TermRangeQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.Version;

// From chapter 5
public class MultiSearcherTest extends TestCase {
    private IndexSearcher[] searchers;

    public void setUp() throws Exception {
        String[] animals = { "aardvark", "beaver", "coati", "dog", "elephant", "frog", "gila monster", "horse",
                "iguana", "javelina", "kangaroo", "lemur", "moose", "nematode", "orca", "python", "quokka", "rat",
                "scorpion", "tarantula", "uromastyx", "vicuna", "walrus", "xiphias", "yak", "zebra" };

        Analyzer analyzer = new WhitespaceAnalyzer(Version.LUCENE_46);

        Directory aTOmDirectory = new RAMDirectory(); // #1
        Directory nTOzDirectory = new RAMDirectory(); // #1

        IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_46, analyzer);

        IndexWriter aTOmWriter = new IndexWriter(aTOmDirectory, config);
        IndexWriter nTOzWriter = new IndexWriter(nTOzDirectory, config);

        for (int i = animals.length - 1; i >= 0; i--) {
            Document doc = new Document();
            String animal = animals[i];
            doc.add(new Field("animal", animal, Field.Store.YES, Field.Index.NOT_ANALYZED));
            if (animal.charAt(0) < 'n') {
                aTOmWriter.addDocument(doc); // #2
            } else {
                nTOzWriter.addDocument(doc); // #2
            }
        }

        aTOmWriter.close();
        nTOzWriter.close();

        searchers = new IndexSearcher[2];
        searchers[0] = new IndexSearcher(DirectoryReader.open(aTOmDirectory));
        searchers[1] = new IndexSearcher(DirectoryReader.open(nTOzDirectory));
    }

    public void testMulti() throws Exception {

        MultiReader multiReader = new MultiReader(searchers[0].getIndexReader(), searchers[1].getIndexReader());
        IndexSearcher searcher = new IndexSearcher(multiReader);

        TermRangeQuery query = new TermRangeQuery("animal", // #3
                new BytesRef("h"), // #3
                new BytesRef("t"), // #3
                true, true);// #3

        TopDocs hits = searcher.search(query, 10);
        assertEquals("tarantula not included", 12, hits.totalHits);
    }

    /*
      #1 Create two directories
      #2 Index halves of the alphabet
      #3 Search both indexes
    */
}