com.lucene.index.test.IKAnalyzerdemo.java Source code

Java tutorial

Introduction

Here is the source code for com.lucene.index.test.IKAnalyzerdemo.java

Source

/**
 * IK ?   5.0
 * IK Analyzer release 5.0
 * 
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF 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.
 *
 * ??(linliangyi2005@gmail.com)??
 * ? 2012
 * provided by Linliangyi and copyright 2012 by Oolong studio
 * 
 * 
 */
package com.lucene.index.test;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.CorruptIndexException;
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.index.MultiReader;
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
import org.apache.lucene.queryparser.classic.MultiFieldQueryParser;
import org.apache.lucene.queryparser.classic.ParseException;
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.Directory;
import org.apache.lucene.store.LockObtainFailedException;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.Version;
import org.wltea.analyzer.lucene.IKAnalyzer;

/**
 * IKAnalyzerLucene
 * 2012-3-2
 * 
 * ?IKAnalyzerdemo API
 *
 */
public class IKAnalyzerdemo {

    /**
     * 
     * ???
     * @param args
     */
    public static void main(String[] args) {
        //Lucene Document??
        String fieldName = "text";
        //
        String text1 = "oracle,?";
        String text2 = "?";
        String text3 = "?";

        //IKAnalyzer?
        Analyzer analyzer = new IKAnalyzer();

        Directory directory1 = null;
        Directory directory2 = null;
        IndexWriter iwriter1 = null;
        IndexWriter iwriter2 = null;
        IndexReader ireader1 = null;
        IndexReader ireader2 = null;
        IndexSearcher isearcher = null;
        try {
            //
            directory1 = new RAMDirectory();
            directory2 = new RAMDirectory();

            //?IndexWriterConfig

            IndexWriterConfig iwConfig1 = new IndexWriterConfig(analyzer);
            iwConfig1.setOpenMode(OpenMode.CREATE);

            IndexWriterConfig iwConfig2 = new IndexWriterConfig(analyzer);
            iwConfig2.setOpenMode(OpenMode.CREATE);
            iwriter1 = new IndexWriter(directory1, iwConfig1);
            iwriter2 = new IndexWriter(directory2, iwConfig2);

            //
            Document doc1 = new Document();
            doc1.add(new StringField("ID", "10000", Field.Store.YES));
            doc1.add(new TextField("text1", text1, Field.Store.YES));
            iwriter1.addDocument(doc1);

            Document doc2 = new Document();
            doc2.add(new StringField("ID", "10001", Field.Store.YES));
            doc2.add(new TextField("text2", text2, Field.Store.YES));
            iwriter2.addDocument(doc2);

            iwriter1.close();
            iwriter2.close();

            //?**********************************
            //?   
            ireader1 = DirectoryReader.open(directory1);
            ireader2 = DirectoryReader.open(directory2);

            IndexReader[] mreader = { ireader1, ireader2 };

            MultiReader multiReader = new MultiReader(mreader);

            isearcher = new IndexSearcher(multiReader);

            String keyword = "?";
            //QueryParser?Query
            String[] fields = { "text1", "text2" };

            Map<String, Float> boosts = new HashMap<String, Float>();
            boosts.put("text1", 5.0f);
            boosts.put("text2", 2.0f);
            /**MultiFieldQueryParser??? 
             * */
            MultiFieldQueryParser parser = new MultiFieldQueryParser(fields, analyzer, boosts);
            Query query = parser.parse(keyword);

            System.out.println("Query = " + query);

            //?5?
            TopDocs topDocs = isearcher.search(query, 5);
            System.out.println("" + topDocs.totalHits);
            //
            ScoreDoc[] scoreDocs = topDocs.scoreDocs;
            for (int i = 0; i < topDocs.totalHits; i++) {
                Document targetDoc = isearcher.doc(scoreDocs[i].doc);
                System.out.println("" + targetDoc.toString());
            }

        } catch (CorruptIndexException e) {
            e.printStackTrace();
        } catch (LockObtainFailedException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        } finally {
            if (ireader1 != null) {
                try {
                    ireader1.close();
                    ireader2.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (directory1 != null) {
                try {
                    directory1.close();
                    directory2.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}