# 各种 Query 及 QueryBuilder
- 要构建 QueryBuilder,我们可以使用工具类 QueryBuilders,里面有大量的方法用来完成各种各样的 QueryBuilder 的构建,字符串的、Boolean 型的、match 的、地理范围的等等。
- 要构建 SortBuilder,可以使用 SortBuilders 来完成各种排序。
- 然后就可以通过 NativeSearchQueryBuilder 来组合这些 QueryBuilder 和 SortBuilder,再组合分页的参数等等,最终就能得到一个 SearchQuery 了。
# QueryBuilders
/**
* A Query that matches documents containing a term.
*
* @param name The name of the field
* @param value The value of the term
*/
public static TermQueryBuilder termQuery(String name, String value) {
return new TermQueryBuilder(name, value);
}
/**
* A Query that matches documents using fuzzy query.
*
* @param name The name of the field
* @param value The value of the term
*
* @see #matchQuery(String, Object)
* @see #rangeQuery(String)
*/
public static FuzzyQueryBuilder fuzzyQuery(String name, String value) {
return new FuzzyQueryBuilder(name, value);
}
/**
* A Query that matches documents containing terms with a specified prefix.
*
* @param name The name of the field
* @param prefix The prefix query
*/
public static PrefixQueryBuilder prefixQuery(String name, String prefix) {
return new PrefixQueryBuilder(name, prefix);
}
/**
* A Query that matches documents within an range of terms.
*
* @param name The field name
*/
public static RangeQueryBuilder rangeQuery(String name) {
return new RangeQueryBuilder(name);
}
/**
* A Query that matches documents containing terms with a specified regular expression.
*
* @param name The name of the field
* @param regexp The regular expression
*/
public static RegexpQueryBuilder regexpQuery(String name, String regexp) {
return new RegexpQueryBuilder(name, regexp);
}
/**
* A Query that matches documents matching boolean combinations of other queries.
*/
public static BoolQueryBuilder boolQuery() {
return new BoolQueryBuilder();
}
/**
* A filter based on the relationship of a shape and indexed shapes
*
* @param name The shape field name
* @param shape Shape to use in the filter
*/
public static GeoShapeQueryBuilder geoShapeQuery(String name, ShapeBuilder shape) throws IOException {
return new GeoShapeQueryBuilder(name, shape);
}
public static GeoShapeQueryBuilder geoShapeQuery(String name, String indexedShapeId, String indexedShapeType) {
return new GeoShapeQueryBuilder(name, indexedShapeId, indexedShapeType);
}
/**
* A filter to filter indexed shapes that are contained by a shape
* 用于过滤形状所包含的索引形状的过滤器
*
* @param name The shape field name
* @param shape Shape to use in the filter
*/
public static GeoShapeQueryBuilder geoWithinQuery(String name, ShapeBuilder shape) throws IOException {
GeoShapeQueryBuilder builder = geoShapeQuery(name, shape);
builder.relation(ShapeRelation.WITHIN);
return builder;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# GeoShapeQueryBuilder
基于形状和索引形状的关系的过滤器
# RegexpQueryBuilder
与包含具有指定正则表达式的术语的文档匹配的查询。
# RangeQueryBuilder
一个查询,该查询匹配一系列术语内的文档。
# PrefixQueryBuilder
与包含带有指定前缀的术语的文档匹配的查询。
# FuzzyQueryBuilder
使用模糊查询匹配文档的查询。
# TermQueryBuilder
与包含术语的文档匹配的查询。
# BoolQueryBuilder
must的性能要低一些,为什么?因为他要进行打分评估,也就是说要进行_score,而filter则不会。与文档匹配的查询,该文档与其他查询的布尔组合匹配。