|
4 | 4 | <titleabbrev>Parent ID</titleabbrev>
|
5 | 5 | ++++
|
6 | 6 |
|
7 |
| -The `parent_id` query can be used to find child documents which belong to a particular parent. |
8 |
| -Given the following mapping definition: |
| 7 | +Returns child documents <<parent-join,joined>> to a specific parent document. |
| 8 | +You can use a <<parent-join,join>> field mapping to create parent-child |
| 9 | +relationships between documents in the same index. |
9 | 10 |
|
| 11 | +[[parent-id-query-ex-request]] |
| 12 | +==== Example request |
| 13 | + |
| 14 | +[[parent-id-index-setup]] |
| 15 | +===== Index setup |
| 16 | +To use the `parent_id` query, your index must include a <<parent-join,join>> |
| 17 | +field mapping. To see how you can set up an index for the `parent_id` query, try |
| 18 | +the following example. |
| 19 | + |
| 20 | +. Create an index with a <<parent-join,join>> field mapping. |
| 21 | ++ |
| 22 | +-- |
10 | 23 | [source,js]
|
11 |
| --------------------------------------------- |
12 |
| -PUT my_index |
| 24 | +---- |
| 25 | +PUT /my-index |
13 | 26 | {
|
14 |
| - "mappings": { |
15 |
| - "properties": { |
16 |
| - "my_join_field": { |
17 |
| - "type": "join", |
18 |
| - "relations": { |
19 |
| - "my_parent": "my_child" |
| 27 | + "mappings": { |
| 28 | + "properties" : { |
| 29 | + "my-join-field" : { |
| 30 | + "type" : "join", |
| 31 | + "relations": { |
| 32 | + "my-parent": "my-child" |
| 33 | + } |
| 34 | + } |
20 | 35 | }
|
21 |
| - } |
22 | 36 | }
|
23 |
| - } |
24 | 37 | }
|
25 | 38 |
|
26 |
| -PUT my_index/_doc/1?refresh |
| 39 | +---- |
| 40 | +// CONSOLE |
| 41 | +// TESTSETUP |
| 42 | +-- |
| 43 | + |
| 44 | +. Index a parent document with an ID of `1`. |
| 45 | ++ |
| 46 | +-- |
| 47 | +[source,js] |
| 48 | +---- |
| 49 | +PUT /my-index/_doc/1?refresh |
27 | 50 | {
|
28 |
| - "text": "This is a parent document", |
29 |
| - "my_join_field": "my_parent" |
| 51 | + "text": "This is a parent document.", |
| 52 | + "my-join-field": "my-parent" |
30 | 53 | }
|
| 54 | +---- |
| 55 | +// CONSOLE |
| 56 | +-- |
31 | 57 |
|
32 |
| -PUT my_index/_doc/2?routing=1&refresh |
| 58 | +. Index a child document of the parent document. |
| 59 | ++ |
| 60 | +-- |
| 61 | +[source,js] |
| 62 | +---- |
| 63 | +PUT /my-index/_doc/2?routing=1&refresh |
33 | 64 | {
|
34 |
| - "text": "This is a child document", |
| 65 | + "text": "This is a child document.", |
35 | 66 | "my_join_field": {
|
36 |
| - "name": "my_child", |
| 67 | + "name": "my-child", |
37 | 68 | "parent": "1"
|
38 | 69 | }
|
39 | 70 | }
|
40 |
| -
|
41 |
| --------------------------------------------- |
| 71 | +---- |
42 | 72 | // CONSOLE
|
43 |
| -// TESTSETUP |
| 73 | +-- |
| 74 | + |
| 75 | +[[parent-id-query-ex-query]] |
| 76 | +===== Example query |
| 77 | + |
| 78 | +The following search returns child documents for a parent document with an ID of |
| 79 | +`1`. |
44 | 80 |
|
45 | 81 | [source,js]
|
46 |
| --------------------------------------------------- |
47 |
| -GET /my_index/_search |
| 82 | +---- |
| 83 | +GET /my-index/_search |
48 | 84 | {
|
49 | 85 | "query": {
|
50 |
| - "parent_id": { |
51 |
| - "type": "my_child", |
52 |
| - "id": "1" |
53 |
| - } |
| 86 | + "parent_id": { |
| 87 | + "type": "my-child", |
| 88 | + "id": "1" |
| 89 | + } |
54 | 90 | }
|
55 | 91 | }
|
56 |
| --------------------------------------------------- |
| 92 | +---- |
57 | 93 | // CONSOLE
|
58 | 94 |
|
| 95 | +[[parent-id-top-level-params]] |
| 96 | +==== Top-level parameters for `parent_id` |
| 97 | + |
| 98 | +`type`:: |
| 99 | +(Required, string) Name of the child relationship mapped for the |
| 100 | +<<parent-join,join>> field. |
59 | 101 |
|
60 |
| -==== Parameters |
| 102 | +`id`:: |
| 103 | +(Required, string) ID of the parent document. The query will return child |
| 104 | +documents of this parent document. |
61 | 105 |
|
62 |
| -This query has two required parameters: |
| 106 | +`ignore_unmapped`:: |
| 107 | ++ |
| 108 | +-- |
| 109 | +(Optional, boolean) Indicates whether to ignore an unmapped `type` and not |
| 110 | +return any documents instead of an error. Defaults to `false`. |
63 | 111 |
|
64 |
| -[horizontal] |
65 |
| -`type`:: The **child** type name, as specified in the <<parent-join,`join` field>>. |
66 |
| -`id`:: The ID of the parent document. |
| 112 | +If `false`, {es} returns an error if the `type` is unmapped. |
67 | 113 |
|
68 |
| -`ignore_unmapped`:: When set to `true` this will ignore an unmapped `type` and will not match any |
69 |
| -documents for this query. This can be useful when querying multiple indexes |
70 |
| -which might have different mappings. When set to `false` (the default value) |
71 |
| -the query will throw an exception if the `type` is not mapped. |
| 114 | +You can use this parameter to query multiple indices that may not contain the |
| 115 | +`type`. |
| 116 | +-- |
0 commit comments