就搜一条信息!需要一秒!如果按这个速度还不如直接在数据库查呢!!
package com.test.lucene;import java.io.File;import jeasy.analysis.MMAnalyzer;import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Fieldable;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocCollector;public class LuceneIndex {
public void createIndex(String username,String password,String path){
File f = new File(path);
IndexWriter it = null;
try {
if(f.list()==null){
it = new IndexWriter(path,new MMAnalyzer(),true,IndexWriter.MaxFieldLength.LIMITED);
}else{
it = new IndexWriter(path,new MMAnalyzer(),false,IndexWriter.MaxFieldLength.LIMITED);
}
Document doc = new Document();
Fieldable fa = new Field("un",username,Field.Store.YES, Field.Index.NOT_ANALYZED);
Fieldable fa1 = new Field("pw",password,Field.Store.YES, Field.Index.NOT_ANALYZED);
doc.add(fa);
doc.add(fa1);
it.addDocument(doc);
it.optimize();
it.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public boolean readIndex(String username,String password,String path){
long a = System.currentTimeMillis();
try {
IndexSearcher search = new IndexSearcher(path);
QueryParser parser = new QueryParser("un", new MMAnalyzer());
Query query =parser.parse(username);
TopDocCollector collector = new TopDocCollector(2);
search.search(query,collector);
ScoreDoc[] hits = collector.topDocs().scoreDocs;
for(int i=0;i<hits.length;i++){
Document doc = search.doc(hits[i].doc);
if(username.equals(doc.get("un"))&&password.equals(doc.get("pw"))){
long b = System.currentTimeMillis();
System.out.println(b-a);
return true;
}
} search.close();
} catch (Exception e) {
e.printStackTrace();
}
long b = System.currentTimeMillis();
System.out.println(b-a);
return false;
}
public static void main(String[] args) {
String path = "D:/index";
String username = "xiaosan";
String password = "xieke";
LuceneIndex li = new LuceneIndex();
// li.createIndex(username, password,path);
System.out.println(li.readIndex(username,password,path));
}
}
package com.test.lucene;import java.io.File;import jeasy.analysis.MMAnalyzer;import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Fieldable;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocCollector;public class LuceneIndex {
public void createIndex(String username,String password,String path){
File f = new File(path);
IndexWriter it = null;
try {
if(f.list()==null){
it = new IndexWriter(path,new MMAnalyzer(),true,IndexWriter.MaxFieldLength.LIMITED);
}else{
it = new IndexWriter(path,new MMAnalyzer(),false,IndexWriter.MaxFieldLength.LIMITED);
}
Document doc = new Document();
Fieldable fa = new Field("un",username,Field.Store.YES, Field.Index.NOT_ANALYZED);
Fieldable fa1 = new Field("pw",password,Field.Store.YES, Field.Index.NOT_ANALYZED);
doc.add(fa);
doc.add(fa1);
it.addDocument(doc);
it.optimize();
it.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public boolean readIndex(String username,String password,String path){
long a = System.currentTimeMillis();
try {
IndexSearcher search = new IndexSearcher(path);
QueryParser parser = new QueryParser("un", new MMAnalyzer());
Query query =parser.parse(username);
TopDocCollector collector = new TopDocCollector(2);
search.search(query,collector);
ScoreDoc[] hits = collector.topDocs().scoreDocs;
for(int i=0;i<hits.length;i++){
Document doc = search.doc(hits[i].doc);
if(username.equals(doc.get("un"))&&password.equals(doc.get("pw"))){
long b = System.currentTimeMillis();
System.out.println(b-a);
return true;
}
} search.close();
} catch (Exception e) {
e.printStackTrace();
}
long b = System.currentTimeMillis();
System.out.println(b-a);
return false;
}
public static void main(String[] args) {
String path = "D:/index";
String username = "xiaosan";
String password = "xieke";
LuceneIndex li = new LuceneIndex();
// li.createIndex(username, password,path);
System.out.println(li.readIndex(username,password,path));
}
}
解决方案 »
- spring和hibernate整合做单元测试时报错:DEFAULT和NULL不允许作为显示标识值
- myeclipse安装失败求解啊....【图】
- 使用JNDI名称查找Database的问题
- 对于Java算法主要应学些什么呢
- 关于MyEclipse控件svn
- linux自带的sendmail向hotmail或者sina发邮件时对方收不到
- 使用Middlegen-Hibernate-r4,如何能将“视图”也生成PO ,Oracle数据库
- 各位达人:struts1.2 DynaValidatorForm的bug!大家都过来看看!
- 服务器的win2000是中文版,客户端win2000是英文版,小应用程序输出是乱码?
- 求助 myeclipse8.5创建Filter时项目名称为空导致无法创建Filter 在网上没找到解决办法,请大侠帮我看下有没有解决的办法,或者给些提示。
- 急 Javamail 解析eml文件
- aop拦截器的问题
SSH、Ant、IBatis、jsf、seam、portal、设计模式、
ZK、DWR、ajax、CSS 、Oracle
searcher.Search(new TermQuery(new Term("userName", "你好")));
任何java程序(也包括其它的程序)的执行时间,不要以第一次执行时间为准,请在第一次执行之后再执行同样的代码记录它的时间(不是重新启动程序,而是重复代码)
如果不是你的程序问题,建议你对这个查询循环1000次以上