Lucene系列-搜索

Lucene搜索的时候就要构造查询语句,本篇就介绍下各种Query。IndexSearcher是搜索主类,提供的常用查询接口有:

TopDocs search(Query query, int n);//find the top n hits for query
TopDocs search(Query query, Filter filter, int n);// find the top n hits for query, applying filter if no-null

Query

quey在org.apache.lucene.search上,代表最终的查询语法树,传入IndexSearcher进行查找。

TermQuery:在某个Field上查找一个词条

Term t = new Term("bookname", "Lucene");//词条所在Field,词条内容
Query q = new TermQuery(t);

BooleanQuery:由多个子句组成,子句间由“与、或、非”这样的布尔逻辑连接。BooleanClause.Occur是个枚举,包括MUST/MUST_NOT/SHOULD。常用的组合有:

MUST和MUST:求交集,MUST和MUST_NOT:求差集,SHOULD和SHOULD:求并集。

void add(Query query, BooleanClause.Occur occur)

NumericRangeQuery/TermRangeQuery:范围查询,范围可以是日期、时间、数字,如果不设上限或下限,对应的边界设为null,inclusive设为false。

TermRangeQuery(String field, String lowerTerm, String upperTerm, boolean includeLower, boolean includeUpper);
//NumericRangeQuery
static NumericRangeQuery<Double>	newDoubleRange(String field, Double min, Double max, boolean minInclusive, boolean maxInclusive);
static NumericRangeQuery<Float>  newFloatRange(String field, Float min, Float max, boolean minInclusive, boolean maxInclusive); 
static NumericRangeQuery<Integer>	newIntRange(String field, Integer min, Integer max, boolean minInclusive, boolean maxInclusive);
static NumericRangeQuery<integer>	newIntRange(String field, int precisionStep, Integer min, Integer max, boolean minInclusive, boolean maxInclusive);

PhraseQuery:短语搜索,一个以上的关键字组成的短语,如中国,钢铁。可以设置slop,允许短语中的字之间有其他字的个数,默认为0

void add(Term term);//add a term to the end of the query phrase
void setSlop(int s);//set the number of other words between words in the query phrase
//sample,bookname包含"中国"的会被搜到,其他组合都不会被搜到
PhraseQuery query = new PhraseQuery();
query.add(new Term("bookname", "中"));
query.add(new Term("bookname", "国"));

MultiPhraseQuery:一些短语有相同的前缀,或后缀,或中间词,如中国好声音和美国好声音

void	add(Term term);//Add a single term at the next position in the phrase.
void	add(Term[] terms);//Add multiple terms at the next position in the phrase.
//sample
MultiPhraseQuery query = new MultiPhraseQuery();
query.add(new Term[]{new Term("bookname", "中"), new Term("bookname", "美")});
query.add(new Term("song", "国"));
query.add(new Term("song", "好"));
query.add(new Term("song", "声"));
query.add(new Term("song", "音"));

PrefixQuery:前缀匹配

PrefixQuery query = new PrefixQuery(new Term("bookname","钢"));//查找以钢开头的bookname

FuzzyQuery:模糊匹配,比较两个字符串时,执行一个串转变为另一个串的操作(增、删、改变字母),每执行一次转变就扣除一定分数,最终得到两者的距离(模糊度)

FuzzyQuery(Term term);
FuzzyQuery(Term term, int maxEdits);//maxEdits-an edit distance fo at most maxEdits to term
FuzzyQuery(Term term, int maxEdits, int prefixLength);//prefixLength-length of common (no-fuzzy) prefix

WildcardQuery:使用'?'和'*'通配符

WildcardQuery query = new WildcardQuery(new Term("bookname", "?o*")); 

Filter

filter相当于是一个搜索必须条件,用于对搜索结果进行限制,如返回的文档安全级别限制。所有过滤器都继承org.apache.lucene.search.Filter,因为Filter条件大多与query无关,不需要每次都执行一次索引遍历,所以lucene引入了缓存技术,避免一遍遍重复的搜索索引过滤文档。

常用的有NumericRangeFilter、PrefixFilter、TermRangeFilter,封装Filter以实现缓存的CachingWrapperFilter,针对某个Field进行缓存的FieldCacheRangeFilter、FieldCacheTermsFilter。

QueryParser

org.apache.lucene.queryParser用于解析子句生成Query。支持的语法规则如下

Query  ::= ( Clause )*
Clause ::= ["+", "-"] [<TERM> ":"] ( <TERM> | "(" Query ")" )

+ 必须,- 排除,: 表示针对某个Field搜索,通配符?*。举例

+bookname:java -bookname:structs,搜bookname中包含java不包含structs的doc
publishdate:[1990 TO 1998],初版日期在1990和1998之间
bookname:work~0.5,模糊查询
bookname:"apache lucene"~5,松散短语查询,bookname必须包含apache和lucene,但两者距离要在5个词内
"God helps",加引号表示不分词,作为完整的一个短语查询
bookname:(java search),空格隔开的多个词需要加括号,否则后面一个词"search"不会被认为是在bookname上的搜索,会认为是default field上的搜索 

常用方法有:

Query parse(String query);
QueryParser(Version matchVersion, String f, Analyzer a)//分词器应该与建索引的分词器保持一致

注:

构造好Query后,想看下实际的查询内容,可以用query.toString()

posted @ 2014-10-24 19:59  春文秋武  阅读(499)  评论(0编辑  收藏  举报