Skip to content

Commit b5decac

Browse files
committed
Track 9 files into repository.
- modified query-dsl/match-all-query.md - modified query-dsl.md - modified query-dsl/full-text-queries.md - modified query-dsl/term-level-queries.md - modified query-dsl/compound-queries.md - modified query-dsl/joining-queries.md - modified query-dsl/geo-queries.md - modified query-dsl/specialized-queries.md - modified query-dsl/span-queries.md Auto commit by GitBook Editor
1 parent 4d8ce78 commit b5decac

9 files changed

+892
-10
lines changed

query-dsl.md

-10
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,3 @@ import static org.elasticsearch.index.query.QueryBuilders.*;
1616
1717
`QueryBuilder`可以用于接受任何查询API,如`count``search`
1818

19-
### Match All Query
20-
21-
> 最简单的查询,它匹配所有文档
22-
23-
查看 [Match All Query](https://www.elastic.co/guide/en/elasticsearch/reference/5.6/query-dsl-match-all-query.html)
24-
25-
26-
```
27-
QueryBuilder qb = matchAllQuery();
28-
```

query-dsl/compound-queries.md

+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
2+
### Compound queries
3+
4+
复合查询用来包装其他复合或者叶子查询,一方面可综合其结果和分数,从而改变它的行为,另一方面可从查询切换到过滤器上下文。此类查询包含:
5+
6+
- constant_score 查询
7+
8+
这是一个包装其他查询的查询,并且在过滤器上下文中执行。与此查询匹配的所有文件都需要返回相同的“常量” _score 。
9+
10+
查看[Constant Score Query](https://www.elastic.co/guide/en/elasticsearch/reference/5.6/query-dsl-constant-score-query.html)
11+
12+
13+
```
14+
QueryBuilder qb = constantScoreQuery(
15+
termQuery("name","kimchy") //查询语句
16+
)
17+
.boost(2.0f); //分数
18+
```
19+
20+
21+
- bool 查询
22+
23+
组合多个叶子并发查询或复合查询条件的默认查询类型,例如must, should, must_not, 以及 filter 条件。 在 must 和 should 子句他们的分数相结合-匹配条件越多,预期越好-而 must_not 和 filter 子句在过滤器上下文中执行。
24+
25+
查看[Bool Query](https://www.elastic.co/guide/en/elasticsearch/reference/5.6/query-dsl-bool-query.html)
26+
27+
```
28+
QueryBuilder qb = boolQuery()
29+
.must(termQuery("content", "test1")) //must query
30+
.must(termQuery("content", "test4"))
31+
.mustNot(termQuery("content", "test2")) //must not query
32+
.should(termQuery("content", "test3")) // should query
33+
.filter(termQuery("content", "test5// 与一般查询作用一样,只不过不参与评分
34+
```
35+
36+
- dis_max 查询
37+
38+
支持多并发查询的查询,并可返回与任意查询条件子句匹配的任何文档类型。与 bool 查询可以将所有匹配查询的分数相结合使用的方式不同的是,dis_max 查询只使用最佳匹配查询条件的分数。
39+
40+
查看[Dis Max Query](https://www.elastic.co/guide/en/elasticsearch/reference/5.6/query-dsl-dis-max-query.html)
41+
42+
```
43+
QueryBuilder qb = disMaxQuery()
44+
.add(termQuery("name", "kimchy"))
45+
.add(termQuery("name", "elasticsearch"))
46+
.boost(1.2f) //boost factor
47+
.tieBreaker(0.7f); //tie breaker
48+
```
49+
50+
- function_score 查询
51+
52+
使用函数修改主查询返回的分数,以考虑诸如流行度,新近度,距离或使用脚本实现的自定义算法等因素。
53+
54+
查看[Function Score Query](https://www.elastic.co/guide/en/elasticsearch/reference/5.6/query-dsl-function-score-query.html)
55+
56+
57+
```
58+
import static org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders.*;
59+
60+
```
61+
62+
63+
```
64+
FilterFunctionBuilder[] functions = {
65+
new FunctionScoreQueryBuilder.FilterFunctionBuilder(
66+
matchQuery("name", "kimchy"), //根据查询添加第一个function
67+
randomFunction("ABCDEF")), //根据给定的种子随机分数
68+
new FunctionScoreQueryBuilder.FilterFunctionBuilder(
69+
exponentialDecayFunction("age", 0L, 1L)) //根据年龄字段添加另一个function
70+
71+
};
72+
QueryBuilder qb = QueryBuilders.functionScoreQuery(functions);
73+
```
74+
75+
76+
- boosting 查询
77+
78+
返回匹配 positive 查询的文档,并且当减少文档的分数时其结果也匹配 negative 查询。
79+
80+
81+
查看[Boosting Query](https://www.elastic.co/guide/en/elasticsearch/reference/5.6/query-dsl-boosting-query.html)
82+
83+
84+
```
85+
QueryBuilder qb = boostingQuery(
86+
termQuery("name","kimchy"),
87+
termQuery("name","dadoonet"))
88+
.negativeBoost(0.2f);
89+
```
90+
91+
- indices 查询
92+
93+
对指定的索引执行一个查询,对其他索引执行另一个查询。
94+
95+
查看[Indices Query](https://www.elastic.co/guide/en/elasticsearch/reference/5.6/query-dsl-indices-query.html)
96+
97+
98+
> 在5.0.0中已弃用。用搜索 _index 字段来代替
99+
100+
```
101+
// Using another query when no match for the main one
102+
QueryBuilder qb = indicesQuery(
103+
termQuery("tag", "wow"),
104+
"index1", "index2"
105+
).noMatchQuery(termQuery("tag", "kow"));
106+
107+
```
108+
109+
110+
```
111+
// Using all (match all) or none (match no documents)
112+
QueryBuilder qb = indicesQuery(
113+
termQuery("tag", "wow"),
114+
"index1", "index2"
115+
).noMatchQuery("all");
116+
```
117+

query-dsl/full-text-queries.md

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
2+
### Full text queries 全文搜索
3+
4+
高级别的全文搜索通常用于在全文字段(例如:一封邮件的正文)上进行全文搜索。它们了解如何分析查询的字段,并在执行之前将每个字段的分析器(或搜索分析器)应用于查询字符串。
5+
6+
这样的查询有以下这些:
7+
8+
- 匹配查询(match query)
9+
10+
用于执行全文查询的标准查询,包括模糊匹配和词组或邻近程度的查询
11+
12+
查看[ Match Query](https://www.elastic.co/guide/en/elasticsearch/reference/5.6/query-dsl-match-query.html)
13+
14+
15+
```
16+
QueryBuilder qb = matchQuery(
17+
"name", //field 字段
18+
"kimchy elasticsearch" // text
19+
);
20+
```
21+
22+
- 多字段查询(multi_match query)
23+
24+
可以用来对多个字段的版本进行匹配查询
25+
26+
查看 [Multi Match Query](https://www.elastic.co/guide/en/elasticsearch/reference/5.6/query-dsl-multi-match-query.html)
27+
28+
29+
```
30+
QueryBuilder qb = multiMatchQuery(
31+
"kimchy elasticsearch", //text
32+
"user", "message" //fields 多个字段
33+
);
34+
```
35+
36+
37+
- 常用术语查询(common_terms query)
38+
39+
可以对一些比较专业的偏门词语进行的更加专业的查询
40+
41+
查看[Common Terms Query](https://www.elastic.co/guide/en/elasticsearch/reference/5.6/query-dsl-common-terms-query.html)
42+
43+
44+
```
45+
QueryBuilder qb = commonTermsQuery("name", //field 字段
46+
"kimchy"); // value
47+
```
48+
49+
50+
- 查询语句查询(query_string query)
51+
52+
与lucene查询语句的语法结合的更加紧密的一种查询,允许你在一个查询语句中使用多个 特殊条件关键字(如:AND|OR|NOT )对多个字段进行查询,当然这种查询仅限专家用户去使用。
53+
54+
查看[Query String Query](https://www.elastic.co/guide/en/elasticsearch/reference/5.6/query-dsl-query-string-query.html)
55+
56+
57+
```
58+
QueryBuilder qb = queryStringQuery("+kimchy -elasticsearch"); //text
59+
60+
```
61+
62+
63+
- 简单查询语句(simple_query_string)
64+
65+
是一种适合直接暴露给用户的简单的且具有非常完善的查询语法的查询语句
66+
67+
查看[Simple Query String Query](https://www.elastic.co/guide/en/elasticsearch/reference/5.6/query-dsl-simple-query-string-query.html)
68+
69+
70+
```
71+
QueryBuilder qb = simpleQueryStringQuery("+kimchy -elasticsearch"); //text
72+
```

query-dsl/geo-queries.md

+116
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
2+
### Geo queries 地理位置查询
3+
4+
Elasticsearch支持两种类型的地理数据:geo_point类型支持成对的纬度/经度,geo_shape类型支持点、线、圆、多边形、多个多边形等。
5+
在这组的查询中:
6+
7+
- geo_shape查询
8+
9+
查找要么相交,包含的,要么指定形状不相交的地理形状的文档。
10+
11+
查看[Geo Shape Query](https://www.elastic.co/guide/en/elasticsearch/reference/5.6/query-dsl-geo-shape-query.html)
12+
13+
> `geo_shape` 类型使用 [`Spatial4J`](http://search.maven.org/#search%7Cga%7C1%7Cg%3A%22org.locationtech.spatial4j%22%20AND%20a%3A%22spatial4j%22)[`JTS`](http://search.maven.org/#search%7Cga%7C1%7Cg%3A%22com.vividsolutions%22%20AND%20a%3A%22jts%22) ,这两者都是可选的依赖项。 因此,必须将 [`Spatial4J`](http://search.maven.org/#search%7Cga%7C1%7Cg%3A%22org.locationtech.spatial4j%22%20AND%20a%3A%22spatial4j%22)[`JTS`](http://search.maven.org/#search%7Cga%7C1%7Cg%3A%22com.vividsolutions%22%20AND%20a%3A%22jts%22) 添加到 `classpath ` 中才能使用此类型:
14+
15+
```
16+
<dependency>
17+
<groupId>org.locationtech.spatial4j</groupId>
18+
<artifactId>spatial4j</artifactId>
19+
<version>0.6</version>
20+
</dependency>
21+
22+
<dependency>
23+
<groupId>com.vividsolutions</groupId>
24+
<artifactId>jts</artifactId>
25+
<version>1.13</version>
26+
<exclusions>
27+
<exclusion>
28+
<groupId>xerces</groupId>
29+
<artifactId>xercesImpl</artifactId>
30+
</exclusion>
31+
</exclusions>
32+
</dependency>
33+
```
34+
35+
36+
```
37+
// Import ShapeRelation and ShapeBuilder
38+
import org.elasticsearch.common.geo.ShapeRelation;
39+
import org.elasticsearch.common.geo.builders.ShapeBuilder;
40+
```
41+
42+
43+
```
44+
List<Coordinate> points = new ArrayList<>();
45+
points.add(new Coordinate(0, 0));
46+
points.add(new Coordinate(0, 10));
47+
points.add(new Coordinate(10, 10));
48+
points.add(new Coordinate(10, 0));
49+
points.add(new Coordinate(0, 0));
50+
51+
QueryBuilder qb = geoShapeQuery(
52+
"pin.location", //field
53+
ShapeBuilders.newMultiPoint(points) //shape
54+
.relation(ShapeRelation.WITHIN); //relation 可以是 ShapeRelation.CONTAINS, ShapeRelation.WITHIN, ShapeRelation.INTERSECTS 或 ShapeRelation.DISJOINT
55+
```
56+
57+
58+
59+
60+
```
61+
// Using pre-indexed shapes
62+
QueryBuilder qb = geoShapeQuery(
63+
"pin.location", //field
64+
"DEU", //The ID of the document that containing the pre-indexed shape.
65+
"countries") //Index type where the pre-indexed shape is.
66+
.relation(ShapeRelation.WITHIN)) //relation
67+
.indexedShapeIndex("shapes") //Name of the index where the pre-indexed shape is. Defaults to shapes.
68+
.indexedShapePath("location"); //The field specified as path containing the pre-indexed shape. Defaults to shape.
69+
```
70+
71+
72+
- geo_bounding_box 查询
73+
74+
查找落入指定的矩形地理点的文档。
75+
76+
查看[Geo Bounding Box Query](https://www.elastic.co/guide/en/elasticsearch/reference/5.6/query-dsl-geo-bounding-box-query.html)
77+
78+
79+
```
80+
QueryBuilder qb = geoBoundingBoxQuery("pin.location") //field
81+
.setCorners(40.73, -74.1, //bounding box top left point
82+
40.717, -73.99); //bounding box bottom right point
83+
```
84+
85+
86+
- geo_distance 查询
87+
88+
查找在一个中心点指定范围内的地理点文档。
89+
90+
查看[Geo Distance Query](https://www.elastic.co/guide/en/elasticsearch/reference/5.6/query-dsl-geo-distance-query.html)
91+
92+
93+
```
94+
QueryBuilder qb = geoDistanceQuery("pin.location") //field
95+
.point(40, -70) //center point
96+
.distance(200, DistanceUnit.KILOMETERS); //distance from center point
97+
```
98+
99+
100+
- geo_polygon 查询
101+
102+
103+
查找指定多边形内地理点的文档。
104+
105+
查看[Geo Polygon Query](https://www.elastic.co/guide/en/elasticsearch/reference/5.6/query-dsl-geo-polygon-query.html)
106+
107+
108+
```
109+
List<GeoPoint> points = new ArrayList<>(); //add your polygon of points a document should fall within
110+
points.add(new GeoPoint(40, -70));
111+
points.add(new GeoPoint(30, -80));
112+
points.add(new GeoPoint(20, -90));
113+
114+
QueryBuilder qb =
115+
geoPolygonQuery("pin.location", points); //initialise the query with field and points
116+
```

0 commit comments

Comments
 (0)