csdn.lucene.first.version.Searcher.java Source code

Java tutorial

Introduction

Here is the source code for csdn.lucene.first.version.Searcher.java

Source

package csdn.lucene.first.version;

/**
 * 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 org.apache.lucene.document.Document;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.wltea.analyzer.lucene.IKAnalyzer;

import java.nio.file.Paths;
import java.util.ArrayList;

// From chapter 1

/**
 * This code was originally written for
 * Erik's Lucene intro java.net article
 */

//you should build indexer and then search that lead to a correct answer
public class Searcher {

    String indexDir = "d:/crawl/data3/text_index/"; //1
    String dataDir = "d:/crawl/data3/text/"; //2
    String s; //query words
    Analyzer analyzer = new IKAnalyzer(true);
    //5.3  lists to store
    ArrayList<String> hit_ids = new ArrayList<String>();
    ArrayList<String> hit_paths = new ArrayList<String>();

    public ArrayList<String> search_init(String s) {

        this.s = s;
        System.out.println("From Searcher search_init : query = " + s);
        try {
            hit_ids = search(indexDir, s);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return hit_ids;
    }

    protected ArrayList<String> search(String indexDir, String q) {
        //5.5 replace directory with path 
        java.nio.file.Path pathA = Paths.get(indexDir);
        FSDirectory dir;
        IndexSearcher is;
        try {
            dir = FSDirectory.open(pathA);
            DirectoryReader dReader = DirectoryReader.open(dir);
            is = new IndexSearcher(dReader);

            QueryParser parser = new QueryParser("contents", analyzer);
            Query query = parser.parse(q);
            long start = System.currentTimeMillis();
            //is.search():Finds the top n hits for query
            //TopDocs:Represents hits returned by IndexSearcher.search(Query,int).
            TopDocs hits = is.search(query, 10); //5 
            long end = System.currentTimeMillis();

            System.err.println("Found " + hits.totalHits + //6  
                    " document(s) (in " + (end - start) + // 6
                    " milliseconds) that matched query '" + // 6
                    q + "':"); // 6

            //ScoreDocs:The top hits for the query.
            for (ScoreDoc scoreDoc : hits.scoreDocs) {
                Document doc = is.doc(scoreDoc.doc);
                //         System.out.println("From Searcher : filename = " + doc.get("fieldname"));
                //         System.out.println("From Searcher : fullpath = " + doc.get("fullpath"));
                hit_ids.add(doc.get("fieldname"));
                hit_paths.add(doc.get("fullpath"));
            }
            dReader.close();
            dir.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return hit_ids;
    }
}

/*
#1 Parse provided index directory
#2 Parse provided query string
#3 Open index
#4 Parse query
#5 Search index
#6 Write search stats
#7 Retrieve matching document
#8 Display filename
#9 Close IndexSearcher
*/