|
| 1 | +[[shared-index]] |
| 2 | +=== Shared index |
| 3 | + |
| 4 | +We can use a large shared index for the many smaller forums by indexing |
| 5 | +the forum identifier in a field and using it as a filter: |
| 6 | + |
| 7 | +[source,json] |
| 8 | +------------------------------ |
| 9 | +PUT /forums |
| 10 | +{ |
| 11 | + "settings": { |
| 12 | + "number_of_shards": 10 <1> |
| 13 | + }, |
| 14 | + "mappings": { |
| 15 | + "post": { |
| 16 | + "properties": { |
| 17 | + "forum_id": { <2> |
| 18 | + "type": "string", |
| 19 | + "index": "not_analyzed" |
| 20 | + } |
| 21 | + } |
| 22 | + } |
| 23 | + } |
| 24 | +} |
| 25 | +
|
| 26 | +PUT /forums/post/1 |
| 27 | +{ |
| 28 | + "forum_id": "baking", <2> |
| 29 | + "title": "Easy recipe for ginger nuts", |
| 30 | + ... |
| 31 | +} |
| 32 | +------------------------------ |
| 33 | +<1> Create an index large enough to hold thousands of smaller forums. |
| 34 | +<2> Each post must include a `forum_id` to identify which forum it belongs |
| 35 | + to. |
| 36 | + |
| 37 | +We can use the `forum_id` as a filter to search within a single forum. The |
| 38 | +filter will exclude most of the documents in the index (those from other |
| 39 | +forums) and filter caching will ensure that responses are fast: |
| 40 | + |
| 41 | +[source,json] |
| 42 | +------------------------------ |
| 43 | +GET /forums/post/_search |
| 44 | +{ |
| 45 | + "query": { |
| 46 | + "filtered": { |
| 47 | + "query": { |
| 48 | + "match": { |
| 49 | + "title": "ginger nuts" |
| 50 | + } |
| 51 | + }, |
| 52 | + "filter": { |
| 53 | + "term": { <1> |
| 54 | + "forum_id": { |
| 55 | + "baking" |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | +} |
| 62 | +------------------------------ |
| 63 | +<1> The `term` filter is cached by default. |
| 64 | + |
| 65 | +This approach works, but we can do better. The posts from a single forum |
| 66 | +would fit easily onto one shard but currently they are scattered across all 10 |
| 67 | +shards in the index. This means that every search request has to be forwarded |
| 68 | +to a primary or replica of all 10 shards. What would be ideal is to ensure |
| 69 | +that all the posts from a single forum are stored on the same shard. |
| 70 | + |
| 71 | +In <<routing-value>>, we explained that a document is allocated to a |
| 72 | +particular shard using this formula: |
| 73 | + |
| 74 | + shard = hash(routing) % number_of_primary_shards |
| 75 | + |
| 76 | +The `routing` value defaults to the document's `_id`, but we can override that |
| 77 | +and provide our own custom routing value, such as the `forum_id`. All |
| 78 | +documents with the same `routing` value will be stored on the same shard: |
| 79 | + |
| 80 | +[source,json] |
| 81 | +------------------------------ |
| 82 | +PUT /forums/post/1?routing=baking <1> |
| 83 | +{ |
| 84 | + "forum_id": "baking", <1> |
| 85 | + "title": "Easy recipe for ginger nuts", |
| 86 | + ... |
| 87 | +} |
| 88 | +------------------------------ |
| 89 | +<1> Using the `forum_id` as the routing value ensures that all posts from the |
| 90 | + same forum are stored on the same shard. |
| 91 | + |
| 92 | +When we search for posts in a particular forum, we can pass the same `routing` |
| 93 | +value to ensure that the search request is only run on the single shard that |
| 94 | +holds our documents: |
| 95 | + |
| 96 | +[source,json] |
| 97 | +------------------------------ |
| 98 | +GET /forums/post/_search?routing=baking <1> |
| 99 | +{ |
| 100 | + "query": { |
| 101 | + "filtered": { |
| 102 | + "query": { |
| 103 | + "match": { |
| 104 | + "title": "ginger nuts" |
| 105 | + } |
| 106 | + }, |
| 107 | + "filter": { |
| 108 | + "term": { <2> |
| 109 | + "forum_id": { |
| 110 | + "baking" |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | +} |
| 117 | +------------------------------ |
| 118 | +<1> The query is only run on the shard that corresponds to this `routing` value. |
| 119 | +<2> We still need the filter, as a single shard can hold posts from many forums. |
| 120 | + |
| 121 | +Multiple forums can be queried by passing a comma-separated list of `routing` |
| 122 | +values, and including each `forum_id` in a `terms` filter: |
| 123 | + |
| 124 | +[source,json] |
| 125 | +------------------------------ |
| 126 | +GET /forums/post/_search?routing=baking,cooking,recipes <1> |
| 127 | +{ |
| 128 | + "query": { |
| 129 | + "filtered": { |
| 130 | + "query": { |
| 131 | + "match": { |
| 132 | + "title": "ginger nuts" |
| 133 | + } |
| 134 | + }, |
| 135 | + "filter": { |
| 136 | + "terms": { |
| 137 | + "forum_id": { |
| 138 | + [ "baking", "cooking", "recipes" ] |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | +} |
| 145 | +------------------------------ |
| 146 | + |
| 147 | +While this approach is technically efficient, it looks a bit clumsy because of |
| 148 | +the need to specify `routing` values and `terms` filters on every query or |
| 149 | +indexing request. Things look a lot better once we add index aliases into the |
| 150 | +mix. |
| 151 | + |
| 152 | + |
0 commit comments