lucene 6.1.0
在内存中建立索引, 90W条数据占用约1.5G内存
public class AppInst {
private static AppInst ourInstance = new AppInst();
public static AppInst getInstance() {
return ourInstance;
}
private AppInst() {
}
public static void main(String argv[]) throws Exception {
AppInst.getInstance().main();
}
public void main() throws Exception {
RAMDirectory ram = new RAMDirectory();
IndexWriter writer = new IndexWriter(ram, new IndexWriterConfig(new StandardAnalyzer()));
createIndex(writer);
writer.close();
while (true) {
search(ram);
Thread.sleep(1000);
}
}
void createIndex(IndexWriter writer) throws Exception {
Connection conn = Proxool.getInstance().getConnection();
try {
PreparedStatement stmt = conn.prepareStatement("select * from items_bak");
if (stmt.execute()) {
ResultSet rs = stmt.getResultSet();
int nCount = 0;
while (rs.next()) {
int id = rs.getInt("id");
String detail = rs.getString("detail");
createIndexEx(writer, id, detail);
nCount++;
}
System.out.println(String.format("共%d条数据", nCount));
}
} finally {
conn.close();
}
}
void createIndexEx(IndexWriter writer, int id, String detail) throws IOException {
Document doc = new Document();
doc.add(new Field("id", Integer.toString(id), TextField.TYPE_STORED));
doc.add(new Field("detail", detail, TextField.TYPE_STORED));
writer.addDocument(doc);
}
void search(Directory dir) throws IOException, ParseException {
IndexSearcher searcher = new IndexSearcher(DirectoryReader.open(dir));
QueryParser parser = new QueryParser("detail", new StandardAnalyzer());
long t1 = System.currentTimeMillis();
TopDocs rs = searcher.search(parser.parse("北京"), 100);
long t2 = System.currentTimeMillis();
System.out.println("耗时: " + (t2 - t1));
System.out.println(String.format("找到%d条匹配数据", rs.totalHits));
for (ScoreDoc sd : rs.scoreDocs) {
Document doc = searcher.doc(sd.doc);
String detail = doc.getField("detail").stringValue();
//System.out.println(detail);
}
}
}
共907984条数据
耗时: 53
找到226921条匹配数据
刚开始接触, 很多接口和参数还不了解, 若有错误请指出.