2013-03-30 72 views
0

要在Lucene索引中搜索,我通過JSP中的Web用戶界面捕獲了用戶查詢。在JSP中,我編寫了簡短的JAVA代碼來解析查詢並調用Lucene索引搜索器來搜索查詢。但問題在於它反覆出現編譯錯誤,因爲「查詢無法解析,MultiFieldQueryParser無法解析...」。所以沒有一個Lucene類得到解決。代碼如下:從JSP中的外部JAR文件導入類

文件名:result.jsp中

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" import="java.util.*,java.io.*,org.apache.lucene.*" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<%! String query; %> 
<% 
    query=request.getParameter("myQuery"); 
%> 
<form name="frm" method="post" action="result.jsp"> 
<table width="100%" border="0" cellspacing="0" cellpadding="0"> 
<tr> 
<td width="22%">&nbsp;</td> 
<td width="78%">&nbsp;</td> 
</tr> 
<tr> 
<td>&nbsp; </td> 
<td><input type="text" name="myQuery" placeholder="Type here"></td> 
</tr> 
<tr> 
<td>&nbsp;</td> 
<td><input type="submit" name="submit" value="Submit"></td> 
</tr> 
<tr> 
<td>&nbsp;</td> 
<td>&nbsp;</td> 
</tr> 
</table> 
</form> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
<title>Insert title here</title> 
</head> 
<body> 
<% 
    Directory dir=new FSDirectory.open(new File(path of index directory)); 
    QueryParser parser=new MultiFieldQueryParser(Version.LUCENE_30, new String[] 
    {"title","address","city"},new BooleanClause.Occur[]{BooleanClause.Occur.MUST, 
    BooleanClause.Occur.SHOULD, BooleanCaluse.Occur.SHOULD},new StandardAnalyzer()); 
    Query query=parser.parse(query); 
    IndexSearcher searcher=new Indexsearcher(dir,true); 
    TopDocs hits=searcher.search(query,20); 
    searcher.close(); 
    dir.close(); 
%> 
<p>Query phrase is : <%=query%></p> 
</body> 
</html> 

我不明白爲什麼任何的Lucene的類都沒有得到甚至高於進口的Lucene後解決。所以我在問是否有人可以幫我解決上面的代碼。謝謝。

+0

[Java導入混淆]的可能重複(http://stackoverflow.com/questions/1335327/java-import-confusion) – femtoRgon

回答

2

經驗法則:導入abc.xyz。*將只導入包abc.xyz中的所有類,但不導入包abc.xyz.ijk中的類。

例: 「org.apache.lucene *」 將不導入 「org.apache.lucene.store.Directory」

要解決上述錯誤,進口下列包/班。

1) 「org.apache.lucene.store.Directory」 或 「org.apache.lucene.store。*」

2) 「org.apache.lucene.queryParser.QueryParser」

3) 「org.apache.lucene.queryParser.MultiFieldQueryParser」

4) 「org.apache.lucene.search.BooleanClause」

5) 「則把org.apache.lucene.analysis.standard.StandardAnalyzer」

6)org.apache.lucene.search.TopDocs

7)org.apache.lucene.search.IndexSearcher

我可能會錯過一個或兩個類。只需按照「經驗法則」並使用全長程序包名稱導入丟失的類。

順便說一下,NEVER use scriptlets in a JSP,即JSP中的直接Java代碼。這是一個糟糕的做法。

你可以找到其他資源來更好地理解它。

+0

我明白,不應該在JSP內部使用scriptlet。許多人建議使用JSTL而不是使用scriptlet。但在上述情況下,我不知道如何使用JSTL來避免scriptlet。所以我問是否有人可以請一個想法來處理它。謝謝。 – Joy

+0

@Joy,那會是一個不同的問題。用您的查詢發佈另一個問題,您肯定會在評論中得到比這裏更好,更全面的答案.. – phani

+0

謝謝主席先生。 – Joy