TermQuery
这个检索通常查找文档中唯一数据的字段(在更新索引时特别有用),一般这个字段使用Field.Index.NOT_ANALYZED方式保存索引(但并不意味着所有Field.Index.NOT_ANALYZED方式保存索引的字段有唯一值).
1 | Term t = new Term( "contents" , "java" ); |
2 | Query query = new TermQuery(t); |
TermRangeQuery
范围检索,可以返回在两个单词之间的数据.
1 | TermRangeQuery query = new TermRangeQuery( "title" , "d" , "j" , true , true ); |
3 | TermRangeQuery query = new TermRangeQuery( "title" , "tim" , "tom" , true , true ); |
NumericRangeQuery
数字范围检索,可以返回在两个数字直接的数据.在建索引时,该字段必须是以数字方式建的.并且创建时和检索时的使用的数字类型要相同.1 | NumericRangeQuery query = NumericRangeQuery.newIntRange( "pubmonth" ,198805,198810, true , true ); |
PrefixQuery
前缀检索,就像数据库里的LIKE 'abc%'一样.普遍用于产品分类,不确定单词等.1 | Term term = new Term( "category" , "/technology/computers/programming" ); |
3 | Term term = new Term( "title" , "can" ); |
4 | PrefixQuery query = new PrefixQuery(term); |
BooleanQuery
布尔检索,对两个Query进行逻辑运算.选项有BooleanClause.Occur.MUST, BooleanClause.Occur.SHOULD 和 BooleanClause.Occur.MUST_NOT.1 | TermQuery searchingBooks = new TermQuery( new Term( "subject" , "search" )); |
2 | Query books2004 = NumericRangeQuery.newIntRange( "pubmonth" , 200401, ,200412, true , true ); |
3 | BooleanQuery searchingBooks2004 = new BooleanQuery(); |
4 | searchingBooks2004.add(searchingBooks, BooleanClause.Occur.MUST); |
5 | searchingBooks2004.add(books2004, BooleanClause.Occur.MUST); |
PhraseQuery
短语检索,可以检索一个短语或词组.可以设置单词间隔距离,默认是11 | PhraseQuery query = new PhraseQuery(); |
3 | for ( int i=0; i < phrase.length; i++) { |
4 | query.add( new Term( "field" , phrase[i])); |
WildcardQuery
通配符检索,可以使用?,*这些通配符进行检索.?代表一个字符,*代表多个字符.(注意在单词开头使用通配符会严重影响效率)1 | Query query = new WildcardQuery( new Term( "contents" , "?ild*" )); |
FuzzyQuery
模糊检索,可以检索相似的词.比如wuzza和fuzzy.(模糊检索也会影响效率)1 | Query query = new FuzzyQuery( new Term( "contents" , "wuzza" )); |
MatchAllDocsQuery
匹配全部文档,顾名思义就是匹配全部文档的意思.
1 | Query query = new MatchAllDocsQuery(field); |
注意,字段名和值都要区分大小写!