|
| 1 | +--- |
| 2 | +title: Using multi-search to perform a federated search — Meilisearch API reference |
| 3 | +description: In this tutorial you will see how to perform a query searching multiple indexes at the same time to obtain a single list of results. |
| 4 | +--- |
| 5 | + |
| 6 | +# Using multi-search to perform a federated search |
| 7 | + |
| 8 | +Meilisearch allows you to make multiple search requests at the same time with the `/multi-search` endpoint. A federated search is a multi-search that returns results from multiple queries in a single list. |
| 9 | + |
| 10 | +In this tutorial 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 data from a fictional CRM application. |
| 20 | + |
| 21 | +Add the datasets to Meilisearch and create three separate indexes, `profiles`, `chats`, and `tickets`: |
| 22 | + |
| 23 | +```sh |
| 24 | +curl -X POST 'http://localhost:7700/indexes/profiles' -H 'Content-Type: application/json' --data-binary @crm-profiles.json && |
| 25 | +curl -X POST 'http://localhost:7700/indexes/chats' -H 'Content-Type: application/json' --data-binary @crm-chats.json && |
| 26 | +curl -X POST 'http://localhost:7700/indexes/tickets' -H 'Content-Type: application/json' --data-binary @crm-tickets.json |
| 27 | +``` |
| 28 | + |
| 29 | +[Use the tasks endpoint](/learn/async/working_with_tasks) to check the indexing status. Once Meilisearch successfully indexed all three datasets, you are ready to perform a federated search. |
| 30 | + |
| 31 | +## Perform a federated search |
| 32 | + |
| 33 | +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. |
| 34 | + |
| 35 | +Use the `/multi-search` endpoint with the `federation` parameter to query the three indexes simultaneously: |
| 36 | + |
| 37 | +```sh |
| 38 | +curl \ |
| 39 | + -X POST 'http://localhost:7700/multi-search' \ |
| 40 | + -H 'Content-Type: application/json' \ |
| 41 | + --data-binary '{ |
| 42 | + "federation": {}, |
| 43 | + "queries": [ |
| 44 | + { |
| 45 | + "indexUid": "chats", |
| 46 | + "q": "natasha" |
| 47 | + }, |
| 48 | + { |
| 49 | + "indexUid": "profiles", |
| 50 | + "q": "natasha" |
| 51 | + }, |
| 52 | + { |
| 53 | + "indexUid": "tickets", |
| 54 | + "q": "natasha" |
| 55 | + } |
| 56 | + ] |
| 57 | + }' |
| 58 | +``` |
| 59 | + |
| 60 | +Meilisearch should respond with a single list of search results: |
| 61 | + |
| 62 | +```json |
| 63 | +{ |
| 64 | + "hits": [ |
| 65 | + { |
| 66 | + "id": 0, |
| 67 | + "client_name": "Natasha Nguyen", |
| 68 | + "message": "My email is [email protected]", |
| 69 | + "time": 1727349362, |
| 70 | + "_federation": { |
| 71 | + "indexUid": "chats", |
| 72 | + "queriesPosition": 0 |
| 73 | + } |
| 74 | + }, |
| 75 | + … |
| 76 | + ], |
| 77 | + "processingTimeMs": 0, |
| 78 | + "limit": 20, |
| 79 | + "offset": 0, |
| 80 | + "estimatedTotalHits": 3, |
| 81 | + "semanticHitCount": 0 |
| 82 | +} |
| 83 | +``` |
| 84 | + |
| 85 | +## Promote results from a specific index |
| 86 | + |
| 87 | +Since this is a CRM application, users have profiles with their preferred contact information. If you want to search for Riccardo Rotondo's preferred email, you can boost documents in the `profiles` index. |
| 88 | + |
| 89 | +Use the `weight` property of the `federation` parameter to boost results coming from a specific query: |
| 90 | + |
| 91 | +```sh |
| 92 | +curl \ |
| 93 | + -X POST 'http://localhost:7700/multi-search' \ |
| 94 | + -H 'Content-Type: application/json' \ |
| 95 | + --data-binary '{ |
| 96 | + "federation": {}, |
| 97 | + "queries": [ |
| 98 | + { |
| 99 | + "indexUid": "chats", |
| 100 | + "q": "rotondo" |
| 101 | + }, |
| 102 | + { |
| 103 | + "indexUid": "profiles", |
| 104 | + "q": "rotondo", |
| 105 | + "federationOptions": { |
| 106 | + "weight": 1.2 |
| 107 | + } |
| 108 | + }, |
| 109 | + { |
| 110 | + "indexUid": "tickets", |
| 111 | + "q": "rotondo" |
| 112 | + } |
| 113 | + ] |
| 114 | + }' |
| 115 | +``` |
| 116 | + |
| 117 | +This request will lead to results from the query targeting `profile` ranking higher than documents from other queries: |
| 118 | + |
| 119 | +```json |
| 120 | +{ |
| 121 | + "hits": [ |
| 122 | + { |
| 123 | + "id": 1, |
| 124 | + "name": "Riccardo Rotondo", |
| 125 | + |
| 126 | + "_federation": { |
| 127 | + "indexUid": "profiles", |
| 128 | + "queriesPosition": 1 |
| 129 | + } |
| 130 | + }, |
| 131 | + … |
| 132 | + ], |
| 133 | + "processingTimeMs": 0, |
| 134 | + "limit": 20, |
| 135 | + "offset": 0, |
| 136 | + "estimatedTotalHits": 3, |
| 137 | + "semanticHitCount": 0 |
| 138 | +} |
| 139 | +``` |
| 140 | + |
| 141 | +## Conclusion |
| 142 | + |
| 143 | +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