Skip to content
This repository was archived by the owner on Sep 21, 2021. It is now read-only.

Commit 1d51270

Browse files
Added snippets for 054_Query_DSL
1 parent 98b5c58 commit 1d51270

20 files changed

+759
-14
lines changed

054_Query_DSL/55_Request_body_search.asciidoc

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ which returns all documents in all indices.
2020
GET /_search
2121
{} <1>
2222
--------------------------------------------------
23+
// SENSE: 054_Query_DSL/60_Empty_query.json
2324
<1> This is an empty request body.
2425

2526
Just as with query-string search, you can search on one, many or `_all`

054_Query_DSL/60_Query_DSL.asciidoc

+4-3
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ GET /_search
2828
}
2929
}
3030
--------------------------------------------------
31-
31+
// SENSE: 054_Query_DSL/60_Empty_query.json
3232

3333
==== Structure of a query clause
3434

@@ -87,7 +87,7 @@ GET /_search
8787
}
8888
}
8989
--------------------------------------------------
90-
90+
// SENSE: 054_Query_DSL/60_Match_query.json
9191

9292
==== Combining multiple clauses
9393

@@ -106,11 +106,12 @@ other to create complex queries. Clauses can be:
106106
{
107107
"bool": {
108108
"must": { "match": { "tweet": "elasticsearch" }},
109-
"must_not": { "match": { "name": "john" }},
109+
"must_not": { "match": { "name": "mary" }},
110110
"should": { "match": { "tweet": "full text" }}
111111
}
112112
}
113113
--------------------------------------------------
114+
// SENSE: 054_Query_DSL/60_Bool_query.json
114115

115116

116117
It is important to note that a compound clause can combine *any* other

054_Query_DSL/70_Important_clauses.asciidoc

+13-9
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ booleans, or `not_analyzed` exact value string fields:
1717
{ "term": { "tag": "full_text" }}
1818
{ "term": { "public": true }}
1919
--------------------------------------------------
20-
20+
// SENSE: 054_Query_DSL/70_Term_filter.json
2121

2222
==== `terms` filter
2323

@@ -29,7 +29,7 @@ the specified values, then the document matches:
2929
--------------------------------------------------
3030
{ "terms": { "tag": [ "search", "full_text", "nosql" ] }}
3131
--------------------------------------------------
32-
32+
// SENSE: 054_Query_DSL/70_Terms_filter.json
3333

3434
==== `range` filter
3535

@@ -47,7 +47,7 @@ the specified range:
4747
}
4848
}
4949
--------------------------------------------------
50-
50+
// SENSE: 054_Query_DSL/70_Range_filter.json
5151

5252
The operators that it accepts are:
5353

@@ -72,7 +72,7 @@ any values (`missing`). It is similar in nature to `IS_NULL` in SQL:
7272
}
7373
}
7474
--------------------------------------------------
75-
75+
// SENSE: 054_Query_DSL/70_Exists_filter.json
7676

7777
These filters are frequently used to apply a condition only if a field is
7878
present, and to apply a different condition if it is missing.
@@ -103,6 +103,7 @@ of filter clauses:
103103
}
104104
}
105105
--------------------------------------------------
106+
// SENSE: 054_Query_DSL/70_Bool_filter.json
106107

107108

108109
==== `match_all` query
@@ -114,6 +115,7 @@ query which is used if no query has been specified.
114115
--------------------------------------------------
115116
{ "match_all": {}}
116117
--------------------------------------------------
118+
// SENSE: 054_Query_DSL/70_Match_all_query.json
117119

118120

119121
This query is frequently used in combination with a filter, for instance to
@@ -131,8 +133,9 @@ the search:
131133

132134
[source,js]
133135
--------------------------------------------------
134-
{ "match": { "tweet": "Cool, bonsai cool" }}
136+
{ "match": { "tweet": "About Search" }}
135137
--------------------------------------------------
138+
// SENSE: 054_Query_DSL/70_Match_query.json
136139

137140
If you use it on a field containing an exact value, such as a date, a number,
138141
a boolean or a `not_analyzed` string field, then it will search for that
@@ -145,6 +148,7 @@ exact value:
145148
{ "match": { "tag": "full_text" }}
146149
{ "match": { "public": true }}
147150
--------------------------------------------------
151+
// SENSE: 054_Query_DSL/70_Match_query.json
148152

149153
TIP: For exact value searches, you probably want to use a filter instead of a
150154
query, as a filter will be cached.
@@ -169,7 +173,7 @@ fields:
169173
}
170174
}
171175
--------------------------------------------------
172-
176+
// SENSE: 054_Query_DSL/70_Multi_match_query.json
173177

174178
==== `bool` query
175179

@@ -200,14 +204,14 @@ match *both* conditions will rank even higher:
200204
"bool": {
201205
"must": { "match": { "title": "how to make millions" }},
202206
"must_not": { "match": { "tag": "spam" }},
203-
"should: [
207+
"should": [
204208
{ "match": { "tag": "starred" }},
205-
{ "match": { "date": { "gte": "2014-01-01" }}
209+
{ "range": { "date": { "gte": "2014-01-01" }}}
206210
]
207211
}
208212
}
209213
--------------------------------------------------
210-
214+
// SENSE: 054_Query_DSL/70_Bool_query.json
211215

212216
If there are no `must` clauses, then at least one `should` clause has to
213217
match, but if there is at least one `must` clause, then no `should`

054_Query_DSL/75_Queries_with_filters.asciidoc

+4-1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ GET /_search
6565
}
6666
}
6767
--------------------------------------------------
68+
// SENSE: 054_Query_DSL/75_Filtered_query.json
6869

6970

