lucene查询分析器语法

Terms
Single Terms:
new Term("key",keyword);

Phrases:
PhraseQuery query = new PhraseQuery();
String[] words = sentence.split(" ");
for (String word : words) {
    query.add(new Term("contents", word));
}
booleanQuery.add(query, BooleanClause.Occur.MUST)

Operator
Key:AND OR BETWEEN TO NOT ()(表示分组) +(前缀标识MUST的意思)
4.9+代码:
String searchString = "(name:cc OR title:aa) AND text:bb";
QueryParser queryParser = new QueryParser(whichField, new StandardAnalyzer());
Query query = queryParser.parse(searchString);

4.7.0代码:
/**
     * 根据查询条件创建Query对象
     * @fields queries中“:”之前的field字段
     * @param queries 查询条件语句
     * @return
     * @throws Exception
     */
    private Query createQuery(String[] fields, String queries) throws Exception {
    	QueryParser queryParser = new MultiFieldQueryParser(version, fields, new StandardAnalyzer(version));  
    	Query query = queryParser.parse(queries);  
		return query;
    }

Wildcard
Key:* ?
String searchString = "te?t";
QueryParser queryParser = new QueryParser(whichField, new StandardAnalyzer());
Query query = queryParser.parse(searchString);


Fuzzy Searches
Key:~
roam~ 匹配单词:form 和 roams.这中文估计不大一样
roam~0.8 0.8表示相似度。相似度0.8以及以上就可以查出结果
"jakarta apache"~10 表示2个单词距离为10以内就可以查出结果
用法和上面一样


Boosting a Term
Key:^
roam^4 keyword 表示因子为4,越大越重要。跟查询结果排序有关。

Escaping Special Characters

特殊字符:+ - && || ! ( ) { } [ ] ^ " ~ * ? : \ 用"\"转义

有用的一些函数:
时间类型可以用DateTools转为String建立索引,查询的时候可以用datetime:[xx TO xx]
Date date = DateUtils.parseDate(value, "yyyy-MM-dd");
value = DateTools.dateToString(date, DateTools.Resolution.DAY);
doc.add(new StringField(name, value, Store.YES));

float , int , long 类型使用以下方式建立索引,查询的时候可以用number:[1.11 TO 2.11]。复合查询时用NumericRangeQuery
float flValue = Float.parseFloat(value);
type.setNumericType(NumericType.FLOAT);
doc.add(new FloatField(name, flValue, type));

根据查询字符串产生query
    /**
     * 根据查询条件创建Query对象
     * @fields queries中“:”之前的field字段
     * @param queries 查询条件语句
     * @return
     * @throws Exception
     */
    private Query createQuery(String[] fields, String queries) throws Exception {
    	QueryParser queryParser = new MultiFieldQueryParser(version, fields, new StandardAnalyzer(version));  
    	Query query = queryParser.parse(queries);  
		return query;
    }

复合查询
String[] fields = new String[]{"packageName","className","date"};
Date d1 = fmt.parse("2013-01-00");
Date d2 = fmt.parse("2014-01-02");
String s = "["+d1.getTime()+" TO "+d2.getTime()+"]";
//字符串和时间合并查询
String queries = new String("packageName:vv AND className:vvNode4 AND date:"+s);
BooleanQuery query = new BooleanQuery();
//float类型单独查询,合并到上面查询结果就错误,所以单独
Query q1 = NumericRangeQuery.newFloatRange("value", 2.22f, 2.22f, true, true);
query.add(q1, BooleanClause.Occur.MUST);
//创建复合查询query
QueryParser queryParser = new MultiFieldQueryParser(Version.LUCENE_47, fields, new StandardAnalyzer(Version.LUCENE_47));  
Query q2 = queryParser.parse(queries);  
query.add(q2, BooleanClause.Occur.MUST);
//查询
TopDocs tds = searcher.search(query, Integer.MAX_VALUE);

你可能感兴趣的:(lucene 查询 语法)