lucene对数据库建立索引

package com.mysqindex;


import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
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.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.wltea.analyzer.lucene.IKAnalyzer;

import com.mysql.MySql;

public class MysqlIndex {
	//对博客内容创建索引
	public static boolean buildBlogIndex(final Analyzer analyzer, final Directory directory, String selectsql, final int num){
		try {
			IndexWriter indexWriter = new IndexWriter(directory, new IndexWriterConfig(Version.LUCENE_47, analyzer));
			indexWriter.deleteAll();
			int begin = 0;
			String selectsql2 = selectsql;
			selectsql2 += " limit " + begin + "," + num;
			//读取表里面指定的内容
			Map<String,String> map = MySql.selectBlogData(selectsql2);
			while(map.size() != 0){
				Iterator it = map.entrySet().iterator();
				while(it.hasNext()){
					Document doc = new Document();
					Map.Entry<String, String> entry = (Entry<String, String>) it.next();
					doc.add(new Field("id", entry.getKey(), TextField.TYPE_STORED));
					doc.add(new Field("content", entry.getValue(), TextField.TYPE_NOT_STORED ));
					indexWriter.addDocument(doc);
				}
				selectsql2 = selectsql;
				begin += num;
				selectsql2 += " limit " + (begin-1) + "," + num;
				//indexWriter.close();
				map = MySql.selectBlogData(selectsql2);
			}
			indexWriter.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		return true;
	}
	//根据关键字进行搜索
	public static LinkedList<String> searchBlogIndex(final Analyzer analyzer, final Directory directory, final String keyword, final int score) {
        DirectoryReader ireader = null;
        LinkedList<String> list = new LinkedList<String>();
        try {
            // 打开索引存储
            ireader = DirectoryReader.open(directory);
            IndexSearcher isearcher = new IndexSearcher(ireader);
            QueryParser par1 = new QueryParser(Version.LUCENE_47,"content",analyzer);
            Query query = par1.parse(keyword);
            //设置评分靠前的score条数据
            ScoreDoc[] hits = isearcher.search(query, null, score).scoreDocs;
            for (int i = 0; i < hits.length; i++) {
                Document hitDoc = isearcher.doc(hits[i].doc);
                //得到查询数据的内容
               // System.out.println(hitDoc.get("content")+ " +++++++++++++++++++");
                //得到查询数据的地址
                list.add(hitDoc.get("id"));
               // System.out.println(hitDoc.get("id") );
                //得到查询数据的题目
                //System.out.println(hitDoc.get("title"));
      
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
            
        } finally {
            try {
                ireader.close();
                directory.close();
            } catch (IOException e) {
            }
        }
      return list;
    }
	
	
	public static void main(String []args){
		File indexFile = new File("c:/lucene");
		Analyzer analyzer = new IKAnalyzer();
		Directory directory;
		try {
			directory = FSDirectory.open(indexFile);
			String selectsql = "select * from blog";
			int num = 10;
			int score = 5;
			String keyword = "winnerspring是何许人也";
			buildBlogIndex(analyzer, directory,selectsql, num);
			searchBlogIndex(analyzer, directory, keyword, score);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值