为何同样的代码,却不同的结果。。。

jsnjlc 2008-08-05
这个问题我已经被困1个星期了,真是欲哭无泪。。。
我用的是lucene2.3.2。里面有个demo,我准备仿照它将其改造成struts架构的,没想到噩梦开始。。。
    我分别写了2个类,一个类是用main方法生成索引文件。另一个类则是查询生成的索引文件。写了个main方法测试了下生成索引文件的类,通过。下面就开始搭建成struts下的了。action写好了,配置好了,运行——D:\opt\lucene\index\segments (系统找不到指定的文件。)。指错了?没有啊。文件确实在这个文件夹啊。我将那个demo跑起来运行,没有报错啊。。。
    我又将我的代码全部换成了demo中的代码,还是一样。我与demo之间的区别只是demo是将代码写jsp中,我写在了bean中而已。
    我将demo中的jsp代码贴出来,我的代码也贴出来。。。
   
<%@ page
	import="javax.servlet.*,javax.servlet.http.*,java.io.*,org.apache.lucene.analysis.*,org.apache.lucene.analysis.standard.StandardAnalyzer,org.apache.lucene.document.*,org.apache.lucene.index.*,org.apache.lucene.search.*,org.apache.lucene.queryParser.*,org.apache.lucene.demo.*,org.apache.lucene.demo.html.Entities,java.net.URLEncoder"%>

<%
	/*
	Author: Andrew C. Oliver, SuperLink Software, Inc. (acoliver2@users.sourceforge.net)

	This jsp page is deliberatly written in the horrible java directly embedded 
	in the page style for an easy and concise demonstration of Lucene.
	Due note...if you write pages that look like this...sooner or later
	you'll have a maintenance nightmare.  If you use jsps...use taglibs
	and beans!  That being said, this should be acceptable for a small
	page demonstrating how one uses Lucene in a web app. 

	This is also deliberately overcommented. ;-)

	 */
%>
<%!public String escapeHTML(String s) {
		s = s.replaceAll("&", "&amp;");
		s = s.replaceAll("<", "&lt;");
		s = s.replaceAll(">", "&gt;");
		s = s.replaceAll("\"", "&quot;");
		s = s.replaceAll("'", "&apos;");
		return s;
	}%>
<%@include file="header.jsp"%>
<%
	boolean error = false; //used to control flow for error messages
	String indexName = indexLocation; //local copy of the configuration variable
	IndexSearcher searcher = null; //the searcher used to open/search the index
	Query query = null; //the Query created by the QueryParser
	Hits hits = null; //the search results
	int startindex = 0; //the first index displayed on this page
	int maxpage = 50; //the maximum items displayed on this page
	String queryString = null; //the query entered in the previous page
	String startVal = null; //string version of startindex
	String maxresults = null; //string version of maxpage
	int thispage = 0; //used for the for/next either maxpage or
	//hits.length() - startindex - whichever is
	//less

	try {
		searcher = new IndexSearcher("/opt/lucene/index"); //create an indexSearcher for our page
		//NOTE: this operation is slow for large
		//indices (much slower than the search itself)
		//so you might want to keep an IndexSearcher 
		//open

	} catch (Exception e) { //any error that happens is probably due
		//to a permission problem or non-existant
		//or otherwise corrupt index
		System.out.println(e.getMessage());
%>
<p>
	ERROR opening the Index - contact sysadmin!
</p>
<p>
	Error message:
	<%=escapeHTML(e.getMessage())%></p>
<%
	error = true; //don't do anything up to the footer
	}
%>
    
我只贴上面一部分,因为我只需要到这里。。。下面是我的bean的代码。。。希望各位大虾能够帮帮我,下个项目越来越近了。。。我真怕来不及。。。
package lucene;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

public class TestSearch {
	public List search(String queryString) throws IOException, ParseException {
//		Hits hits = null;
//		Query query = null;
//		IndexSearcher searcher = new IndexSearcher("c:\\ccc");

//		IndexReader.getCurrentVersion(new File("c:"+File.separatorChar+"ccc"));
//		IndexSearcher searcher = new IndexSearcher("c:\\ccc");
		Directory directory =null;
//		IndexReader ir = null;
//		try {
//			File file = new File("e:"+File.separatorChar+"ccc");
//			directory= FSDirectory.getDirectory(file,true);
//			ir = IndexReader.open("ccc");
//			
//			
//			
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
		
		
		
		boolean error = false; //used to control flow for error messages
//		String indexName = indexLocation; //local copy of the configuration variable
		IndexSearcher searcher = null; //the searcher used to open/search the index
		Query query = null; //the Query created by the QueryParser
		Hits hits = null; //the search results
		int startindex = 0; //the first index displayed on this page
		int maxpage = 50; //the maximum items displayed on this page
//		String queryString = null; //the query entered in the previous page
		String startVal = null; //string version of startindex
		String maxresults = null; //string version of maxpage
		int thispage = 0; //used for the for/next either maxpage or
		//hits.length() - startindex - whichever is
		//less

		try {
			searcher = new IndexSearcher("/opt/lucene/index"); //create an indexSearcher for our page
			//NOTE: this operation is slow for large
			//indices (much slower than the search itself)
			//so you might want to keep an IndexSearcher 
			//open

		} catch (Exception e) { //any error that happens is probably due
			//to a permission problem or non-existant
			//or otherwise corrupt index
			System.out.println(e.getMessage());
		}
		
		
		
		
		
		
		
//		IndexSearcher searcher = new IndexSearcher("/opt/lucene/index");

		
		
		
		
		Analyzer analyzer = new StandardAnalyzer();
		try {
			QueryParser qp = new QueryParser("desc", analyzer);
			query = qp.parse(queryString);
		} catch (ParseException e) {
		}
		if (searcher != null) {
			hits = searcher.search(query);
			
			List list = null;
			if (hits.length() > 0) {
				System.out.println("找到:" + hits.length() + " 个结果!");
				list = new ArrayList();
				Form form = null;
				for(int i=0;i<hits.length();i++){
					form = new Form();
//					System.out.print(hits.doc(i).get("name")+"\t\t");
//					System.out.println(hits.doc(i).get("desc"));
					form.setName(hits.doc(i).get("name"));
					form.setDesc(hits.doc(i).get("desc"));
					list.add(form);
				}
				return list;
			}
			else{
				System.out.println("没有找到任何结果!");
				return null;
			}
		}
		return null;
	}

}
zhanjianhua 2008-08-05
试试将/opt/lucene/index改成d:/opt/lucene/index
jsnjlc 2008-08-05
没有用的,我已经试过很多次了。
我将另外一个网站的demo下载下来了。然后将索引文件指向我的索引文件还是说文件找不到。。。我疯了。。。
http://www.ibm.com/developerworks/cn/web/wa-lucene2/#download
jsnjlc 2008-08-05
问题解决。。。jar包问题。。。换成了demo里面的jar包就OK了。。。汗。。。
Global site tag (gtag.js) - Google Analytics