lucene查询之WildcardQuery报错(备忘)

我测试了一下多字段构造查询,代码如下:

try {
   long  start=System.currentTimeMillis();
   Directory dic = new SimpleFSDirectory(new File(ILuceneManager.DEFAULT_REGION_LUCENE_INDEX_PATH));
   IndexSearcher searcher=new IndexSearcher(dic);
   //----------设置相似度
   //searcher.setSimilarity(new IKSimilarity());
   
   //-----------多字段查询     经过测试StandardAnalyzer分词器要比IKAnalyzer的效率要快
   Analyzer analyzer=new StandardAnalyzer(Version.LUCENE_CURRENT);
   //Analyzer analyzer=new IKAnalyzer(true);
   String[] fields=new String[]{"NAME","USERNAME","weight","STANDARDNAME","ADDRESS"};
   MultiFieldQueryParser  multiQParser=new MultiFieldQueryParser(Version.LUCENE_CURRENT, fields, analyzer);
  
   //----------关键字
   
   String  keyword="?府 ";
   Query query=multiQParser.parse(keyword);
   
   //------------查询
   //searcher.search(query, LuceneManagerImpl.DEFAULT_QUERY_NUM);
   TopDocs tops=searcher.search(query,10);
   ScoreDoc[] scores=tops.scoreDocs;
   int length=tops.totalHits;
   for(int i=0;i<(length>LuceneManagerImpl.DEFAULT_QUERY_NUM?LuceneManagerImpl.DEFAULT_QUERY_NUM:length);i++){
    Document targetDoc=searcher.doc(scores[i].doc);
    //System.out.println(targetDoc.getFields("NAME")[0].stringValue());
    //System.out.println(targetDoc.getFields("ADDRESS")[0].stringValue());
    //System.out.println(targetDoc.get("NAME"));
    System.out.println(targetDoc.get("ADDRESS"));
    System.out.println(targetDoc.get("USERNAME"));
    System.out.println(targetDoc.get("NAME"));
   };
   long  end=System.currentTimeMillis();
   System.out.println("搜索共耗时:"+(end-start)+" 毫秒");

 我搜索关键字是:“?府”。结果后台直接报错,错误信息如下:

org.apache.lucene.queryParser.ParseException: Cannot parse '?府 ': '*' or '?' not allowed as first character in WildcardQuery
	at org.apache.lucene.queryParser.QueryParser.parse(QueryParser.java:187)
	at com.geostar.test.poi.dao.LuceneManagerTest.testMultipleFieldQuery(LuceneManagerTest.java:79)
	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.springframework.test.context.junit4.SpringTestMethod.invoke(SpringTestMethod.java:160)
	at org.springframework.test.context.junit4.SpringMethodRoadie.runTestMethod(SpringMethodRoadie.java:233)
	at org.springframework.test.context.junit4.SpringMethodRoadie$RunBeforesThenTestThenAfters.run(SpringMethodRoadie.java:333)
	at org.springframework.test.context.junit4.SpringMethodRoadie.runWithRepetitions(SpringMethodRoadie.java:217)
	at org.springframework.test.context.junit4.SpringMethodRoadie.runTest(SpringMethodRoadie.java:197)
	at org.springframework.test.context.junit4.SpringMethodRoadie.run(SpringMethodRoadie.java:143)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.invokeTestMethod(SpringJUnit4ClassRunner.java:160)
	at org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:51)
	at org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:44)
	at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:27)
	at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:37)
	at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:42)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:97)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: org.apache.lucene.queryParser.ParseException: '*' or '?' not allowed as first character in WildcardQuery
	at org.apache.lucene.queryParser.QueryParser.getWildcardQuery(QueryParser.java:923)
	at org.apache.lucene.queryParser.MultiFieldQueryParser.getWildcardQuery(MultiFieldQueryParser.java:175)
	at org.apache.lucene.queryParser.MultiFieldQueryParser.getWildcardQuery(MultiFieldQueryParser.java:170)
	at org.apache.lucene.queryParser.QueryParser.Term(QueryParser.java:1347)
	at org.apache.lucene.queryParser.QueryParser.Clause(QueryParser.java:1250)
	at org.apache.lucene.queryParser.QueryParser.Query(QueryParser.java:1178)
	at org.apache.lucene.queryParser.QueryParser.TopLevelQuery(QueryParser.java:1167)
	at org.apache.lucene.queryParser.QueryParser.parse(QueryParser.java:182)
	... 24 more

 由错误信息提示,让我记住了,使用WildCardQuery对象进行查询时,查询关键字前不能有“?”和“*”。

posted @ 2012-02-16 10:48  李克华  阅读(3524)  评论(1编辑  收藏  举报