`
jushi1988
  • 浏览: 74431 次
  • 性别: Icon_minigender_1
  • 来自: 黑龙江
社区版块
存档分类
最新评论

Lucene与DB结合示例

阅读更多

   ---  2010-06-18 17:00 代码修改: 修改重复创建索引,添加双字段高亮。

 

   今天发一个简单的和数据库交互的Lucene示例,只是初步的靠Lucene自带的分词实现中文分词,效果肯定没有网上琳琅的分词器相媲美,只为了示例,用了自带的高亮。页面不好看,将就一下哦。

    主要是由 Spring + Struts1 + Mysql 5 实现, 只是为了实现功能。请各位大侠拍砖。

    好了,不多说了。贴代码:

 

    1. T_ARTICLE 表

DROP TABLE IF EXISTS `t_article`;
CREATE TABLE `t_article` (
  `ID` varchar(32) NOT NULL default '',
  `ARTICLE_TITLE` varchar(255) default NULL,
  `ARTICLE_TAG` varchar(255) default NULL,
  `ARTICLE_CONTENT` text,
  PRIMARY KEY  (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

    上面的数据 随便加一下吧,主要是article_content和article_title字段的数据,article_content是个text字段,所以你可以粘贴网页的文字内容添加到本字段中。

 

    2.业务接口 ArticleService.java

 

package com.jushi.lucene.business;

import java.util.List;
import com.jushi.lucene.entities.Article;
/**
 * @Author jushi
 * @CreateDate Jun 4, 2010 1:24:42 PM
 * @Version V 1.0
 */

public interface IArticleService {

	public List<Article> getArticles(String query);

	public boolean createIndex();

}


   3. 接口的实现 ArticleServiceImpl.java 主要的业务在这里实现:创建索引,搜索数据处理。

 

package com.jushi.lucene.business;

import java.io.File;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.queryParser.MultiFieldQueryParser;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.Filter;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.search.highlight.Highlighter;
import org.apache.lucene.search.highlight.QueryScorer;
import org.apache.lucene.search.highlight.SimpleFragmenter;
import org.apache.lucene.search.highlight.SimpleHTMLFormatter;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

import com.jushi.lucene.dao.IArticleDAO;
import com.jushi.lucene.entities.Article;

/**
 * @Author jushi
 * @CreateDate Jun 4, 2010 1:25:00 PM
 * @Version V 1.0
 */
public class ArticleServiceImpl implements IArticleService {
	
	private IArticleDAO articleDAO;
	
	private final String INDEXPATH = "g:\\index";
	private Analyzer analyzer = new StandardAnalyzer();
	
	public List<Article> getArticles(String query) {
		
		try{
		List<Article> qlist = new ArrayList<Article>();
		String fieldName = "title";
		IndexSearcher indexSearcher = new IndexSearcher(INDEXPATH);
		
		//QueryParser parser = new QueryParser(fieldName, analyzer); //单 key 搜索
		//Query queryOBJ = parser.parse(query);
		System.out.println(">>> 2.开始读取索引... ... 通过关键字:【 "+ query +" 】");
		long begin = new Date().getTime();
		
		//下面的是进行title,content 两个范围内进行收索.
		BooleanClause.Occur[] clauses = { BooleanClause.Occur.SHOULD,BooleanClause.Occur.SHOULD };
		Query queryOBJ = MultiFieldQueryParser.parse(query, new String[]{"title","content"}, clauses, new StandardAnalyzer());//parser.parse(query);
		Filter filter = null;
		
		//################# 搜索相似度最高的记录 ###################
		TopDocs topDocs = indexSearcher.search(queryOBJ, filter, 1000);
		//TopDocs topDocs = indexSearcher.search(queryOBJ , 10000);
		System.out.println("*** 共匹配:" + topDocs.totalHits + "个 ***");
		
		Article article = null;
		
		//输出结果
		for (ScoreDoc scoreDoc : topDocs.scoreDocs){
				Document targetDoc = indexSearcher.doc(scoreDoc.doc);
				article = new Article();
				
				//设置高亮显示格式
				SimpleHTMLFormatter simpleHTMLFormatter = new SimpleHTMLFormatter("<font color='red'><strong>", "</strong></font>"); 
				/* 语法高亮显示设置 */
				Highlighter highlighter = new Highlighter(simpleHTMLFormatter,new QueryScorer(queryOBJ)); 
				highlighter.setTextFragmenter(new SimpleFragmenter(100)); 
				
				// 设置高亮 设置 title,content 字段
				String title = targetDoc.get("title");
				String content = targetDoc.get("content");
				TokenStream titleTokenStream = analyzer.tokenStream(fieldName,new StringReader(title));
				TokenStream contentTokenStream = analyzer.tokenStream("content",new StringReader(content));
				String highLightTitle = highlighter.getBestFragment(titleTokenStream, title);
				String highLightContent = highlighter.getBestFragment(contentTokenStream, content);
				
			     if(highLightTitle == null)
			    	 highLightTitle = title;

			     if(highLightContent == null) 
			    	 highLightContent = content;
			     
			    article.setTitle(highLightTitle);
				article.setContent(highLightContent);
				article.setTag(targetDoc.get("tag"));
				article.setTotalHits(topDocs.totalHits);
				
				qlist.add(article);
		}
		
		long end = new Date().getTime();
		System.out.println(">>> 3.搜索完毕... ... 共花费:" + (end - begin) +"毫秒...");
		
		indexSearcher.close();
		
		return qlist;
		
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
	
	public boolean createIndex()
	{
		//检查索引是否存在
		if(this.isIndexExisted())
			return this.isIndexExisted();
		
		List<Article> list = articleDAO.getArticles();
		
		try
		{
			Directory directory = FSDirectory.getDirectory(INDEXPATH);
			IndexWriter indexWriter = new IndexWriter(directory, analyzer ,true, IndexWriter.MaxFieldLength.LIMITED);
			
			long begin = new Date().getTime();
			for(Article art: list)
			{
				Document doc = new Document();
				String title = art.getTitle() == null ? "" : art.getTitle().trim();
				String content = art.getContent() == null ? "" : art.getContent();
				String tag = art.getTag() == null ? "" : art.getTag();
				doc.add(new Field("title", title, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.YES));
				doc.add(new Field("content", content, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.YES));
				doc.add(new Field("tag", tag, Field.Store.COMPRESS,Field.Index.NO));
				indexWriter.addDocument(doc);
			}
			long end = new Date().getTime();
			System.out.println(">>> 1.存入索引完毕.. 共花费:" + (end - begin) +"毫秒...");
			
			indexWriter.optimize();
			indexWriter.close();
			return true;
			
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}
	
	/**
	 * check Index is Existed
	 * @return true or false
	 */
	private boolean isIndexExisted()
	{
		try
		{
			File dir = new File(INDEXPATH);
			if(dir.listFiles().length>0)
				return true;
			else
				return false;
			
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}
	
	public void setArticleDAO(IArticleDAO articleDAO) {
		this.articleDAO = articleDAO;
	}

}

    4. DAO 接口及 DAO实现 

 

public interface IArticleDAO {

	public List<Article> getArticles();
}

 

 

public class ArticleDAOImpl extends JdbcDaoSupport implements IArticleDAO{

	public List<Article> getArticles() {
		String sql = "SELECT article_title, article_tag, article_content FROM t_article";
		return (List<Article>)this.getJdbcTemplate().query(sql, new ArticleRowMapper());
	}

}

 

   5. 上面DAO需要的 ArticleRowMapper: 这里做了下小改动

 

package com.jushi.lucene.rowmapper;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;

import org.springframework.jdbc.core.RowMapper;

import com.jushi.lucene.entities.Article;

/**
 * @Author jushi
 * @CreateDate Jun 4, 2010 1:31:54 PM
 * @Version V 1.0
 */
public class ArticleRowMapper implements RowMapper {

	public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
		Article article = new Article();
		ResultSetMetaData meta = rs.getMetaData();
		int colNum = meta.getColumnCount();
		for (int i = 1; i <= colNum; i++) {
			String colName = meta.getColumnName(i).toLowerCase();
			if ("article_title".equals(colName)) {
				article.setTitle(rs.getString(i));
			} else if ("article_content".equals(colName)) {
				article.setContent(rs.getString(i));
			} else if ("article_tag".equals(colName)) {
				article.setTag(rs.getString(i));
				//...
			}
		}
		return article;
	}
}

 

   6. Article 实体 添加了两个属性: TotalHits, TotalTime

 

package com.jushi.lucene.entities;

/**
 * @Author jushi
 * @CreateDate Jun 4, 2010 1:18:48 PM
 * @Version V 1.0
 */
public class Article {

	private String title;
	private String content;
	private String tag;
	private int TotalHits; //命中率
	private int TotalTime; //花费时间
	
	public int getTotalTime() {
		return TotalTime;
	}
	public void setTotalTime(int totalTime) {
		TotalTime = totalTime;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	public String getTag() {
		return tag;
	}
	public void setTag(String tag) {
		this.tag = tag;
	}
	public int getTotalHits() {
		return TotalHits;
	}
	public void setTotalHits(int totalHits) {
		TotalHits = totalHits;
	}
	
}

 

   7. Spring 的配置如下:

   --- applicationContext-lucene.xml

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>

<bean id="articleDAO" class="com.jushi.lucene.dao.ArticleDAOImpl" scope="singleton" >  
	<property name="dataSource" ref="articleDataSource"></property>  
</bean>

<bean id="articleService" class="com.jushi.lucene.business.ArticleServiceImpl" scope="singleton" >  
	<property name="articleDAO" ref="articleDAO"></property> 
</bean> 
  
</beans> 

   ---  applicationContext-lucene-datasource.xml

 

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>

<bean id="articleDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
	<property name="driverClassName" value="com.mysql.jdbc.Driver" ></property>
	<property name="url" value="jdbc:mysql://localhost/companyweb?useUnicode=true&amp;characterEncoding=utf-8" />
	<property name="username" value="root" />
	<property name="password" value="jushi" />
	<property name="initialSize" value="5" />
	<property name="maxActive" value="20" />
	<property name="maxIdle" value="20" />
	<property name="minIdle" value="5" />
</bean> 

</beans> 

 

   8. action配置 struts-config.xml

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">

<struts-config>

	<form-beans>
	   <form-bean name="lazyForm" type="org.apache.struts.validator.LazyValidatorForm"/>
    </form-beans>
    <action-mappings>
    
    	<action path="/index"
	            type="com.jushi.lucene.businessview.IndexAction"
	            name="lazyForm" parameter="m" scope="request" validate="false">
				<forward name="result" path="/result.jsp"></forward>
		</action>

	</action-mappings>
</struts-config>

 

    9. web.xml 文件配置: 监听、加载配置文件

 

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
	
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath*:/springConfig/applicationContext*.xml</param-value>
	</context-param>
	
	<!--  Character Encoding filter -->
	<filter>
          <filter-name>encodingFilter</filter-name>
          <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
          <init-param>
                   <param-name>encoding</param-name>
                   <param-value>UTF-8</param-value>
          </init-param>
     </filter>
     <filter-mapping>
          <filter-name>encodingFilter</filter-name>
          <url-pattern>/*</url-pattern>
     </filter-mapping>
     
     <listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	
	<servlet>
		<servlet-name>action</servlet-name>
		<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
		<init-param>
			<param-name>config</param-name>
			<param-value>/WEB-INF/struts-config.xml</param-value>
		</init-param>
	</servlet>

	<servlet-mapping>
		<servlet-name>action</servlet-name>
		<url-pattern>*.html</url-pattern>
	</servlet-mapping>
	
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

 

   10. 前端 View 两个jsp index.jsp和 result.jsp

 

     index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>简单的lucene测试</title>
    <script type="text/javascript" src="js/CheckFunction.js"></script>
	<script type="text/javascript">
		function _search()
		{
			var form = document.form1;
			if(Trim(form.title.value) == '')
			{
				alert("输入一个关键字吧!");
				return false;
			}
			form.action = 'index.html?m=index';
			form.submit();
		}
	</script>
  </head>
  
  <body>
    	
    	<center><h1>测试Lucene</h1>
    	<hr/>
    	<form name="form1" action="index.html" method="post">
    	请输入文章的标题关键字:<input type="text" maxlength="20" name="title" size="40"/>&nbsp;
    	<button onclick="javascript:_search();return false;" style="border: thin;border-color: blue" name="search"> GO </button>
    	</form>
    	</center>
  </body>
</html>

 

    result.jsp

 

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="com.jushi.lucene.entities.*"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%
	List<Article> list = (List<Article>) request.getAttribute("articlelist");
	String TotalTime = "0.000";
	int TotalHits = 0;
	if(request.getAttribute("totalTime")!=null)
		TotalTime = request.getAttribute("totalTime").toString();
	if(list.size()>0 && list!=null)
		TotalHits = list.get(0).getTotalHits(); 
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
   	<title>搜索的结果如下:</title>
   	<style type="text/css">
   		.Fee {
   			width: 650px;
   			height:28px;
   			float: right;
   			padding:1px;
   			overflow:hidden;
   			border-bottom:1px solid #bde2ff;
   			line-height:28px;
   			font-family: '宋体';
   			font-size: 15px;
   			font-weight: bold;
   		}
   		.Fee span {
   			color: red;
   			font-size:14px;
   		}
   		
   	</style>
  </head>
  
  <body>
  	<div class="Fee">共搜到 <span><%=TotalHits %></span> 条结果. 共花费:<span><%=TotalTime %> </span>秒.</div><br/><br/>
  	
    <table width="80%" height="70%" border="1" cellpadding="0" cellspacing="0" align="center">
    		<tr height="20" bgcolor="#CCCCCC">
    		<td align="center" width="20%">标题</td>
    		<td align="center" width="60%">文章内容</td>
    		<td align="center" width="20%">所属标签</td>
    		</tr>
    		<%
    		if(list.size()>0){
    		for(Article art : list){
    			String title = art.getTitle();
    			String content = art.getContent();
    			String tag = art.getTag();
    		%>
    		<tr>
    			<td><%=title %></td>
    			<td><%=content %></td>
    			<td><%=tag %></td>
    		</tr>
    		<%}}else{ %>
    		<tr>
    			<td colspan="3" align="center"><h3>对不起...没有搜到相关信息..请尝试其他关键字!</h3></td>
    		</tr>
    		<%} %>
    	</table>
    	<br/>
    	<center><a href="<%=basePath %>./">返回上一级</a></center>
  </body>
</html>

 

    12. action:IndexAction.java

 

package com.jushi.lucene.businessview;

import java.text.DecimalFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.jushi.lucene.business.IArticleService;
import com.jushi.lucene.entities.Article;

/**
 * @Author fenglong
 * @CreateDate Jun 4, 2010 1:50:02 PM
 * @Version V 1.0
 * @CopyRight 2010 jushi
 */
public class IndexAction extends DispatchAction {

	public ActionForward index(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(this.getServlet().getServletContext());
		IArticleService articleService = (IArticleService) wac.getBean("articleService");

		String q = request.getParameter("title");
		DecimalFormat df = new DecimalFormat("#0.000");
		
		if(articleService.createIndex()){
			
			long begin = new Date().getTime();
			List<Article> list = articleService.getArticles(q);
			long end = new Date().getTime();
			double time = (double) (end - begin) / 1000;
			String timeStr = df.format(time);
			
			request.setAttribute("totalTime", timeStr);
			request.setAttribute("articlelist", list);
			
		}
		
		return mapping.findForward("result");
	}

}

 

 

    好了,所有的配置已经完成了。大概就是个Lucene的简单实现。只是稍微的和Spring结合了一下。功能就是搜索数据,高亮显示关键字。页面不好看,逻辑教简单。

     欢迎拍砖。

 

 

  • 大小: 24.5 KB
  • 大小: 16.1 KB
  • 大小: 142.5 KB
分享到:
评论
16 楼 fl_zxf 2015-02-03  
在看,3Q
15 楼 jushi1988 2015-01-12  
哎.米休 写道
在action的第一行会报空指针异常 求解啊

你看看是不是jar包版本问题,我有jar包截图,debug下代码
14 楼 哎.米休 2015-01-09  
在action的第一行会报空指针异常 求解啊
13 楼 jushi1988 2012-02-12  
八岭书生 写道
jushi1988 写道
八岭书生 写道
跑起来 会报错
java.lang.NullPointerException
at com.jushi.lucene.business.ArticleServiceImpl.isIndexExisted(ArticleServiceImpl.java:161)
at com.jushi.lucene.business.ArticleServiceImpl.createIndex(ArticleServiceImpl.java:117)
at com.jushi.lucene.businessview.IndexAction.index(IndexAction.java:38)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:270)
at org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:187)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:96)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:291)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:619)
java.io.IOException: 设备未就绪。




这什么情况啊

我的索引文件创建在g:\\index目录,你本地包含这个目录吗?



我的索引文件创建在d:\\index目录   可就是会报这个错啊

看着貌似是 common-logging.jar包的版本不对,没遇见过这个异常,google一下吧。
12 楼 八岭书生 2012-02-07  
八岭书生 写道
jushi1988 写道
八岭书生 写道
跑起来 会报错
java.lang.NullPointerException
at com.jushi.lucene.business.ArticleServiceImpl.isIndexExisted(ArticleServiceImpl.java:161)
at com.jushi.lucene.business.ArticleServiceImpl.createIndex(ArticleServiceImpl.java:117)
at com.jushi.lucene.businessview.IndexAction.index(IndexAction.java:38)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:270)
at org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:187)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:96)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:291)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:619)
java.io.IOException: 设备未就绪。




这什么情况啊

我的索引文件创建在g:\\index目录,你本地包含这个目录吗?



我的索引文件创建在d:\\index目录   可就是会报这个错啊


是报这个错:

org.apache.commons.logging.LogConfigurationException: Invalid class loader hierarchy.  You have more than one version of 'org.apache.commons.logging.Log' visible, which is not allowed.
11 楼 八岭书生 2012-02-07  
jushi1988 写道
八岭书生 写道
跑起来 会报错
java.lang.NullPointerException
at com.jushi.lucene.business.ArticleServiceImpl.isIndexExisted(ArticleServiceImpl.java:161)
at com.jushi.lucene.business.ArticleServiceImpl.createIndex(ArticleServiceImpl.java:117)
at com.jushi.lucene.businessview.IndexAction.index(IndexAction.java:38)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:270)
at org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:187)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:96)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:291)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:619)
java.io.IOException: 设备未就绪。




这什么情况啊

我的索引文件创建在g:\\index目录,你本地包含这个目录吗?



我的索引文件创建在d:\\index目录   可就是会报这个错啊
10 楼 jushi1988 2012-02-06  
八岭书生 写道
跑起来 会报错
java.lang.NullPointerException
at com.jushi.lucene.business.ArticleServiceImpl.isIndexExisted(ArticleServiceImpl.java:161)
at com.jushi.lucene.business.ArticleServiceImpl.createIndex(ArticleServiceImpl.java:117)
at com.jushi.lucene.businessview.IndexAction.index(IndexAction.java:38)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:270)
at org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:187)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:96)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:291)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:619)
java.io.IOException: 设备未就绪。




这什么情况啊

我的索引文件创建在g:\\index目录,你本地包含这个目录吗?
9 楼 八岭书生 2012-02-06  
跑起来 会报错
java.lang.NullPointerException
at com.jushi.lucene.business.ArticleServiceImpl.isIndexExisted(ArticleServiceImpl.java:161)
at com.jushi.lucene.business.ArticleServiceImpl.createIndex(ArticleServiceImpl.java:117)
at com.jushi.lucene.businessview.IndexAction.index(IndexAction.java:38)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:270)
at org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:187)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:96)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:291)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:619)
java.io.IOException: 设备未就绪。




这什么情况啊
8 楼 jushi1988 2010-07-28  
harbey 写道
lucene的应该还是很广泛的,特别是大数据量搜索的时候,能解决数据查询性能问题!

恩. lucene在数据查询性能上有很大的优势。
7 楼 huangfoxAgain 2010-07-28  
jushi1988 写道
hellojinjie 写道
为了分布式。。。有时索引并不是存在本机上的,需要到其他的机器上去取

恩 对,这个就根据需要进行配置了。

其实索引数据分离可以提高检索效率~~~~
6 楼 harbey 2010-07-27  
lucene的应该还是很广泛的,特别是大数据量搜索的时候,能解决数据查询性能问题!
5 楼 dukai1008 2010-07-27  
不错,支持下,下载下来跑下
4 楼 jushi1988 2010-06-30  
skyuck 写道
为啥要结合db呢?

因为有的时候数据是以数据库形式存在的,而非单纯文件形式。故根据需要,本例只是个简单与db结合。
3 楼 jushi1988 2010-06-30  
hellojinjie 写道
为了分布式。。。有时索引并不是存在本机上的,需要到其他的机器上去取

恩 对,这个就根据需要进行配置了。
2 楼 hellojinjie 2010-06-29  
为了分布式。。。有时索引并不是存在本机上的,需要到其他的机器上去取
1 楼 skyuck 2010-06-29  
为啥要结合db呢?

相关推荐

    Lucene.NET结合Sql建立全文检索Demo源码

    本Demo源码展示了如何将Lucene.NET与SQL数据库结合,以实现对数据库存储的文本数据进行全文检索。通常,我们会在数据库中存储大量结构化数据,而使用Lucene.NET可以对这些非结构化的文本信息进行高效的检索。 **...

    lucene搜索引擎项目

    `db_search_Log.LDF`和`db_search_Data.MDF`可能是数据库日志和数据文件,它们可能被用作Lucene索引的来源。 3. **查询解析与执行** Lucene提供了强大的查询解析器,能够处理多种查询语法,包括布尔查询、短语查询...

    使用lucene进行简单的开发

    总结,这个简单的Lucene开发示例展示了如何将数据库中的数据构建为Lucene索引,以及如何使用这些索引执行搜索。在实际应用中,可能还需要考虑更复杂的查询逻辑、优化性能、错误处理等其他方面。随着对Lucene的深入...

    orientdb手册

    提供有关如何使用 OrientDB 的图模型进行建模和查询的具体示例。 **1.11 使用图的模式 (Using Schema with Graphs)** 讨论如何结合图模型和模式定义来增强数据完整性。 **1.12 设置分布式数据库 (Setting Up a ...

    引入局部统计识别高频词汇的Lucene中文分词程序src.rar

    描述中提到的信息与标题一致,暗示这个压缩包包含的源代码着重于利用统计方法改进Lucene的中文分词性能。局部统计方法可能指的是在特定上下文中对词语频率的分析,以此提高对常用词汇的识别准确性和效率,这在处理大...

    H2DB 参考手册(英文版)

    - **与其他数据库系统的对比**:例如 Daffodil DB 和 One$DB 等。 #### H2 在实际项目中的应用案例 - **应用场景**:列举了一些 H2 实际使用的场景。 #### 连接模式 - **嵌入式模式**:适合于单个 JVM 内部的...

    google suggest 的实现

    - **Ajax**:通过 JavaScript 与 XMLHttpRequest 的结合,实现了浏览器与服务器之间的异步通信,从而避免了每次搜索建议都需要刷新页面的情况。 - **JavaScript**:作为前端的主要编程语言,用于处理用户输入、构建...

    up_1106017_SolrPhpClient_jxpqk.rar

    - "47db6d1cbd85774b3bdbc7fadde56bc5.txt":这看起来是一个哈希值命名的文本文件,可能包含了代码、日志数据或特定的配置信息。哈希值通常用于确保文件的完整性和一致性。 - "fileinfo.txt":这是一个常见的文件...

    图解Solr6.6.0安装与MySQL配置

    - 在此示例中,我们将创建一个名为`solr_home`的目录,路径为`E:\lucene\solr_home`。 - 从解压的`solr-6.6.0\server\solr`路径中复制主配置文件到`solr_home`目录。 **2.4 创建Core** - Core是Solr中的逻辑单元,...

    emh-exist-glossary:这是一个示例搜索应用程序,带有用于管理SKOS词汇表的eXist-db开源XML数据库的方面

    EasyMetaHub词汇表,用于eXist-db项目 介绍 该应用程序是一个SKOS词汇表管理器和可管理多个词汇表的多面搜索应用程序。 它适用于需要管理一个或多个词汇表的组织。 那里有许多项目不需要MarkLogic的功能以及它的...

    Nutch相关框架视频教程 讲义 杨尚川

    #### 三、Lucene、Nutch与Hadoop的重要性 在搜索引擎领域,**Lucene**、**Nutch**和**Hadoop**都是非常重要的项目和技术。其中: - **Lucene**:是一个高性能、全文检索引擎库,它提供了完整的索引和搜索功能。 ...

    java文集

    正则表达式 lucene索引合并 探查Weblogic JDBC Multipool 问题 struts通用Exception处理 Grails中默认数据库HSQLDB点滴 从request获取各种路径总结 DIV实现的表格自动伸张与收缩 java 邮件...

    up_151180_SolrPhpClient_4432s9.rar

    Solr是Apache Lucene项目的一个开源搜索服务器,而SolrPhpClient则是用来方便PHP应用程序与Solr服务器通信的工具。 描述中提到"脚本语言服务端php实现",暗示我们将探讨如何使用PHP作为服务器端脚本语言来处理数据...

    up_1060818_SolrPhpClient_xqjho.rar

    压缩包中的"db42ed8c9bdc36c078cbedee8bac3ef2.txt"和"fileinfo.txt"可能是文档或者日志文件,可能包含有关SolrPhpClient的使用说明、示例代码或者版本信息。"SolrPhpClient"目录则可能包含了SolrPhpClient的源代码...

    bookstore-example:显示 Elasticsearch + Searchkick 集成的示例应用

    它基于Lucene构建,提供了一个分布式、多租户的全文搜索和分析引擎服务。Elasticsearch能够快速地存储、搜索和分析大量数据,支持丰富的查询语法,使得数据检索变得简单。 Searchkick是Ruby on Rails框架中的一个...

    ES同步的sql语句和同步的配置文件.rar

    综上所述,这个压缩包提供了MySQL到Elasticsearch数据同步的基本工具和配置示例,帮助用户实现从传统数据库到现代搜索与分析平台的数据迁移和实时同步。通过学习和应用这些知识点,用户能够提升数据处理效率,利用...

Global site tag (gtag.js) - Google Analytics