首页 新闻 会员 周边 捐助

Lucene4.4为什么总是搜索不到内容呢?在线急求解答..

0
[待解决问题]

这个是创建的Field:

private String[] ids = { "1", "2", "3", "4", "5" };
private String[] name = { "Jake", "Sally", "Sara", "Lady", "Bella" };
private String[] email = { "asdfd@sina.com", "sadf@qq.com",
"fdsgjdf@gmail.com", "fas@gmail.com", "dsbi@sina.com" };
private String[] content = { "I like you and like my baby",
"sara is a girl", "I like java football", "The weather is sunny",
"He is cool" };
Directory dir=null;

下面是创建的索引:

public void index() {
IndexWriter iwriter = null;
try {
dir = FSDirectory.open(new File(
FirstLucene.FILE_INDEX_PATH));
iwriter = new IndexWriter(dir, new IndexWriterConfig(
Version.LUCENE_44, new StandardAnalyzer(Version.LUCENE_44)));
// 先清除所有的索引
iwriter.deleteAll();
Document doc = null;
for (int i = 0; i < ids.length; i++) {
doc = new Document();
doc.add(new StringField("id", ids[i], Field.Store.YES));
doc.add(new StringField("name", name[i], Field.Store.YES));
doc.add(new StringField("content", content[i], Field.Store.YES));
doc.add(new StringField("email", email[i], Field.Store.YES));
iwriter.addDocument(doc);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(iwriter!=null){
try {
iwriter.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

}

下面是创建的查询:

public void query(){
DirectoryReader reader=null;
try {
reader=DirectoryReader.open(dir);
IndexSearcher searcher=new IndexSearcher(reader);
String field="content";
QueryParser parser=new QueryParser(Version.LUCENE_44,field,new StandardAnalyzer(Version.LUCENE_44));
Query query=parser.parse("cool");
//TermQuery query=new TermQuery(new Term("email","asdfd@sina.com"));
TopDocs topDoc=searcher.search(query, 10);
ScoreDoc[] scoreDocs=topDoc.scoreDocs;
System.out.println("搜到的结果总数 :"+scoreDocs.length);
for(ScoreDoc scoreDoc:scoreDocs){
Document doc=searcher.doc(scoreDoc.doc);
System.out.println("id:"+doc.get("id"));
System.out.println("name:"+doc.get("name"));
System.out.println("content:"+doc.get("content"));
System.out.println("email:"+doc.get("email"));

}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(reader!=null){
try {
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

为什么总是查询不到,但是如果把查询的Field改为id,然后查询id=1的话就可以的,不晓得这个是为什么?

Lion_的主页 Lion_ | 菜鸟二级 | 园豆:204
提问于:2013-09-06 19:57
< >
分享
所有回答(1)
0

建议给代码加上着色,点击编辑器中倒数第2个按钮

dudu | 园豆:30925 (高人七级) | 2013-09-06 21:25

编辑器倒数第二个按钮?我用的Myeclipse,哪个是倒数第二个按钮?

支持(0) 反对(0) Lion_ | 园豆:204 (菜鸟二级) | 2013-09-07 09:13

如果使用这个类TermQuery来查询id和name的话都可以查到,但是就是content无论怎么也查不到,不晓得这个是不是Lucene4.4哪里改动了么

支持(0) 反对(0) Lion_ | 园豆:204 (菜鸟二级) | 2013-09-07 10:03
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册