Lucene-2.0学习文档(2)
接http://www.iteye.com/post/190334
IndexWriter(File path, Analyzer a, boolean create)
IndexWriter(String path, Analyzer a, boolean create)
可见构造它需要一个索引文件目录,一个分析器(一般用标准的这个),最后一个参数是标识是否清空索引目录
它有一些设置参数的功能如:设置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开头和数组一样。
程序员之家----------3--------程序员之
??????????????????????????????????? 0 1 2? 3
Welcome to the home of programmers------3------Welcome to the home of programmers
?????????????????????????????????????????????????? 0?????????? 1???????? 2
大家还可以试一下别的,以便加深一下印象
(未完) 1 楼 LucasLee 2006-12-21 提个题外话,Javaeye怎么搞了新的文本编辑器,搞得代码又这么长,每行后面都有空行,看得太不紧凑了。 2 楼 ouspec 2006-12-22 Lucas Lee 写道提个题外话,Javaeye怎么搞了新的文本编辑器,搞得代码又这么长,每行后面都有空行,看得太不紧凑了。
这个和JavaEye的代码编辑没有关系。发贴没有用代码输入。