7071
==== Just a filter
@@ -84,6 +85,7 @@ GET /_search
8485
}
8586
}
8687
--------------------------------------------------
88+
// SENSE: 054_Query_DSL/75_Filtered_query.json
8789

8890

8991
If a query is not specified then it defaults to using the `match_all` query, so
@@ -130,11 +132,12 @@ GET /_search
130132
}
131133
}
132134
--------------------------------------------------
135+
// SENSE: 054_Query_DSL/75_Filtered_query.json
133136
<1> Note the `query` filter, which is allowing us to use the `match` *query*
134137
inside a `bool` *filter*.
135138

136139

137-
You seldom need to use a query as a filter, but we have included it for
140+
NOTE: You seldom need to use a query as a filter, but we have included it for
138141
completeness' sake. The only time you may need it is when you need to use
139142
full text matching while in filter context.
140143

054_Query_DSL/80_Validating_queries.asciidoc

+3-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ GET /gb/tweet/_validate/query
1515
}
1616
}
1717
--------------------------------------------------
18+
// SENSE: 054_Query_DSL/80_Validate_query.json
1819

1920

2021
The response to the above `validate` request tells us that the query is
@@ -49,6 +50,7 @@ GET /gb/tweet/_validate/query?explain <1>
4950
}
5051
}
5152
--------------------------------------------------
53+
// SENSE: 054_Query_DSL/80_Validate_query.json
5254
<1> The `explain` flag provides more information about why a query is
5355
invalid.
5456

@@ -87,7 +89,7 @@ GET /_validate/query?explain
8789
}
8890
}
8991
--------------------------------------------------
90-
92+
// SENSE: 054_Query_DSL/80_Understanding_queries.json
9193

9294
An `explanation` is returned for each index that we query, because each
9395
index can have different mapping and analyzers:
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# First load the test data
2+
# https://gist.github.com/clintongormley/8579281
3+
4+
# Bool query
5+
GET /_search
6+
{
7+
"query": {
8+
"bool": {
9+
"must": {
10+
"match": {
11+
"tweet": "elasticsearch"
12+
}
13+
},
14+
"must_not": {
15+
"match": {
16+
"name": "mary"
17+
}
18+
},
19+
"should": {
20+
"match": {
21+
"tweet": "full text"
22+
}
23+
}
24+
}
25+
}
26+
}
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# First load the test data
2+
# https://gist.github.com/clintongormley/8579281
3+
4+
# Empty query
5+
GET /_search
6+
{}
7+
8+
# Match all query
9+
GET /_search
10+
{
11+
"query": {
12+
"match_all": {}
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Match query
2+
GET /_search
3+
{
4+
"query": {
5+
"match": {
6+
"tweet": "elasticsearch"
7+
}
8+
}
9+
}
+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Delete the `test` index
2+
DELETE /test
3+
4+
# Insert some examples
5+
PUT /test/test/1
6+
{
7+
"folder": "inbox",
8+
"tag": [
9+
"work"
10+
],
11+
"starred": true,
12+
"unread": false
13+
}
14+
15+
PUT /test/test/2
16+
{
17+
"folder": "inbox",
18+
"tag": [
19+
"spam"
20+
],
21+
"starred": true,
22+
"unread": false
23+
}
24+
25+
PUT /test/test/3
26+
{
27+
"folder": "inbox",
28+
"tag": [
29+
"other"
30+
],
31+
"starred": false,
32+
"unread": true
33+
}
34+
35+
36+
37+
# Where `folder` is "inbox"
38+
# and `tag` is not spam
39+
# and either `starred` or `unread` is true
40+
GET /test/test/_search
41+
{
42+
"query": {
43+
"filtered": {
44+
"filter": {
45+
"bool": {
46+
"must": {
47+
"term": {
48+
"folder": "inbox"
49+
}
50+
},
51+
"must_not": {
52+
"term": {
53+
"tag": "spam"
54+
}
55+
},
56+
"should": [
57+
{
58+
"term": {
59+
"starred": true
60+
}
61+
},
62+
{
63+
"term": {
64+
"unread": true
65+
}
66+
}
67+
]
68+
}
69+
}
70+
}
71+
}
72+
}
+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# Delete the `test` index
2+
DELETE /test
3+
4+
# Insert some example docs
5+
PUT /test/test/1
6+
{
7+
"title": "How to make millions!",
8+
"tag": [
9+
"opportunity"
10+
],
11+
"date": "2014-01-09"
12+
}
13+
14+
PUT /test/test/2
15+
{
16+
"title": "How to make millions!",
17+
"tag": [
18+
"spam"
19+
],
20+
"date": "2014-01-09"
21+
}
22+
23+
PUT /test/test/3
24+
{
25+
"title": "How to make millions!",
26+
"tag": [
27+
"opportunity",
28+
"starred"
29+
],
30+
"date": "2013-01-09"
31+
}
32+
33+
34+
# Match "how to make millions" in the `title`
35+
# and `tag` must not include "spam"
36+
# and either `tag` should include "starred"
37+
# or `date` must be >= "2014-01-01"
38+
GET /test/test/_search
39+
{
40+
"query": {
41+
"bool": {
42+
"must": {
43+
"match": {
44+
"title": "how to make millions"
45+
}
46+
},
47+
"must_not": {
48+
"match": {
49+
"tag": "spam"
50+
}
51+
},
52+
"should": [
53+
{
54+
"match": {
55+
"tag": "starred"
56+
}
57+
},
58+
{
59+
"range": {
60+
"date": {
61+
"gte": "2014-01-01"
62+
}
63+
}
64+
}
65+
]
66+
}
67+
}
68+
}

0 commit comments

Comments
 (0)