论坛首页 入门技术论坛

用lucene 搜索时出现Parser错误,望解!!

浏览 6130 次
该帖已经被评为新手帖
作者 正文
   发表时间:2007-07-30  
出错的信息为:
建立搜索引擎
建立搜索域和分析器
Exception in thread "main" org.apache.lucene.queryParser.ParseException: Encountered "<EOF>" at line 1, column 0.
Was expecting one of:
    <NOT> ...
    "+" ...
    "-" ...
    "(" ...
    "*" ...
    <QUOTED> ...
    <TERM> ...
    <PREFIXTERM> ...
    <WILDTERM> ...
    "[" ...
    "{" ...
    <NUMBER> ...
   
at org.apache.lucene.queryParser.QueryParser.generateParseException(QueryParser.java:1485)
at org.apache.lucene.queryParser.QueryParser.jj_consume_token(QueryParser.java:1365)
at org.apache.lucene.queryParser.QueryParser.Clause(QueryParser.java:997)
at org.apache.lucene.queryParser.QueryParser.Query(QueryParser.java:907)
at fangLucene.TwoLucene.main(TwoLucene.java:86)

其lucene应用代码为:
package fangLucene;
import org.apache.lucene.analysis.SimpleAnalyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;

import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.DriverManager;

public class TwoLucene {
	public static ResultSet rs = null;
	public static Statement statement = null;
	public static Connection con = null;
	public TwoLucene() throws Exception{
		
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception{
		Directory directory = FSDirectory.getDirectory("D:\\luceneIndex");
		/** 建索 */
		IndexWriter indexWriter = new IndexWriter(directory,new SimpleAnalyzer(),true);
		indexWriter.setMaxFieldLength(250000);
		indexWriter.setMergeFactor(100); 
		
		System.out.println("开始取数据");
		Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
		System.out.println("已装载驱动");
		con = DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=lssDB","sa","1234");
		System.out.println("已连接上数据库");
		statement = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
		rs = statement.executeQuery("select databankid,TitleName,mostlycontent,lastDate from tabmdatabank");
		System.out.println("已读取数据集");
		String id,title,content,date;
		int j=0;
		while(rs.next()){
			Document doc = new Document();
			id = rs.getString(1);
			title =rs.getString(2);
			content = rs.getString(3);
			date = rs.getString(4);
			if(id == null)
				id = "";
			if(title == null)
				title = "";
			if(content == null )
				content = "";
			if(date == null)
				date = "";
			doc.add(new Field("dbid",id,Field.Store.YES,Field.Index.UN_TOKENIZED));
			doc.add(new Field("title",title,Field.Store.YES,Field.Index.TOKENIZED,Field.TermVector.YES));
			doc.add(new Field("content",content,Field.Store.NO,Field.Index.TOKENIZED,Field.TermVector.YES));
			doc.add(new Field("date",date,Field.Store.COMPRESS,Field.Index.TOKENIZED));
			indexWriter.addDocument(doc);
			
			System.out.println("已添加"+(++j)+"doc,其ID为:"+id);
		}
		indexWriter.optimize();
		rs.close();
		statement.close();
		con.close();
		System.out.println("结束取数据");
		indexWriter.close();
		System.out.println("已生成所有数据索引");
		
		//*********************************************************************
		/** 搜索 */
		IndexReader reader = IndexReader.open(directory);
		IndexSearcher indexSearcher = new IndexSearcher(reader);
		System.out.println("建立搜索引擎");
		QueryParser parser = new QueryParser("content",new SimpleAnalyzer());
		System.out.println("建立搜索域和分析器");
		Query query = parser.Query("劳动法律");
		System.out.println("提供搜索内容");
		Hits hits = indexSearcher.search(query);
		System.out.println("进行搜索并返回数据集");
		for(int i=0;i<hits.length();i++){
			Document doc = hits.doc(i);
				System.out.println(doc.get("title"));
		}
		System.out.println("取得查询数据标题");
		directory.close();
		indexSearcher.close();
		
		
		System.out.println("关闭搜索器");
		
	}
	
	public static void getCon() throws Exception{
		
	}
	public static void getStatement() throws Exception{
		
	}
	
}
   发表时间:2007-07-30  
要过滤一下搜索关键词,不能出现+,-,“,(,等特殊符号,因为那是搜索运算符。
0 请登录后投票
   发表时间:2007-07-30  
网上看了!!说是在搜索一些lucene的特殊字!
可俺明明就是搜索"劳动"二字,何来特殊符啊!!
0 请登录后投票
   发表时间:2007-07-30  
见鬼.....
0 请登录后投票
   发表时间:2007-07-30  
俺的内容都是html文件来的!!不知有没有影响!!
用不用自已再写个过滤呢?
0 请登录后投票
   发表时间:2007-07-30  
找到啦!!原来是一个方法名写错啦!!真是晕
      Query query = parser.Query("劳动法律"); 
应为:
      Query query = parser.parser("劳动法律"); 
0 请登录后投票
论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics