请高手进来帮助解决lucene编程问题

slg_sparkler 2007-02-02 10:32:34
我用的lucene版本是2.0,程序的输出结果为:
查找 : word1
总共找到 2 个文档
=============================
文档的内部ID号:0
path:null
=============================
文档的内部ID号:1
path:null

我想知道为什么path的值始终为null呢! 网上搜索了很久没有找到解决办法。
代码如下:

public class LuceneTest {

public static void buildIndex() throws Exception {
Document doc1 = new Document();
doc1.add(new Field("contents", new StringReader("word1 word")));
doc1.add(new Field("path", new StringReader("path\\document1.txt")));

Document doc2 = new Document();
doc2.add(new Field("contents", new StringReader("word2 word1")));
doc2.add(new Field("path", new StringReader("path\\document2.txt")));

IndexWriter writer = new IndexWriter("c:\\index",
new StandardAnalyzer(), true);

writer.addDocument(doc1);
writer.addDocument(doc2);

writer.close();
}

public static void main(String[] args) throws Exception {
// 构建索引
buildIndex();

// 使用已经存在索引目录
Searcher searcher = new IndexSearcher("c:\\index");
// 使用标准分析器
Analyzer aStandardAnalyzer = new StandardAnalyzer();
// 从标准输入读取查询的字符串
QueryParser parser = new QueryParser("contents", aStandardAnalyzer);
String line = "word1";
Query query = parser.parse(line);

// 输出要搜索的内容
System.out.println("查找 : " + query.toString("contents"));
// 使用searcher对象的search方法进行搜索,返回的是一个Hits类型的对象
Hits hits = searcher.search(query);
// 使用Hits对象的length()方法,输出搜索到的文档的数量
System.out.println("总共找到 " + hits.length() + " 个文档");
for (int i = 0; i < hits.length(); i++) {
Document doc = hits.doc(i);
System.out.println("=============================");
// 输出文档的ID编号
System.out.println("文档的内部ID号:" + hits.id(i));
// 输出文档的存放路径
String path = doc.get("path");
System.out.println("path:" + path);

}

searcher.close();
}

}
...全文
214 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
shanhe 2007-03-13
  • 打赏
  • 举报
回复
搂主理解错了
slg_sparkler 2007-03-03
  • 打赏
  • 举报
回复
To buffon001

你的写法在2.0中好像不对吧,跟本没有你说的那种重载创建函数。

高手指点一下吧!
buffon001 2007-02-06
  • 打赏
  • 举报
回复
doc1.add(new Field("contents", new StringReader("word1 word")));
doc1.add(new Field("path", new StringReader("path\\document1.txt")));
这里这样写
doc1.add(new Field("contents", "word1 word"));
doc1.add(new Field("path", "path\\document1.txt"));

2,759

社区成员

发帖
与我相关
我的任务
社区描述
搜索引擎的服务器通过网络搜索软件或网络登录等方式,将Internet上大量网站的页面信息收集到本地,经过加工处理建立信息数据库和索引数据库。
社区管理员
  • 搜索引擎技术社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