<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆 - 154  文章 - 60  trackbacks - 0
    <2008年3月>
    2425262728291
    2345678
    9101112131415
    16171819202122
    23242526272829
    303112345

    聲明:

    該blog是為了收集資料,認(rèn)識(shí)朋友,學(xué)習(xí)、提高技術(shù),所以本blog的內(nèi)容除非聲明,否則一律為轉(zhuǎn)載!!

    感謝那些公開(kāi)自己技術(shù)成果的高人們!!!

    支持開(kāi)源,尊重他人的勞動(dòng)!!

    常用鏈接

    留言簿(3)

    隨筆分類(148)

    隨筆檔案(143)

    收藏夾(2)

    其他

    學(xué)習(xí)(技術(shù))

    觀察思考(非技術(shù))

    搜索

    •  

    最新評(píng)論

    閱讀排行榜

    評(píng)論排行榜

    搜索篇:lucene的簡(jiǎn)單實(shí)例<一>
    原文地址:http://www.javaeye.com/topic/39597

    說(shuō)明一下,這一篇文章的用到的lucene,是用2.0版本的,主要在查詢的時(shí)候2.0版本的lucene與以前的版本有了一些區(qū)別.
    其實(shí)這一些代碼都是早幾個(gè)月寫的,自己很懶,所以到今天才寫到自己的博客上,高深的文章自己寫不了,只能記錄下一些簡(jiǎn)單的記錄與點(diǎn)滴,其中的代碼算是自?shī)首詷?lè)的,希望高手不要把重構(gòu)之類的砸下來(lái)...

    1、在windows系統(tǒng)下的的C盤,建一個(gè)名叫s的文件夾,在該文件夾里面隨便建三個(gè)txt文件,隨便起名啦,就叫"1.txt","2.txt"和"3.txt"啦
    其中1.txt的內(nèi)容如下:
    1. 中華人民共和國(guó)   
    2. 全國(guó)人民   
    3. 2006年  
    而"2.txt"和"3.txt"的內(nèi)容也可以隨便寫幾寫,這里懶寫,就復(fù)制一個(gè)和1.txt文件的內(nèi)容一樣吧

    2、下載lucene包,放在classpath路徑中
    建立索引:

    java代碼:
    package lighter.javaeye.com;

    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.Date;

    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;

    /**
     * author lighter date 2006-8-7
     
    */

    public class TextFileIndexer {
        
    public static void main(String[] args) throws Exception {
            
    /* 指明要索引文件夾的位置,這里是C盤的S文件夾下 */
            File fileDir 
    = new File("c:\\s");

            
    /* 這里放索引文件的位置 */
            File indexDir 
    = new File("c:\\index");
            Analyzer luceneAnalyzer 
    = new StandardAnalyzer();
            IndexWriter indexWriter 
    = new IndexWriter(indexDir, luceneAnalyzer,
                    
    true);
            File[] textFiles 
    = fileDir.listFiles();
            
    long startTime = new Date().getTime();
            
            
    //增加document到索引去
            for (int i = 0; i < textFiles.length; i++{
                
    if (textFiles[i].isFile()
                        
    && textFiles[i].getName().endsWith(".txt")) {
                    System.out.println(
    "File " + textFiles[i].getCanonicalPath()
                            
    + "正在被索引.");
                    String temp 
    = FileReaderAll(textFiles[i].getCanonicalPath(),
                            
    "GBK");
                    System.out.println(temp);
                    Document document 
    = new Document();
                    Field FieldPath 
    = new Field("path", textFiles[i].getPath(),
                            Field.Store.YES, Field.Index.NO);
                    Field FieldBody 
    = new Field("body", temp, Field.Store.YES,
                            Field.Index.TOKENIZED,
                            Field.TermVector.WITH_POSITIONS_OFFSETS);
                    document.add(FieldPath);
                    document.add(FieldBody);
                    indexWriter.addDocument(document);
                }

            }

            
    //optimize()方法是對(duì)索引進(jìn)行優(yōu)化
            indexWriter.optimize();
            indexWriter.close();
            
            
    //測(cè)試一下索引的時(shí)間
            long endTime = new Date().getTime();
            System.out
                    .println(
    "這花費(fèi)了"
                            
    + (endTime - startTime)
                            
    + " 毫秒來(lái)把文檔增加到索引里面去!"
                            
    + fileDir.getPath());
        }


        
    public static String FileReaderAll(String FileName, String charset)
                
    throws IOException {
            BufferedReader reader 
    = new BufferedReader(new InputStreamReader(
                    
    new FileInputStream(FileName), charset));
            String line 
    = new String();
            String temp 
    = new String();
            
            
    while ((line = reader.readLine()) != null{
                temp 
    += line;
            }

            reader.close();
            
    return temp;
        }

    }

    索引的結(jié)果:
    File C:\s\1.txt正在被索引.
    中華人民共和國(guó)全國(guó)人民2006年
    File C:\s\
    2.txt正在被索引.
    中華人民共和國(guó)全國(guó)人民2006年
    File C:\s\
    3.txt正在被索引.
    中華人民共和國(guó)全國(guó)人民2006年
    這花費(fèi)了297 毫秒來(lái)把文檔增加到索引里面去
    !c:\s

    3、建立了索引之后,查詢啦....

    java代碼:
    package lighter.javaeye.com;

    import java.io.IOException;

    import org.apache.lucene.analysis.Analyzer;
    import org.apache.lucene.analysis.standard.StandardAnalyzer;
    import org.apache.lucene.queryParser.ParseException;
    import org.apache.lucene.queryParser.QueryParser;
    import org.apache.lucene.search.Hits;
    import org.apache.lucene.search.IndexSearcher;
    import org.apache.lucene.search.Query;

    public class TestQuery {
        
    public static void main(String[] args) throws IOException, ParseException {
            Hits hits 
    = null;
            String queryString 
    = "中華";
            Query query 
    = null;
            IndexSearcher searcher 
    = new IndexSearcher("c:\\index");

            Analyzer analyzer 
    = new StandardAnalyzer();
            
    try {
                QueryParser qp 
    = new QueryParser("body", analyzer);
                query 
    = qp.parse(queryString);
            }
     catch (ParseException e) {
            }

            
    if (searcher != null{
                hits 
    = searcher.search(query);
                
    if (hits.length() > 0{
                    System.out.println(
    "找到:" + hits.length() + " 個(gè)結(jié)果!");
                }

            }

        }


    }

    其運(yùn)行結(jié)果:
    找到:3 個(gè)結(jié)果!

    具體的API的用法,這里就不說(shuō)了,具體的做法參考lucene的官方文檔吧...


    注:lucene2.0的API 與lucene1.4.3的API 有了一些區(qū)別. (例子中用到的)

    這是lucene2.0的API
     QueryParser qp = new QueryParser("body", analyzer);   
     query 
    = qp.parse(queryString);   

    這是lucene1.4.3版的API
     query = QueryParser.parse(key,queryString,new new StandardAnalyzer());


    ================================

    搜索篇:lucene的簡(jiǎn)單實(shí)例<二>

    原文地址:http://www.javaeye.com/post/190576

    寫文章的時(shí)候,感覺(jué)比較難寫的就是標(biāo)題,有時(shí)候不知道起什么名字好,反正這里寫的都是關(guān)于lucene的一些簡(jiǎn)單的實(shí)例,就隨便起啦.

    Lucene 其實(shí)很簡(jiǎn)單的,它最主要就是做兩件事:建立索引和進(jìn)行搜索
    來(lái)看一些在lucene中使用的術(shù)語(yǔ),這里并不打算作詳細(xì)的介紹,只是點(diǎn)一下而已----因?yàn)檫@一個(gè)世界有一種好東西,叫搜索。

    IndexWriter:lucene中最重要的的類之一,它主要是用來(lái)將文檔加入索引,同時(shí)控制索引過(guò)程中的一些參數(shù)使用。

    Analyzer:分析器,主要用于分析搜索引擎遇到的各種文本。常用的有StandardAnalyzer分析器,StopAnalyzer分析器,WhitespaceAnalyzer分析器等。

    Directory:索引存放的位置;lucene提供了兩種索引存放的位置,一種是磁盤,一種是內(nèi)存。一般情況將索引放在磁盤上;相應(yīng)地lucene提供了FSDirectory和RAMDirectory兩個(gè)類。

    Document:文檔;Document相當(dāng)于一個(gè)要進(jìn)行索引的單元,任何可以想要被索引的文件都必須轉(zhuǎn)化為Document對(duì)象才能進(jìn)行索引。

    Field:字段。

    IndexSearcher:是lucene中最基本的檢索工具,所有的檢索都會(huì)用到IndexSearcher工具;

    Query:查詢,lucene中支持模糊查詢,語(yǔ)義查詢,短語(yǔ)查詢,組合查詢等等,如有TermQuery,BooleanQuery,RangeQuery,WildcardQuery等一些類。

    QueryParser: 是一個(gè)解析用戶輸入的工具,可以通過(guò)掃描用戶輸入的字符串,生成Query對(duì)象。

    Hits:在搜索完成之后,需要把搜索結(jié)果返回并顯示給用戶,只有這樣才算是完成搜索的目的。在lucene中,搜索的結(jié)果的集合是用Hits類的實(shí)例來(lái)表示的。

    上面作了一大堆名詞解釋,下面就看幾個(gè)簡(jiǎn)單的實(shí)例吧:
    1、簡(jiǎn)單的的StandardAnalyzer測(cè)試?yán)?

    java代碼:
    package lighter.javaeye.com;

    import java.io.IOException;
    import java.io.StringReader;

    import org.apache.lucene.analysis.Analyzer;
    import org.apache.lucene.analysis.Token;
    import org.apache.lucene.analysis.TokenStream;
    import org.apache.lucene.analysis.standard.StandardAnalyzer;

    public class StandardAnalyzerTest 
    {
        
    //構(gòu)造函數(shù),
        public StandardAnalyzerTest()
        
    {
        }

        
    public static void main(String[] args) 
        
    {
            
    //生成一個(gè)StandardAnalyzer對(duì)象
            Analyzer aAnalyzer = new StandardAnalyzer();
            
    //測(cè)試字符串
            StringReader sr = new StringReader("lighter javaeye com is the are on");
            
    //生成TokenStream對(duì)象
            TokenStream ts = aAnalyzer.tokenStream("name", sr);    
            
    try {
                
    int i=0;
                Token t 
    = ts.next();
                
    while(t!=null)
                
    {
                    
    //輔助輸出時(shí)顯示行號(hào)
                    i++;
                    
    //輸出處理后的字符
                    System.out.println(""+i+"行:"+t.termText());
                    
    //取得下一個(gè)字符
                    t=ts.next();
                }

            }
     catch (IOException e) {
                e.printStackTrace();
            }

        }

    }


    顯示結(jié)果:
    第1行:lighter 
    第2行:javaeye 
    第3行:com

    提示一下:
    StandardAnalyzer是lucene中內(nèi)置的"標(biāo)準(zhǔn)分析器",可以做如下功能:
    1、對(duì)原有句子按照空格進(jìn)行了分詞
    2、所有的大寫字母都可以能轉(zhuǎn)換為小寫的字母
    3、可以去掉一些沒(méi)有用處的單詞,例如"is","the","are"等單詞,也刪除了所有的標(biāo)點(diǎn)
    查看一下結(jié)果與"new StringReader("lighter javaeye com is the are on")"作一個(gè)比較就清楚明了。
    這里不對(duì)其API進(jìn)行解釋了,具體見(jiàn)lucene的官方文檔。需要注意一點(diǎn),這里的代碼使用的是lucene2的API,與1.43版有一些明顯的差別。

    2、看另一個(gè)實(shí)例,簡(jiǎn)單地建立索引,進(jìn)行搜索
    java代碼:
    package lighter.javaeye.com;
    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 org.apache.lucene.queryParser.QueryParser;
    import org.apache.lucene.search.Hits;
    import org.apache.lucene.search.IndexSearcher;
    import org.apache.lucene.search.Query;
    import org.apache.lucene.store.FSDirectory;

    public class FSDirectoryTest {

        
    //建立索引的路徑
        public static final String path = "c:\\index2";

        
    public static void main(String[] args) throws Exception {
            Document doc1 
    = new Document();
            doc1.add( 
    new Field("name""lighter javaeye com",Field.Store.YES,Field.Index.TOKENIZED));

            Document doc2 
    = new Document();
            doc2.add(
    new Field("name""lighter blog",Field.Store.YES,Field.Index.TOKENIZED));

            IndexWriter writer 
    = new IndexWriter(FSDirectory.getDirectory(path, true), new StandardAnalyzer(), true);
            writer.setMaxFieldLength(
    3);
            writer.addDocument(doc1);
            writer.setMaxFieldLength(
    3);
            writer.addDocument(doc2);
            writer.close();

            IndexSearcher searcher 
    = new IndexSearcher(path);
            Hits hits 
    = null;
            Query query 
    = null;
            QueryParser qp 
    = new QueryParser("name",new StandardAnalyzer());
            
            query 
    = qp.parse("lighter");
            hits 
    = searcher.search(query);
            System.out.println(
    "查找\"lighter\" 共" + hits.length() + "個(gè)結(jié)果");

            query 
    = qp.parse("javaeye");
            hits 
    = searcher.search(query);
            System.out.println(
    "查找\"javaeye\" 共" + hits.length() + "個(gè)結(jié)果");

        }


    }

    運(yùn)行結(jié)果:

    查找"lighter" 共2個(gè)結(jié)果
    查找
    "javaeye" 共1個(gè)結(jié)果

    posted on 2008-03-10 11:04 lk 閱讀(419) 評(píng)論(0)  編輯  收藏 所屬分類: j2se
    主站蜘蛛池模板: 免费在线看v网址| 91大神亚洲影视在线| 免费的涩涩视频在线播放| 99久久久国产精品免费牛牛四川| 国产精品亚洲精品爽爽| 亚洲一区在线免费观看| 亚洲AV天天做在线观看| 久久精品国产精品亚洲| 午夜神器成在线人成在线人免费| 亚洲成人免费电影| 毛片无码免费无码播放| 久久99精品国产免费观看| 成全视频高清免费观看电视剧| 人妻无码中文字幕免费视频蜜桃 | 99999久久久久久亚洲| 亚洲伊人久久大香线蕉苏妲己| 亚洲国产精品无码久久久秋霞2| 国产精品亚洲综合一区| 亚洲国产精品丝袜在线观看| 又黄又大又爽免费视频| 亚洲日韩国产一区二区三区| 又粗又黄又猛又爽大片免费| 免费久久精品国产片香蕉| 国产乱人免费视频| 亚洲成aⅴ人片久青草影院| 中文字幕亚洲专区| 亚洲av之男人的天堂网站| 久久精品亚洲中文字幕无码麻豆| 亚洲免费视频在线观看| 亚洲一区二区影视| 色偷偷亚洲男人天堂| 黄 色一级 成 人网站免费| 国内精品久久久久影院免费| 最近中文字幕2019高清免费| 三年片在线观看免费观看高清电影| 午夜视频免费观看| 最新国产AV无码专区亚洲| 亚洲小视频在线观看| 亚洲熟女www一区二区三区| 五级黄18以上免费看| 最近中文字幕完整免费视频ww |