创建索引:--------------------------------------------
private File baseDir;
public File getBaseDir() {
return baseDir;
} public void setBaseDir(File baseDir) {
this.baseDir = baseDir;
} public LogFileIndexer(File baseDir) {
super();
this.baseDir = baseDir;
}
// 读取分词内容方法
public void readContext(String indexName,IndexWriter iwriter,String strLine) throws CorruptIndexException, IOException{
String [] logs=strLine.split(" ");//分隔
for (int j = 0; j < logs.length; j++) {
Document doc = new Document();
Field fileDate=new Field("fielDate",logs[j],Field.Store.YES,Field.Index.NO);//日期不分词
Field fileContext=new Field(indexName,logs[j+1],Field.Store.YES,Field.Index.ANALYZED);
//System.out.println("indexName:"+indexName);////
//System.out.println(logs[j+1]);
doc.add(fileDate);
doc.add(fileContext);
iwriter.addDocument(doc);
break;
}
}
//创建索引
public int index(String indexName, File logFile)throws IOException{
int count=0;
Directory directory = FSDirectory.open(this.getBaseDir());
Analyzer analyzer=new ChineseAnalyzer();
IndexWriter iwriter = new IndexWriter(directory, analyzer,IndexWriter.MaxFieldLength.LIMITED);
File f = new File("D:\\suqingdiWORK\\SS");
File[] files=f.listFiles();// files
for (int i = 0; i < files.length; i++) {
BufferedReader br=new BufferedReader(new FileReader(files[i]));
int aa=br.read();
System.out.println("length:"+aa);//文件条数
String strLine=null;
while (br.readLine()!=null) {//如果还有下一条数据继续读取
strLine=br.readLine();
readContext(indexName,iwriter, strLine);//read
}
}
count= iwriter.numDocs();
// iwriter.optimize();
iwriter.close();
return count;
}
//这里计数索引条数为34条,并且可以把分词的内容输出无误
----------------------------------------------------搜索索引:-----------------------------------------------
private File baseDir;
public File getBaseDir() {
return baseDir;
} public void setBaseDir(File baseDir) {
this.baseDir = baseDir;
} public LogFileSearcher(File baseDir) {
super();
this.baseDir = baseDir;
}
public List search(String indexName, String text) throws IOException, ParseException{
List logContexts=new ArrayList();
Directory dir=FSDirectory.open(this.getBaseDir());//索引位置
IndexSearcher searcher=new IndexSearcher(dir);
QueryParser parser=new QueryParser(Version.LUCENE_30, indexName,new ChineseAnalyzer());
Query query=parser.parse(text);
TopDocs dd=searcher.search(query,null,5000);
ScoreDoc[] hits=dd.scoreDocs;
// System.out.println(hits.length);
for (int i = 0; i < hits.length; i++) {
Document hitDoc = searcher.doc(i);
DocBean db=new DocBean();
db.setContext(hitDoc.get(indexName));
db.setDate(hitDoc.get("fielDate"));
logContexts.add(db);
// System.out.println(hitDoc.get(indexName));
}
searcher.close();
return logContexts;
}---------------------------------------------------
测试:
private static final File INDEX_DIR=new File("D:\\suqingdiWORK\\cc\\2");
public static void main(String[] args) {
LogFileIndexer log=new LogFileIndexer(INDEX_DIR);
try {
int indexCount=log.index("fielContext", INDEX_DIR);
System.out.println("indexCount:"+indexCount);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
LogFileSearcher searcher=new LogFileSearcher(INDEX_DIR);
try {
List logContexts=searcher.search("fielContext", "仙");
System.out.println("listSize:"+logContexts.size()); } catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//
这里打印出listSize:2,而且如果打出来的话每次都是前2条数据,如果传入的是 “ ”会报错(org.apache.lucene.queryParser.ParseException: Cannot parse ' ': Encountered "<EOF>" at line 1, column 1.),
调试不出哪里出现问题,放到WEB上也是如此,
请各位帮我看看,在线等,谢谢
private File baseDir;
public File getBaseDir() {
return baseDir;
} public void setBaseDir(File baseDir) {
this.baseDir = baseDir;
} public LogFileIndexer(File baseDir) {
super();
this.baseDir = baseDir;
}
// 读取分词内容方法
public void readContext(String indexName,IndexWriter iwriter,String strLine) throws CorruptIndexException, IOException{
String [] logs=strLine.split(" ");//分隔
for (int j = 0; j < logs.length; j++) {
Document doc = new Document();
Field fileDate=new Field("fielDate",logs[j],Field.Store.YES,Field.Index.NO);//日期不分词
Field fileContext=new Field(indexName,logs[j+1],Field.Store.YES,Field.Index.ANALYZED);
//System.out.println("indexName:"+indexName);////
//System.out.println(logs[j+1]);
doc.add(fileDate);
doc.add(fileContext);
iwriter.addDocument(doc);
break;
}
}
//创建索引
public int index(String indexName, File logFile)throws IOException{
int count=0;
Directory directory = FSDirectory.open(this.getBaseDir());
Analyzer analyzer=new ChineseAnalyzer();
IndexWriter iwriter = new IndexWriter(directory, analyzer,IndexWriter.MaxFieldLength.LIMITED);
File f = new File("D:\\suqingdiWORK\\SS");
File[] files=f.listFiles();// files
for (int i = 0; i < files.length; i++) {
BufferedReader br=new BufferedReader(new FileReader(files[i]));
int aa=br.read();
System.out.println("length:"+aa);//文件条数
String strLine=null;
while (br.readLine()!=null) {//如果还有下一条数据继续读取
strLine=br.readLine();
readContext(indexName,iwriter, strLine);//read
}
}
count= iwriter.numDocs();
// iwriter.optimize();
iwriter.close();
return count;
}
//这里计数索引条数为34条,并且可以把分词的内容输出无误
----------------------------------------------------搜索索引:-----------------------------------------------
private File baseDir;
public File getBaseDir() {
return baseDir;
} public void setBaseDir(File baseDir) {
this.baseDir = baseDir;
} public LogFileSearcher(File baseDir) {
super();
this.baseDir = baseDir;
}
public List search(String indexName, String text) throws IOException, ParseException{
List logContexts=new ArrayList();
Directory dir=FSDirectory.open(this.getBaseDir());//索引位置
IndexSearcher searcher=new IndexSearcher(dir);
QueryParser parser=new QueryParser(Version.LUCENE_30, indexName,new ChineseAnalyzer());
Query query=parser.parse(text);
TopDocs dd=searcher.search(query,null,5000);
ScoreDoc[] hits=dd.scoreDocs;
// System.out.println(hits.length);
for (int i = 0; i < hits.length; i++) {
Document hitDoc = searcher.doc(i);
DocBean db=new DocBean();
db.setContext(hitDoc.get(indexName));
db.setDate(hitDoc.get("fielDate"));
logContexts.add(db);
// System.out.println(hitDoc.get(indexName));
}
searcher.close();
return logContexts;
}---------------------------------------------------
测试:
private static final File INDEX_DIR=new File("D:\\suqingdiWORK\\cc\\2");
public static void main(String[] args) {
LogFileIndexer log=new LogFileIndexer(INDEX_DIR);
try {
int indexCount=log.index("fielContext", INDEX_DIR);
System.out.println("indexCount:"+indexCount);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
LogFileSearcher searcher=new LogFileSearcher(INDEX_DIR);
try {
List logContexts=searcher.search("fielContext", "仙");
System.out.println("listSize:"+logContexts.size()); } catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//
这里打印出listSize:2,而且如果打出来的话每次都是前2条数据,如果传入的是 “ ”会报错(org.apache.lucene.queryParser.ParseException: Cannot parse ' ': Encountered "<EOF>" at line 1, column 1.),
调试不出哪里出现问题,放到WEB上也是如此,
请各位帮我看看,在线等,谢谢
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货