DANCE WITH JAVA

开发出高质量的系统

常用链接

统计

积分与排名

好友之家

最新评论

lucene的丰富的各种查询(一)

lucene支持十分丰富的查询,这里列写其中一些比较常用的查询的用法。
term查询、queryParser查询 ,booleanQuery
package search;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

public class Searcher {
    
public static void termQuery() throws Exception{
        Directory directory 
= FSDirectory.getDirectory("./index"false); 
        IndexSearcher searcher 
= new IndexSearcher(directory);
        Term t 
= new Term("body","document");
        Query query 
= new TermQuery(t);
        Hits hits 
= searcher.search(query);
        System.out.println(hits.length());
    }

    
public static void queryParser() throws Exception{
        Directory directory 
= FSDirectory.getDirectory("./index"false); 
        IndexSearcher searcher 
= new IndexSearcher(directory);
        Query query 
= QueryParser.parse("text","body",new StandardAnalyzer());
        Hits hits 
= searcher.search(query);
        System.out.println(hits.length());
    }

    
public static void booleanQuery() throws Exception{
        Query parseQuery 
= QueryParser.parse("text","body",new StandardAnalyzer());
        Term t 
= new Term("body","document");
        Query termQuery 
= new TermQuery(t);
        BooleanQuery boolQuery 
= new BooleanQuery();
        boolQuery.add(parseQuery,
true,false);
        boolQuery.add(termQuery,
true,false);
        
        Directory directory 
= FSDirectory.getDirectory("./index"false); 
        IndexSearcher searcher 
= new IndexSearcher(directory);
        Hits hits 
= searcher.search(boolQuery);
        System.out.println(hits.length());
    }

    
public static void main(String[] args) throws Exception{
        termQuery();
        queryParser();
        booleanQuery();
    }

}


posted on 2007-06-21 15:06 dreamstone 阅读(1252) 评论(4)  编辑  收藏 所属分类: 搜索引擎lucence

评论

# re: lucene的丰富的各种查询(一) 2008-06-18 17:29 eitrade

收藏了..  回复  更多评论   

# re: lucene的丰富的各种查询(一) 2008-06-18 17:29 3wdotec

不错..  回复  更多评论   

# re: lucene的丰富的各种查询(一) 2008-06-18 17:29 环保袋

谢谢..  回复  更多评论   

# re: lucene的丰富的各种查询(一) 2008-07-02 10:00 renkui

谢谢分享  回复  更多评论   


标题  
姓名  
主页
验证码 *  
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
该文被作者在 2007-08-02 22:10 编辑过
 
 
相关链接:
网站导航: