接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 大家還可以試一下別的,以便加深一下印象 (未完) |
|
來自: ShangShujie > 《searchEngine》