|
| 1 | +--- |
| 2 | +title: Federated search guide — Meilisearch API reference |
| 3 | +description: |
| 4 | +--- |
| 5 | + |
| 6 | +# Federated search guide |
| 7 | + |
| 8 | +A federated search is a multi-index search that returns results across multiple indexes in a single list. |
| 9 | + |
| 10 | +In this guide you will see how to create separate indexes containing different types of data from a CRM application. You will then perform a query searching all these indexes at the same time to obtain a single list of results. |
| 11 | + |
| 12 | +## Requirements |
| 13 | + |
| 14 | +- A running Meilisearch project |
| 15 | +- A command-line console |
| 16 | + |
| 17 | +## Create three indexes |
| 18 | + |
| 19 | +Download the following datasets: <a href="/assets/datasets/crm-chats.json">`crm-chats.json`</a>, <a href="/assets/datasets/crm-profiles.json">`crm-profiles.json`</a>, and <a href="/assets/datasets/crm-tickets.json">`crm-tickets.json`</a> containing customer interaction data. Add them to Meilisearch, creating three separate indexes, `profiles`, `chats`, and `tickets`: |
| 20 | + |
| 21 | +```sh |
| 22 | +curl -X POST 'http://localhost:7700/indexes/profiles' -H 'Content-Type: application/json' --data-binary @crm-profiles.json && |
| 23 | +curl -X POST 'http://localhost:7700/indexes/chats' -H 'Content-Type: application/json' --data-binary @crm-chats.json && |
| 24 | +curl -X POST 'http://localhost:7700/indexes/tickets' -H 'Content-Type: application/json' --data-binary @crm-tickets.json |
| 25 | +``` |
| 26 | + |
| 27 | +Use the tasks endpoint to check the indexing status. Once Meilisearch successfully indexed all three datasets, you are ready to perform a federated search. |
| 28 | + |
| 29 | +## Perform a federated search |
| 30 | + |
| 31 | +When you are looking for Natasha Nguyen's email address in your CRM application, you may not know whether you will find it in a chat log, among the existing customer profiles, or in a recent support ticket. In this situation, you can use federated search to search across all possible sources and receive a single list of results. |
| 32 | + |
| 33 | +Use the `/multi-search` endpoint with the `federation` parameter to query the three indexes simultaneously: |
| 34 | + |
| 35 | +```sh |
| 36 | +curl \ |
| 37 | + -X POST 'http://localhost:7700/multi-search' \ |
| 38 | + -H 'Content-Type: application/json' \ |
| 39 | + --data-binary '{ |
| 40 | + "federation": {}, |
| 41 | + "queries": [ |
| 42 | + { |
| 43 | + "indexUid": "chats", |
| 44 | + "q": "natasha" |
| 45 | + }, |
| 46 | + { |
| 47 | + "indexUid": "profiles", |
| 48 | + "q": "natasha" |
| 49 | + }, |
| 50 | + { |
| 51 | + "indexUid": "tickets", |
| 52 | + "q": "natasha" |
| 53 | + } |
| 54 | + ] |
| 55 | + }' |
| 56 | +``` |
| 57 | + |
| 58 | +Meilisearch should respond with a single list of search results: |
| 59 | + |
| 60 | +```json |
| 61 | +{ |
| 62 | + "hits": [ |
| 63 | + { |
| 64 | + "id": 0, |
| 65 | + "client_name": "Natasha Nguyen", |
| 66 | + "message": "My email is [email protected]", |
| 67 | + "time": 1727349362, |
| 68 | + "_federation": { |
| 69 | + "indexUid": "chats", |
| 70 | + "queriesPosition": 0 |
| 71 | + } |
| 72 | + }, |
| 73 | + … |
| 74 | + ], |
| 75 | + "processingTimeMs": 0, |
| 76 | + "limit": 20, |
| 77 | + "offset": 0, |
| 78 | + "estimatedTotalHits": 3, |
| 79 | + "semanticHitCount": 0 |
| 80 | +} |
| 81 | +``` |
| 82 | + |
| 83 | +## Promote results from an index |
| 84 | + |
| 85 | +Since this is a CRM application, it is likely your users have profiles where they specified their preferred contact information. If you want to search for Riccardo Rotondo's official preferred email, you can boost documents in the `profiles` index. |
| 86 | + |
| 87 | +Use the `weight` property of the `federation` parameter to boost results coming from a specific index: |
| 88 | + |
| 89 | +```sh |
| 90 | +curl \ |
| 91 | + -X POST 'http://localhost:7700/multi-search' \ |
| 92 | + -H 'Content-Type: application/json' \ |
| 93 | + --data-binary '{ |
| 94 | + "federation": {}, |
| 95 | + "queries": [ |
| 96 | + { |
| 97 | + "indexUid": "chats", |
| 98 | + "q": "rotondo" |
| 99 | + }, |
| 100 | + { |
| 101 | + "indexUid": "profiles", |
| 102 | + "q": "rotondo", |
| 103 | + "federationOptions": { |
| 104 | + "weight": 1.2 |
| 105 | + } |
| 106 | + }, |
| 107 | + { |
| 108 | + "indexUid": "tickets", |
| 109 | + "q": "rotondo" |
| 110 | + } |
| 111 | + ] |
| 112 | + }' |
| 113 | +``` |
| 114 | + |
| 115 | +This query will result in `profile` results ranking higher than documents in other indexes: |
| 116 | + |
| 117 | +```json |
| 118 | +{ |
| 119 | + "hits": [ |
| 120 | + { |
| 121 | + "id": 1, |
| 122 | + "name": "Riccardo Rotondo", |
| 123 | + |
| 124 | + "_federation": { |
| 125 | + "indexUid": "profiles", |
| 126 | + "queriesPosition": 1 |
| 127 | + } |
| 128 | + }, |
| 129 | + … |
| 130 | + ], |
| 131 | + "processingTimeMs": 0, |
| 132 | + "limit": 20, |
| 133 | + "offset": 0, |
| 134 | + "estimatedTotalHits": 3, |
| 135 | + "semanticHitCount": 0 |
| 136 | +} |
| 137 | +``` |
| 138 | + |
| 139 | +## Conclusion |
| 140 | + |
| 141 | +You have created three indexes, then performed a federated multi-index search to receive all results in a single list. You then used `weight` to boost results from the index most likely to contain the information you wanted. |
0 commit comments