日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

Lucene-2.0學習文檔(2) - 企業(yè)應用 - Java - JavaEye論壇

 ShangShujie 2009-03-18
接http://www./post/190334
IndexWriter(File path, Analyzer a, boolean create)
IndexWriter(String path, Analyzer a, boolean create)
可見構造它需要一個索引文件目錄,一個分析器(一般用標準的這個),最后一個參數(shù)是標識是否清空索引目錄
它有一些設置參數(shù)的功能如:設置Field的最大長度
看個例子:
[code]
public void IndexMaxField() throws IOException
{
        IndexWriter indexWriter= new IndexWriter("c:\\index",new StandardAnalyzer(),true);
        Document doc1 = new Document();
        doc1.add(new Field("name1","程序員之家",Field.Store.YES,Field.Index.TOKENIZED));
        Document doc2 = new Document();
        doc2.add(new Field("name2","Welcome to the Home of programers",Field.Store.YES,Field.Index.TOKENIZED));
        indexWriter.setMaxFieldLength(5);
        indexWriter.addDocument(doc1);
        indexWriter.setMaxFieldLength(3);
        indexWriter.addDocument(doc1);
        indexWriter.setMaxFieldLength(0);
        indexWriter.addDocument(doc2);
        indexWriter.setMaxFieldLength(3);
        indexWriter.addDocument(doc2);
        indexWriter.close();
}
public void SearcherMaxField() throws ParseException, IOException
{
        Query query = null;
        Hits hits = null;
        IndexSearcher indexSearcher= null;
        QueryParser queryParser= null;
        queryParser = new QueryParser("name1",new StandardAnalyzer());
        query = queryParser.parse("程序員");
        indexSearcher= new IndexSearcher("c:\\index");
        hits = indexSearcher.search(query);
        System.out.println("您搜的是:程序員");
        System.out.println("找到了"+hits.length()+"個結果");
        System.out.println("它們分別是:");
        for (int i = 0; i < hits.length(); i++)
        {
            Document doc = hits.doc(i);
            System.out.println(doc.get("name1"));
        }
        query = queryParser.parse("程序員之家");
        indexSearcher= new IndexSearcher("c:\\index");
        hits = indexSearcher.search(query);
        System.out.println("您搜的是:程序員之家");
        System.out.println("找到了"+hits.length()+"個結果");
        System.out.println("它們分別是:");
        for (int i = 0; i < hits.length(); i++)
        {
            Document doc = hits.doc(i);
            System.out.println(doc.get("name1"));
        }
        queryParser = new QueryParser("name2",new StandardAnalyzer());
        query = queryParser.parse("Welcome");
        indexSearcher= new IndexSearcher("c:\\index");
        hits = indexSearcher.search(query);
        System.out.println("您搜的是:Welcome");
        System.out.println("找到了"+hits.length()+"個結果");
        System.out.println("它們分別是:");
        for (int i = 0; i < hits.length(); i++)
        {
            Document doc = hits.doc(i);
            System.out.println(doc.get("name2"));
        }           
        query = queryParser.parse("the");
        indexSearcher= new IndexSearcher("c:\\index");
        hits = indexSearcher.search(query);
        System.out.println("您搜的是:the");
        System.out.println("找到了"+hits.length()+"個結果");
        System.out.println("它們分別是:");
        for (int i = 0; i < hits.length(); i++)
        {
            Document doc = hits.doc(i);
            System.out.println(doc.get("name2"));
        }
        query = queryParser.parse("home");
        indexSearcher= new IndexSearcher("c:\\index");
        hits = indexSearcher.search(query);
        System.out.println("您搜的是:home");
        System.out.println("找到了"+hits.length()+"個結果");
        System.out.println("它們分別是:");
        for (int i = 0; i < hits.length(); i++)
        {
            Document doc = hits.doc(i);
            System.out.println(doc.get("name2"));
        }
}
[/code]
它的運行結果為:
總結一下:
1.設置Field的長度限制只是限制了搜索。如果用了Field.Store.YES的話還是會
全部被保存進索引目錄里的。
2.為什么搜the沒有搜出來呢是因為lucene分析英文的時候不會搜索the to of 等無
用的詞(搜這些詞是無意義的)。
3.New StandardAnlayzer()對于英文的分詞是按空格和一些無用的詞,而中文呢是全部的單個
的字。
4.設置Field的最大長度是以0開頭和數(shù)組一樣。
程序員之家----------3--------程序員之
                                    0 1 2  3
Welcome to the home of programmers------3------Welcome to the home of programmers

                                                   0           1         2
大家還可以試一下別的,以便加深一下印象
(未完)

    本站是提供個人知識管理的網絡存儲空間,所有內容均由用戶發(fā)布,不代表本站觀點。請注意甄別內容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權內容,請點擊一鍵舉報。
    轉藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多