原创作者: likunkun
阅读:1707次
评论:0条
更新时间:2011-05-26
接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
大家还可以试一下别的,以便加深一下印象
(未完)
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
大家还可以试一下别的,以便加深一下印象
(未完)
评论 共 0 条 请登录后发表评论