package ear.lucenedemo.process;import java.io.IOException;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.index.Term;
import org.apache.lucene.queryParser.MultiFieldQueryParser;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.FuzzyQuery;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.MultiPhraseQuery;
import org.apache.lucene.search.MultiSearcher;
import org.apache.lucene.search.ParallelMultiSearcher;
import org.apache.lucene.search.PhraseQuery;
import org.apache.lucene.search.PrefixQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.RangeQuery;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.WildcardQuery;
import org.apache.lucene.search.regex.RegexQuery;
import org.apache.lucene.search.spans.SpanNearQuery;
import org.apache.lucene.search.spans.SpanNotQuery;
import org.apache.lucene.search.spans.SpanOrQuery;
import org.apache.lucene.search.spans.SpanQuery;
import org.apache.lucene.search.spans.SpanTermQuery;public class QueryScoreTest { private static String INDEX_STORE_PATH = "d:\\lucene\\luceneScoreIndex";
private static String INDEX_STORE_PATH1 = "d:\\lucene\\parserIndex"; public static void main(String[] args) throws Exception {
// QueryScoreTest.searcherScore(INDEX_STORE_PATH);
// QueryScoreTest.booleanQuery(INDEX_STORE_PATH);
// QueryScoreTest.booleanQuery1(INDEX_STORE_PATH);
// QueryScoreTest.rangeQuery(INDEX_STORE_PATH);
// QueryScoreTest.prefixQuery(INDEX_STORE_PATH);
// QueryScoreTest.phraseQuery(INDEX_STORE_PATH);
// QueryScoreTest.multiPhraseQuery(INDEX_STORE_PATH);
// QueryScoreTest.fuzzyQuery(INDEX_STORE_PATH);
// QueryScoreTest.wildcardQuery(INDEX_STORE_PATH);
// QueryScoreTest.spanQuery(INDEX_STORE_PATH);
// QueryScoreTest.regexQuery(INDEX_STORE_PATH);
//QueryScoreTest.queryParser(INDEX_STORE_PATH1);
QueryScoreTest.multiFieldAndMutiIndexSearch(INDEX_STORE_PATH1, INDEX_STORE_PATH);

} /**
 * 多域(Field)多索引(index)搜索(利用MutiSearcher类)
 * 和多域多线程搜索(利用ParallelMutiSearcher类) 二者的区别如下:
 * MultiSearcher:是利用一个for循环将所有的IndexSearcher对象取出,然后顺序循环搜索并得出结果
 * ParallelMutiSearcher:是一种更好的模型,一旦一个搜索发出请求,所有的索引可以在同一时刻被搜索,
 * 也就是被多个线程检索
 * 注意:多余搜索MutiSearcher和多线程搜索ParallelMutiSearcher在搜索结束后会自动关闭Searcher的
 * @param path
 * @throws Exception
 */
public static void multiFieldAndMutiIndexSearch(String path1,String path2)throws Exception{
// 创建多域搜索
// 两个查询关键字
//相当于一个TermQuery和一个RangeQuery
String query1="钢";
String query2="[10 TO 98]";//注意格式:中括号还有关键字TO是大写的
String[] queries={query1,query2};

// 指定两个域Field
String field1="bookname";
String field2="price";
String[] fields={field1,field2};

// 指定查询字句之间的关系
BooleanClause.Occur[] clauses={BooleanClause.Occur.MUST,
BooleanClause.Occur.MUST};

// 转成多域查询MultiFieldQuery
Query q=MultiFieldQueryParser.parse(queries, fields, clauses,
new StandardAnalyzer() );

// 打印Query的内容
System.out.println(q);

// 创建两个IndexSearcher,以实现在多个索引目录进行查询
IndexSearcher searcher1=new IndexSearcher(path1);
IndexSearcher searcher2=new IndexSearcher(path2);

IndexSearcher[] searchers={searcher1,searcher2};

//使用多域搜索MultiSearcher类
//在path1和path2这两个索引目录下搜索
long startTime=System.currentTimeMillis();
MultiSearcher searcher=new MultiSearcher(searchers);
Hits hits=searcher.search(q);
long endTime=System.currentTimeMillis();
System.out.println("MultiSearcher 搜索耗时为"+(endTime-startTime)+"毫秒");
for(int i=0;i<hits.length();i++){
System.out.println(hits.doc(i));
}

//使用多线程搜索
startTime=System.currentTimeMillis();
ParallelMultiSearcher multiSearcher=new ParallelMultiSearcher(searchers);
hits=multiSearcher.search(q);
endTime=System.currentTimeMillis();
System.out.println("ParallelMultiSearcher 搜索耗时为"+(endTime-startTime)+"毫秒");
for(int i=0;i<hits.length();i++){
System.out.println(hits.doc(i));
}

} /**
 * 解析查询,先把传入的字符串进行解析,然后再查询
 * 
 * @param path
 * @throws IOException
 */
public static void queryParser(String path) throws Exception {
Document bookdoc1 = new Document();
Field bookNo = new Field("booknumber", "FB246546", Field.Store.YES,
Field.Index.UN_TOKENIZED);
Field bookName = new Field("bookname", "钢铁是怎样炼成的", Field.Store.YES,
Field.Index.TOKENIZED);
Field author = new Field("author", "匿名", Field.Store.YES,
Field.Index.UN_TOKENIZED);
Field publishDate = new Field("publishdate", "1970-01-10",
Field.Store.YES, Field.Index.UN_TOKENIZED);
Field bookAbstract = new Field("bookabstract", "这部苏联小说写得不错哦",
Field.Store.NO, Field.Index.UN_TOKENIZED);
Field price = new Field("price", "25.00", Field.Store.YES,
Field.Index.UN_TOKENIZED);
bookdoc1.add(bookNo);
bookdoc1.add(bookName);
bookdoc1.add(author);
bookdoc1.add(publishDate);
bookdoc1.add(bookAbstract);
bookdoc1.add(price); Document bookdoc2 = new Document();
bookNo = new Field("booknumber", "UJ48886", Field.Store.YES,
Field.Index.UN_TOKENIZED);
bookName = new Field("bookname", "钢铁男人", Field.Store.YES,
Field.Index.TOKENIZED);
author = new Field("author", "匿名", Field.Store.YES,
Field.Index.UN_TOKENIZED);
publishDate = new Field("publishdate", "1970-01-10", Field.Store.YES,
Field.Index.UN_TOKENIZED);
bookAbstract = new Field("bookabstract", "这是一部科幻小说哦", Field.Store.NO,
Field.Index.UN_TOKENIZED);
price = new Field("price", "95.00", Field.Store.YES,
Field.Index.UN_TOKENIZED);
bookdoc2.add(bookNo);
bookdoc2.add(bookName);
bookdoc2.add(author);
bookdoc2.add(publishDate);
bookdoc2.add(bookAbstract);
bookdoc2.add(price); IndexWriter writer = new IndexWriter(path, new StandardAnalyzer(), true);
writer.addDocument(bookdoc1);
writer.addDocument(bookdoc2);
writer.close(); // 解析搜索开始了
IndexSearcher searcher = new IndexSearcher(path);
// 4个查询字符串
String queryStr1 = "刚 publishdate:1970-01-10";
String queryStr2 = "战";
String queryStr3 = "price:25.00";
String queryStr4 = "publishdate:1970-01-10"; // 构建一个QueryParser,分别对每个查询字符串进行分析并生成Query对象
QueryParser parser = new QueryParser("bookname", new StandardAnalyzer());
parser.setDefaultOperator(QueryParser.AND_OPERATOR); Query query = parser.parse(queryStr1);
System.out.println(query.toString());
Hits hits = searcher.search(query);
for (int i = 0; i < hits.length(); i++) {
System.out.println(hits.doc(i));
}
System.out.println("=================================================");
query = parser.parse(queryStr2);
System.out.println(query.toString());
hits = searcher.search(query);
for (int i = 0; i < hits.length(); i++) {
System.out.println(hits.doc(i));
}
System.out.println("=================================================");
query = parser.parse(queryStr3);
System.out.println(query.toString());
hits = searcher.search(query);
for (int i = 0; i < hits.length(); i++) {
System.out.println(hits.doc(i));
}
System.out.println("=================================================");
query = parser.parse(queryStr4);
System.out.println(query.toString());
hits = searcher.search(query);
for (int i = 0; i < hits.length(); i++) {
System.out.println(hits.doc(i));
}
System.out.println("=================================================");
searcher.close();
}

解决方案 »

  1.   

    大虾,问一下,运行queryParser这段代码一直出错,是不是要添加什么包啊
    错误语句
    Query query = parser.parse(queryStr1); 
    query = parser.parse(queryStr2); 
    query = parser.parse(queryStr3); 
    query = parser.parse(queryStr4); 
    错误描述
    Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    Unhandled exception type ParseException
    Unhandled exception type ParseException
    Unhandled exception type ParseException
    Unhandled exception type ParseException at ch4.searcher.parserTest.queryParser(parserTest.java:101)
    at ch4.searcher.parserTest.main(parserTest.java:40)
    望楼主指点一二
    谢谢!在线等…………
      

  2.   

     发表于:2008-04-03 15:57:11 楼主 
    package ear.lucenedemo.process; import java.io.IOException; 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.index.Term; 
    import org.apache.lucene.queryParser.MultiFieldQueryParser; 
    import org.apache.lucene.queryParser.QueryParser; 
    import org.apache.lucene.search.BooleanClause; 
    import org.apache.lucene.search.BooleanQuery; 
    import org.apache.lucene.search.FuzzyQuery; 
    import org.apache.lucene.search.Hits; 
    import org.apache.lucene.search.IndexSearcher; 
    import org.apache.lucene.search.MultiPhraseQuery; 
    import org.apache.lucene.search.MultiSearcher; 
    import org.apache.lucene.search.ParallelMultiSearcher; 
    import org.apache.lucene.search.PhraseQuery; 
    import org.apache.lucene.search.PrefixQuery; 
    import org.apache.lucene.search.Query; 
    import org.apache.lucene.search.RangeQuery; 
    import org.apache.lucene.search.TermQuery; 
    import org.apache.lucene.search.WildcardQuery; 
    import org.apache.lucene.search.regex.RegexQuery; 
    import org.apache.lucene.search.spans.SpanNearQuery; 
    import org.apache.lucene.search.spans.SpanNotQuery; 
    import org.apache.lucene.search.spans.SpanOrQuery; 
    import org.apache.lucene.search.spans.SpanQuery; 
    import org.apache.lucene.search.spans.SpanTermQuery; public class QueryScoreTest { private static String INDEX_STORE_PATH = "d:\\lucene\\luceneScoreIndex"; 
    private static String INDEX_STORE_PATH1 = "d:\\lucene\\parserIndex"; public static void main(String[] args) throws Exception { 
    // QueryScoreTest.searcherScore(INDEX_STORE_PATH); 
    // QueryScoreTest.booleanQuery(INDEX_STORE_PATH); 
    // QueryScoreTest.booleanQuery1(INDEX_STORE_PATH); 
    // QueryScoreTest.rangeQuery(INDEX_STORE_PATH); 
    // QueryScoreTest.prefixQuery(INDEX_STORE_PATH); 
    // QueryScoreTest.phraseQuery(INDEX_STORE_PATH); 
    // QueryScoreTest.multiPhraseQuery(INDEX_STORE_PATH); 
    // QueryScoreTest.fuzzyQuery(INDEX_STORE_PATH); 
    // QueryScoreTest.wildcardQuery(INDEX_STORE_PATH); 
    // QueryScoreTest.spanQuery(INDEX_STORE_PATH); 
    // QueryScoreTest.regexQuery(INDEX_STORE_PATH); 
    //QueryScoreTest.queryParser(INDEX_STORE_PATH1); 
    QueryScoreTest.multiFieldAndMutiIndexSearch(INDEX_STORE_PATH1, INDEX_STORE_PATH); } /** 
     * 多域(Field)多索引(index)搜索(利用MutiSearcher类) 
     * 和多域多线程搜索(利用ParallelMutiSearcher类) 二者的区别如下: 
     * MultiSearcher:是利用一个for循环将所有的IndexSearcher对象取出,然后顺序循环搜索并得出结果 
     * ParallelMutiSearcher:是一种更好的模型,一旦一个搜索发出请求,所有的索引可以在同一时刻被搜索, 
     * 也就是被多个线程检索 
     * 注意:多余搜索MutiSearcher和多线程搜索ParallelMutiSearcher在搜索结束后会自动关闭Searcher的 
     * @param path 
     * @throws Exception 
     */ 
    public static void multiFieldAndMutiIndexSearch(String path1,String path2)throws Exception{ 
    // 创建多域搜索 
    // 两个查询关键字 
    //相当于一个TermQuery和一个RangeQuery 
    String query1="钢"; 
    String query2="[10 TO 98]";//注意格式:中括号还有关键字TO是大写的 
    String[] queries={query1,query2}; // 指定两个域Field 
    String field1="bookname"; 
    String field2="price"; 
    String[] fields={field1,field2}; // 指定查询字句之间的关系 
    BooleanClause.Occur[] clauses={BooleanClause.Occur.MUST, 
    BooleanClause.Occur.MUST}; // 转成多域查询MultiFieldQuery 
    Query q=MultiFieldQueryParser.parse(queries, fields, clauses, 
    new StandardAnalyzer() ); // 打印Query的内容 
    System.out.println(q); // 创建两个IndexSearcher,以实现在多个索引目录进行查询 
    IndexSearcher searcher1=new IndexSearcher(path1); 
    IndexSearcher searcher2=new IndexSearcher(path2); IndexSearcher[] searchers={searcher1,searcher2}; //使用多域搜索MultiSearcher类 
    //在path1和path2这两个索引目录下搜索 
    long startTime=System.currentTimeMillis(); 
    MultiSearcher searcher=new MultiSearcher(searchers); 
    Hits hits=searcher.search(q); 
    long endTime=System.currentTimeMillis(); 
    System.out.println("MultiSearcher 搜索耗时为"+(endTime-startTime)+"毫秒"); 
    for(int i=0;i <hits.length();i++){ 
    System.out.println(hits.doc(i)); 
    } //使用多线程搜索 
    startTime=System.currentTimeMillis(); 
    ParallelMultiSearcher multiSearcher=new ParallelMultiSearcher(searchers); 
    hits=multiSearcher.search(q); 
    endTime=System.currentTimeMillis(); 
    System.out.println("ParallelMultiSearcher 搜索耗时为"+(endTime-startTime)+"毫秒"); 
    for(int i=0;i <hits.length();i++){ 
    System.out.println(hits.doc(i)); 
    } } /** 
     * 解析查询,先把传入的字符串进行解析,然后再查询 
     *  
     * @param path 
     * @throws IOException 
     */ 
    public static void queryParser(String path) throws Exception { 
    Document bookdoc1 = new Document(); 
    Field bookNo = new Field("booknumber", "FB246546", Field.Store.YES, 
    Field.Index.UN_TOKENIZED); 
    Field bookName = new Field("bookname", "钢铁是怎样炼成的", Field.Store.YES, 
    Field.Index.TOKENIZED); 
    Field author = new Field("author", "匿名", Field.Store.YES, 
    Field.Index.UN_TOKENIZED); 
    Field publishDate = new Field("publishdate", "1970-01-10", 
    Field.Store.YES, Field.Index.UN_TOKENIZED); 
    Field bookAbstract = new Field("bookabstract", "这部苏联小说写得不错哦", 
    Field.Store.NO, Field.Index.UN_TOKENIZED); 
    Field price = new Field("price", "25.00", Field.Store.YES, 
    Field.Index.UN_TOKENIZED); 
    bookdoc1.add(bookNo); 
    bookdoc1.add(bookName); 
    bookdoc1.add(author); 
    bookdoc1.add(publishDate); 
    bookdoc1.add(bookAbstract); 
    bookdoc1.add(price);