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 阅读(3245) 评论(6)  编辑  收藏 所属分类: 搜索引擎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

谢谢分享  回复  更多评论   

# re: lucene的丰富的各种查询(一) 2009-06-12 13:54 min

也学了LUcene,自己开发了个小型的系统,
http://www.tudoupian.com
但是效果不怎么好,看了你这篇才知道Lucene查询有这么灵活。
  回复  更多评论   

# re: lucene的丰富的各种查询(一) 2009-06-12 13:55 min

怎么结合 term booleanQuery?
想给 http://www.tudoupian.com 做个价格范围的查询,听说rangeQuery很慢,
  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航: