Skip to content

Commit d867b41

Browse files
committed
Added joining queries
1 parent d4c3fff commit d867b41

9 files changed

+572
-0
lines changed

Joining Queries/adding-documents.md

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# Adding documents
2+
3+
## Adding departments
4+
5+
```
6+
PUT /department/_doc/1
7+
{
8+
"name": "Development",
9+
"join_field": "department"
10+
}
11+
```
12+
13+
```
14+
PUT /department/_doc/2
15+
{
16+
"name": "Marketing",
17+
"join_field": "department"
18+
}
19+
```
20+
21+
## Adding employees for departments
22+
23+
```
24+
PUT /department/_doc/3?routing=1
25+
{
26+
"name": "Bo Andersen",
27+
"age": 28,
28+
"gender": "M",
29+
"join_field": {
30+
"name": "employee",
31+
"parent": 1
32+
}
33+
}
34+
```
35+
36+
```
37+
PUT /department/_doc/4?routing=2
38+
{
39+
"name": "John Doe",
40+
"age": 44,
41+
"gender": "M",
42+
"join_field": {
43+
"name": "employee",
44+
"parent": 2
45+
}
46+
}
47+
```
48+
49+
```
50+
PUT /department/_doc/5?routing=1
51+
{
52+
"name": "James Evans",
53+
"age": 32,
54+
"gender": "M",
55+
"join_field": {
56+
"name": "employee",
57+
"parent": 1
58+
}
59+
}
60+
```
61+
62+
```
63+
PUT /department/_doc/6?routing=1
64+
{
65+
"name": "Daniel Harris",
66+
"age": 52,
67+
"gender": "M",
68+
"join_field": {
69+
"name": "employee",
70+
"parent": 1
71+
}
72+
}
73+
```
74+
75+
```
76+
PUT /department/_doc/7?routing=2
77+
{
78+
"name": "Jane Park",
79+
"age": 23,
80+
"gender": "F",
81+
"join_field": {
82+
"name": "employee",
83+
"parent": 2
84+
}
85+
}
86+
```
87+
88+
```
89+
PUT /department/_doc/8?routing=1
90+
{
91+
"name": "Christina Parker",
92+
"age": 29,
93+
"gender": "F",
94+
"join_field": {
95+
"name": "employee",
96+
"parent": 1
97+
}
98+
}
99+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Mapping document relationships
2+
3+
```
4+
PUT /department
5+
{
6+
"mappings": {
7+
"_doc": {
8+
"properties": {
9+
"join_field": {
10+
"type": "join",
11+
"relations": {
12+
"department": "employee"
13+
}
14+
}
15+
}
16+
}
17+
}
18+
}
19+
```
+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
# Multi-level relations
2+
3+
## Creating the index with mapping
4+
5+
```
6+
PUT /company
7+
{
8+
"mappings": {
9+
"_doc": {
10+
"properties": {
11+
"join_field": {
12+
"type": "join",
13+
"relations": {
14+
"company": ["department", "supplier"],
15+
"department": "employee"
16+
}
17+
}
18+
}
19+
}
20+
}
21+
}
22+
```
23+
24+
## Adding a company
25+
26+
```
27+
PUT /company/_doc/1
28+
{
29+
"name": "My Company Inc.",
30+
"join_field": "company"
31+
}
32+
```
33+
34+
## Adding a department
35+
36+
```
37+
PUT /company/_doc/2?routing=1
38+
{
39+
"name": "Development",
40+
"join_field": {
41+
"name": "department",
42+
"parent": 1
43+
}
44+
}
45+
```
46+
47+
## Adding an employee
48+
49+
```
50+
PUT /company/_doc/3?routing=1
51+
{
52+
"name": "Bo Andersen",
53+
"join_field": {
54+
"name": "employee",
55+
"parent": 2
56+
}
57+
}
58+
```
59+
60+
## Adding some more test data
61+
```
62+
PUT /company/_doc/4
63+
{
64+
"name": "Another Company, Inc.",
65+
"join_field": "company"
66+
}
67+
```
68+
69+
```
70+
PUT /company/_doc/5?routing=4
71+
{
72+
"name": "Marketing",
73+
"join_field": {
74+
"name": "department",
75+
"parent": 4
76+
}
77+
}
78+
```
79+
80+
```
81+
PUT /company/_doc/6?routing=4
82+
{
83+
"name": "John Doe",
84+
"join_field": {
85+
"name": "employee",
86+
"parent": 5
87+
}
88+
}
89+
```
90+
91+
## Example of querying multi-level relations
92+
93+
```
94+
GET /company/_search
95+
{
96+
"query": {
97+
"has_child": {
98+
"type": "department",
99+
"query": {
100+
"has_child": {
101+
"type": "employee",
102+
"query": {
103+
"term": {
104+
"name.keyword": "John Doe"
105+
}
106+
}
107+
}
108+
}
109+
}
110+
}
111+
}
112+
```

Joining Queries/nested-inner-hits.md

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Nested inner hits
2+
3+
```
4+
GET /department/_search
5+
{
6+
"_source": false,
7+
"query": {
8+
"nested": {
9+
"path": "employees",
10+
"inner_hits": {},
11+
"query": {
12+
"bool": {
13+
"must": [
14+
{
15+
"match": {
16+
"employees.position": "intern"
17+
}
18+
},
19+
{
20+
"term": {
21+
"employees.gender.keyword": {
22+
"value": "F"
23+
}
24+
}
25+
}
26+
]
27+
}
28+
}
29+
}
30+
}
31+
}
32+
```
+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Parent/child inner hits
2+
3+
## Including inner hits for the `has_child` query
4+
5+
```
6+
GET /department/_search
7+
{
8+
"query": {
9+
"has_child": {
10+
"type": "employee",
11+
"inner_hits": {},
12+
"query": {
13+
"bool": {
14+
"must": [
15+
{
16+
"range": {
17+
"age": {
18+
"gte": 50
19+
}
20+
}
21+
}
22+
],
23+
"should": [
24+
{
25+
"term": {
26+
"gender.keyword": "M"
27+
}
28+
}
29+
]
30+
}
31+
}
32+
}
33+
}
34+
}
35+
```
36+
37+
## Including inner hits for the `has_parent` query
38+
39+
```
40+
GET /department/_search
41+
{
42+
"query": {
43+
"has_parent": {
44+
"inner_hits": {},
45+
"parent_type": "department",
46+
"query": {
47+
"term": {
48+
"name.keyword": "Development"
49+
}
50+
}
51+
}
52+
}
53+
}
54+
```
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Querying by parent
2+
3+
```
4+
GET /department/_search
5+
{
6+
"query": {
7+
"parent_id": {
8+
"type": "employee",
9+
"id": 1
10+
}
11+
}
12+
}
13+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Querying child documents by parent
2+
3+
## Matching child documents by parent criteria
4+
5+
```
6+
GET /department/_search
7+
{
8+
"query": {
9+
"has_parent": {
10+
"parent_type": "department",
11+
"query": {
12+
"term": {
13+
"name.keyword": "Development"
14+
}
15+
}
16+
}
17+
}
18+
}
19+
```
20+
21+
## Incorporating the parent documents' relevance scores
22+
23+
```
24+
GET /department/_search
25+
{
26+
"query": {
27+
"has_parent": {
28+
"parent_type": "department",
29+
"score": true,
30+
"query": {
31+
"term": {
32+
"name.keyword": "Development"
33+
}
34+
}
35+
}
36+
}
37+
}
38+
```

0 commit comments

Comments
 (0)