lucene索引建好后查询不到。

网名有什么用 发布于 2014/01/10 13:27
阅读 1K+
收藏 0
import java.io.File;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


import model.QDesc;
import mysql.MysqlDb;


import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryparser.classic.MultiFieldQueryParser;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.queryparser.classic.QueryParser.Operator;
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.FSDirectory;
import org.apache.lucene.store.SimpleFSDirectory;
import org.apache.lucene.util.Version;
import org.wltea.analyzer.lucene.IKAnalyzer;


public class LuceneDemo {
public void createIndexFile() {
IndexWriter indexWriter = null;
try {
// 需要的分词器
Analyzer analyzer = new IKAnalyzer(true);
//Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_36);
// 创建的是哪个版本的IndexWriterConfig
IndexWriterConfig indexWriterConfig = new IndexWriterConfig(
Version.LUCENE_46, analyzer);
// 创建系统文件-----
Directory directory = new SimpleFSDirectory(new File("D:\\index"));
indexWriter = new IndexWriter(directory, indexWriterConfig);
// 访问数据库拿数据
MysqlDb db = new MysqlDb();
List<QDesc> QDescList = db.getQDescList();
for (QDesc qdesc : QDescList) {
Document doc = new Document();
doc.add(new StringField("url", qdesc.getUrl(), Field.Store.YES));
doc.add(new StringField("question", qdesc.getQuestion(),
Field.Store.YES));
doc.add(new StringField("date", qdesc.getDate(),
Field.Store.YES));
doc.add(new StringField("test", "test",
Field.Store.YES));
indexWriter.addDocument(doc);
}


} catch (Exception e) {
e.printStackTrace();
} finally {
if (indexWriter != null) {
try {
indexWriter.commit();
indexWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}


public void seacher(String queryString) throws IOException {
FSDirectory fs;
try {
fs = FSDirectory.open(new File("D:\\index"));
IndexReader ir = IndexReader.open(fs);
long start = System.currentTimeMillis();
IndexSearcher search = new IndexSearcher(ir);
String key = queryString;
String fieds[] = new String[] { "test","url", "question", "date" };
//Analyzer ik = new StandardAnalyzer(Version.LUCENE_36);
Analyzer ik = new IKAnalyzer(true);
MultiFieldQueryParser m = new MultiFieldQueryParser(
Version.LUCENE_46, fieds, ik);
m.setPhraseSlop(3); 
m.setDefaultOperator(QueryParser.AND_OPERATOR);

Query query = m.parse(key);
System.out.println(query.toString());
// Query query = m.parse(Version.LUCENE_30, queries, fields, clauses, new StandardAnalyzer(Version.LUCENE_30));


ScoreDoc[] hits = search.search(query, 10).scoreDocs;
System.out.println("共命中" + hits.length + "条记录");
for (ScoreDoc scoreDoc : hits) {
Document doc = search.doc(scoreDoc.doc);
System.out.println(scoreDoc.score + "\t url:"
+ doc.get("url") + "\t question:"
+ doc.get("question") + "\t date:"
+ doc.get("date"));
}
System.out.println("执行时间:" + (System.currentTimeMillis() - start)
+ "毫秒");
} catch (Exception e) {
e.printStackTrace();
}


}


public static void main(String[] args) throws IOException {
LuceneDemo test = new LuceneDemo();
test.createIndexFile();
test.seacher("test");
test.seacher("question");
test.seacher("保监会近期将启动历史存量保单投资蓝筹股");
test.seacher("http://www.zhihu.com/question/20736791");


}
}
加载中
0
Hunts
Hunts
写入索引的字段都是Store,而非Index。也就是未建立索引。
返回顶部
顶部