创建索引:--------------------------------------------
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上也是如此,
请各位帮我看看,在线等,谢谢

解决方案 »

  1.   

    http://download.csdn.net/source/2081722 这有个简单小的例子,你可以去看下,希望有帮助我也是初学 lucene
      

  2.   

    也是正在学Lucene楼主好运!!!
      

  3.   

    ,如果传入的是 “ ”会报错(org.apache.lucene.queryParser.ParseException: Cannot parse ' ': Encountered "<EOF>" at line 1, column 1.), 这说明luncene 不能将空格作为关键词查询 也不符合parser的规范 比如 搜索context field 内容为 ‘xy’ 可以是context:xy 其他的什么range啊可以使number:1-100最好使用 query的子类进行查询 比如termquery,termrangequery,booleanquery 等等
      

  4.   

    用 luke 看看你创建的索引里都是些什么东西
      

  5.   

    不知道怎么做判断啊,现在只是一个初步的索引,可以折分但不会判断,也不知道上哪找点资源来学学,如果哪位有的话,是否可以提供一些学习lucene网址,谢谢
      

  6.   

    lucene 初级的学习过程 看src包里的demo包 或是demo.html demo.pdf很多应用介绍的很详细了 只不过是英文的 有代码看起来也很方便的
      

  7.   

    这个分词器不好使啊,3.0之后的使用SmartChineseAnalyzer(Version.LUCENE_30);这个比较好,还有就是IKAnalyzer();这个分词器是唯一一个支持3.0版本以上的,这是我这几天看的