李国斌ID:siyou
1449次访问,排名2万外好友0人,关注者0
siyou的文章
原创 7 篇
翻译 0 篇
转载 0 篇
评论 24 篇
最近评论
文章分类
收藏
    相册
    存档
    软件项目交易
    订阅我的博客
    XML聚合  FeedSky
    订阅到鲜果
    订阅到Google
    订阅到抓虾
    订阅到BlogLines
    订阅到Yahoo
    订阅到GouGou
    订阅到飞鸽
    订阅到Rojo
    订阅到newsgator
    订阅到netvibes

    原创 如何用LUCENE 2.0进行全文索引搜索收藏

    新一篇: 多线程程序中,如何等待一个线程完成后才继续下面的工作 | 

    文章以最简单为例,展示LUCENE 2.0的索引方法

    需要IMPORT的东西:
    import java.sql.*;
    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.index.IndexWriter;
    import java.io.IOException;
    import org.apache.lucene.search.IndexSearcher;
    import org.apache.lucene.queryParser.QueryParser;
    import org.apache.lucene.search.Hits;
    import org.apache.lucene.index.IndexReader;
    import org.apache.lucene.search.Searcher;
    import org.apache.lucene.queryParser.MultiFieldQueryParser;
    import org.apache.lucene.search.Query;

    建立的代码:
        public void createIndex() {
                try {
                    IndexWriter writer=new IndexWriter("g:\\index",new StandardAnalyzer(),true);
                    for(int i=0;i<1000;i++){
                    Document lDoc=new Document();
                    lDoc.add(new Field("title","中国人民办公室",Field.Store.YES,Field.Index.TOKENIZED));
                    lDoc.add(new Field("content","广州市中山医院",Field.Store.YES,Field.Index.TOKENIZED));
                    writer.addDocument(lDoc);
                    }
                     writer.optimize();
                     writer.close();

                }
            catch(Exception ex){
                System.out.println(ex);
                System.exit(0);
            }
        }

     

    读取的代码:   

     public void seacher(String queryString){
            Hits hits=null;
            IndexReader ir;
            Searcher search;
            try{
                ir=IndexReader.open("g:\\index");
                search=new IndexSearcher(ir);
                String fields[] = { "title", "content" };
                QueryParser parser = new MultiFieldQueryParser(fields,new StandardAnalyzer());
                Query query = parser.parse("中山");
                hits = search.search(query);
                for (int i = 0; i < hits.length(); i++) {
                    Document doc = hits.doc(i);
                    String docValue = doc.get("content");
                    System.out.println(docValue);
                }
            }
            catch(Exception e){
                System.out.print(e);
            }
          }

     

    其中fields是要查找的字段,

    title和content是要索引的字段名称,可以自定义的

    “中国人民办公室”和“广州市中山医院”是要索引的内容

    Field.Index.TOKENIZED是表示要索引的字段,如果不需要索引,可以写Field.Index.NO

    "g:\\index"是要放索引文件的地点,程序会在g:建立一个index的目录,里面会有三个索引文件

    好了,其它更深澳的东东,我还在研究,自己努力吧,呵呵

     

    发表于 @ 2006年07月25日 10:35:00|评论(loading...)|编辑

    新一篇: 多线程程序中,如何等待一个线程完成后才继续下面的工作 | 

    评论:没有评论。

    发表评论  


    当前用户设置只有注册用户才能发表评论。如果你没有登录,请点击登录
    Csdn Blog version 3.1a
    Copyright © siyou