lucene查询语法
一、 使用Query子类去查询
1. MatchAllDocsQuery
[AppleScript]
纯文本查看
复制代码
1 2 3 | //查询所有文档Query query = new MatchAllDocsQuery();// 相当于查询语法 : *:* |
2.NumericRangeQuery
[Java]
纯文本查看
复制代码
01 02 03 04 05 06 07 08 09 10 | //数值范围查询//参数1:要查询的域 //参数2:最小值 newLongRange 就是long类型//参数3:最大值 //参数4:是否包含最小值//参数5:是否包含最大值Query query = NumericRangeQuery.newLongRange("size", 1000l, 10000l, false, true);//相当于查询语法 : [1000 TO 10000] //不支持数字类型 |
3.BooleanQuery
组合查询
[AppleScript]
纯文本查看
复制代码
01 02 03 04 05 06 07 08 09 10 11 12 13 | //创建一个BooleanQuery对象BooleanQuery query = new BooleanQuery();//创建子查询Query query1 = new TermQuery(new Term("name", "lucene"));//文件名中包含mybatis关键字Query query2 = new TermQuery(new Term("name", "apache"));//添加到BooleanQuery对象中query.add(query1, Occur.SHOULD);query.add(query2, Occur.MUST_NOT);//相当于查询语法: AND OR NOT// name:lucene -name:apache// name:lucene NOT name:apache |
4. QueryParser
[AppleScript]
纯文本查看
复制代码
1 2 3 4 5 6 | //指定默认搜索域, 制定分析器QueryParser queryParser = new QueryParser("content", new IKAnalyzer());//对Lucene是java开发的进行分析之后,在content域上进行搜索,分词结果满足其一即可Query query = queryParser.parse("Lucene是java开发的");//相当于查询语法 content:lucene content:是 content:java content:开发 |
5. MultiFieldQueryParser
[AppleScript]
纯文本查看
复制代码
1 2 3 4 5 6 7 8 | //制定多个默认搜索域String[] fields ={"name", "content"};MultiFieldQueryParser queryParser = new MultiFieldQueryParser(fields, new IKAnalyzer());//在多个搜索域上进行分析结果的查询 ,满足其一即可Query query = queryParser.parse("mybatis is a apache project");//相当于查询语法 //(name:mybatis content:mybatis) (name:apache content:apache) (name:project content:project) |
6. WildcardQuery
[AppleScript]
纯文本查看
复制代码
01 02 03 04 05 06 07 08 09 10 11 | @Test// sprin? => 匹配一个字符 spring sprinj sprinl// sprin* => 匹配多个字符 springfdgfd sprinj sprinl// 通配符可以任意的位置public void testQuery() throws Exception { Query query = new WildcardQuery(new Term("name","sp*ng")); printResult(getIndexSearcher(), query);}//相当于查询语法// name:sp*ng name:sprin? name:spring* name:?pring |
7. FuzzyQuery
[AppleScript]
纯文本查看
复制代码
1 2 3 4 5 6 | //可以进行模糊匹配, 即使有个别字母错误,也可以查询出结果//只有两次机会, 只允许修正两个字母的错误Query query = new FuzzyQuery(new Term("name","lvcene"));printResult(getIndexSearcher(), query);// 相当于查询语法 : name:lvcene~2 |
二、 使用QueryParser使用查询语法去查询
1、基础的查询语法,关键词查询:
域名+“:”+搜索的关键字
例如:content:java
2、范围查询
域名+“:”+[最小值 TO 最大值]
例如:size:[1 TO 1000]
范围查询在lucene中不支持数值类型,支持字符串类型。在solr中支持数值类型。
3、组合条件查询
1)+条件1 +条件2:两个条件之间是并且的关系and
例如:+filename:apache +content:apache
2)+条件1 条件2:必须满足第一个条件,应该满足第二个条件
例如:+filename:apache content:apache
3)条件1 条件2:两个条件满足其一即可。
例如:filename:apache content:apache
4)-条件1 条件2:必须不满足条件1,要满足条件2
例如:-filename:apache content:apache
第二种写法:
条件1 AND 条件2
条件1 OR 条件2
条件1 NOT 条件2