|
| 1 | +--- |
| 2 | +ms.topic: include |
| 3 | +ms.date: 10/26/2022 |
| 4 | +author: HeidiSteen |
| 5 | +ms.author: heidist |
| 6 | +ms.service: cognitive-search |
| 7 | +--- |
| 8 | + |
| 9 | +Deploy the search-enabled website as an Azure Static Web Apps site. This deployment includes both the React app and the Function app. |
| 10 | + |
| 11 | +The Static Web app pulls the information and files for deployment from GitHub using your fork of the samples repository. |
| 12 | + |
| 13 | +## Create a Static Web App in Visual Studio Code |
| 14 | + |
| 15 | +1. Select **Azure** from the Activity Bar, then open **Resources** from the Side bar. |
| 16 | + |
| 17 | +1. Right-click **Static Web Apps** and then select **Create Static Web App (Advanced)**. |
| 18 | + |
| 19 | + :::image type="content" source="../media/tutorial-javascript-create-load-index/visual-studio-code-create-static-web-app-resource-advanced.png" alt-text="Screenshot of Visual Studio Code, with the Azure Static Web Apps explorer showing the option to create an advanced static web app."::: |
| 20 | + |
| 21 | +1. If you see a pop-up window in VS Code asking which branch you want to deploy from, select the default branch, usually **master** or **main**. |
| 22 | + |
| 23 | + This setting means only changes you commit to that branch are deployed to your static web app. |
| 24 | + |
| 25 | +1. If you see a pop-up window asking you to commit your changes, don't do this. The secrets from the bulk import step shouldn't be committed to the repository. |
| 26 | + |
| 27 | + To roll back the changes, in VS Code select the Source Control icon in the Activity bar, then select each changed file in the Changes list and select the **Discard changes** icon. |
| 28 | + |
| 29 | +1. Follow the prompts to provide the following information: |
| 30 | + |
| 31 | + |Prompt|Enter| |
| 32 | + |--|--| |
| 33 | + |Select a resource group for new resources.|Use the resource group you created for this tutorial.| |
| 34 | + |Enter the name for the new Static Web App.|Create a unique name for your resource. For example, you can prepend your name to the repository name such as, `joansmith-azure-search-dotnet-samples`. | |
| 35 | + |Select a SKU| Select the free SKU for this tutorial.| |
| 36 | + |Select a location for new resources.|Select a region close to you.| |
| 37 | + |Choose build preset to configure default project structure.|Select **Custom**| |
| 38 | + |Select the location of your application code|`search-website-functions-v4/client`<br><br>This is the path, from the root of the repository, to your static web app. | |
| 39 | + |Enter the path of your build output...|`build`<br><br>This is the path, from your static web app, to your generated files.| |
| 40 | + |
| 41 | +1. The resource is created and a notification window appears. |
| 42 | + |
| 43 | + When the resource is created, it creates a GitHub action file on GitHub. |
| 44 | + |
| 45 | +1. Select **Open Actions in GitHub** from the Notifications. This opens a browser window pointed to your forked repo. |
| 46 | + |
| 47 | + Wait until the _workflow_ completes before continuing. This may take a minute or two to finish. |
| 48 | + |
| 49 | +1. Pull the new GitHub action file to your local computer by synchronizing your local fork with your remote fork: |
| 50 | + |
| 51 | + ```bash |
| 52 | + git pull origin main |
| 53 | + ``` |
| 54 | + |
| 55 | + * _origin_ refers to your forked repo. |
| 56 | + * _main_ refers to the default branch. |
| 57 | + |
| 58 | +1. In Visual Studio file explorer, find and open the workflow file in the `./.github/workflows/` directory. The file path and name looks _something_ `.github\workflows\azure-static-web-apps-lemon-mushroom-0e1bd060f.yml`. |
| 59 | + |
| 60 | + The _part_ of the YAML file relevant to the static web app is shown below: |
| 61 | + |
| 62 | + :::code language="yml" source="~/azure-search-javascript-samples/search-website-functions-v4/example-github-action.yml" highlight="28-33"::: |
| 63 | + |
| 64 | +1. Edit your action file to contain the `api_location` property. If your local file doesn't have the property, add it below the `app_location` property. |
| 65 | +
|
| 66 | + ```yml |
| 67 | + api_location: "search-website-functions-v4/api" |
| 68 | + ``` |
| 69 | +
|
| 70 | +1. Commit changes to your local repository. |
| 71 | +
|
| 72 | + ```bash |
| 73 | + git add *.yml && git commit -m "update action for static web app" |
| 74 | + ``` |
| 75 | +
|
| 76 | +1. Push changes to GitHub. |
| 77 | +
|
| 78 | + ```bash |
| 79 | + git push origin main |
| 80 | + ``` |
| 81 | +
|
| 82 | + The updated action in your remote fork creates a new build and deploy to your static web app. Wait until the _workflow_ completes before continuing. This may take a minute or two to finish. |
| 83 | +
|
| 84 | +## Get Cognitive Search query key in Visual Studio Code |
| 85 | +
|
| 86 | +1. In Visual Studio Code, open the [Activity bar](https://code.visualstudio.com/docs/getstarted/userinterface), and select the Azure icon. |
| 87 | +
|
| 88 | +1. In the Side bar, select your Azure subscription under the **Azure: Cognitive Search** area, then right-click on your Search resource and select **Copy Query Key**. |
| 89 | +
|
| 90 | + :::image type="content" source="../media/tutorial-javascript-create-load-index/visual-studio-code-copy-query-key.png" alt-text="Screenshot of Visual Studio Code showing the Azure Cognitive Search explorer, with the Copy Query Key option shown."::: |
| 91 | +
|
| 92 | +1. Keep this query key, you'll need to use it in the next section. The query key is able to query your Index. |
| 93 | + |
| 94 | +## Add configuration settings in Azure portal |
| 95 | + |
| 96 | +The Azure Function app won't return Search data until the Search secrets are in settings. |
| 97 | +
|
| 98 | +1. Select **Azure** from the Activity Bar. |
| 99 | +1. Right-click on your Static Web Apps resource then select **Open in Portal**. |
| 100 | +
|
| 101 | + :::image type="content" source="../media/tutorial-javascript-static-web-app/open-static-web-app-in-azure-portal.png" alt-text="Screenshot of Visual Studio Code showing Azure Static Web Apps explorer with the Open in Portal option shown."::: |
| 102 | +
|
| 103 | +1. Select **Configuration** then select **+ Add**. |
| 104 | +
|
| 105 | + :::image type="content" source="../media/tutorial-javascript-static-web-app/add-new-application-setting-to-static-web-app-in-portal.png" alt-text="Screenshot of Visual Studio Code showing the Azure Static Web Apps explorer with the Configuration option shown."::: |
| 106 | +
|
| 107 | +1. Add each of the following settings: |
| 108 | +
|
| 109 | + |Setting|Your Search resource value| |
| 110 | + |--|--| |
| 111 | + |SearchApiKey|Your Search query key| |
| 112 | + |SearchServiceName|Your Search resource name| |
| 113 | + |SearchIndexName|`good-books`| |
| 114 | + |SearchFacets|`authors*,language_code`| |
| 115 | +
|
| 116 | + Azure Cognitive Search requires different syntax for filtering collections than it does for strings. Add a `*` after a field name to denote that the field is of type `Collection(Edm.String)`. This allows the Azure Function to add filters correctly to queries. |
| 117 | +
|
| 118 | +1. Select **Save** to save the settings. |
| 119 | +
|
| 120 | + :::image type="content" source="../media/tutorial-javascript-static-web-app/save-new-application-setting-to-static-web-app-in-portal.png" alt-text="Screenshot of browser showing Azure portal with the button to save the settings for your app.."::: |
| 121 | +
|
| 122 | +1. Return to VS Code. |
| 123 | +1. Refresh your static web app to see the application settings. |
| 124 | +
|
| 125 | + :::image type="content" source="../media/tutorial-javascript-static-web-app/visual-studio-code-extension-fresh-resource.png" alt-text="Screenshot of Visual Studio Code showing the Azure Static Web Apps explorer with the new application settings."::: |
| 126 | +
|
| 127 | +## Use search in your static web app |
| 128 | +
|
| 129 | +1. In Visual Studio Code, open the [Activity bar](https://code.visualstudio.com/docs/getstarted/userinterface), and select the Azure icon. |
| 130 | +1. In the Side bar, **right-click on your Azure subscription** under the `Static Web Apps` area and find the static web app you created for this tutorial. |
| 131 | +1. Right-click the static web app name and select **Browse site**. |
| 132 | + |
| 133 | + :::image type="content" source="../media/tutorial-javascript-create-load-index/visual-studio-code-browse-static-web-app.png" alt-text="Screenshot of Visual Studio Code showing the Azure Static Web Apps explorer showing the **Browse site** option."::: |
| 134 | +
|
| 135 | +1. Select **Open** in the pop-up dialog. |
| 136 | +1. In the website search bar, enter a search query such as `code`, so the suggest feature suggests book titles. Select a suggestion or continue entering your own query. Press enter when you've completed your search query. |
| 137 | +1. Review the results then select one of the books to see more details. |
| 138 | + |
| 139 | + |
| 140 | +## Troubleshooting |
| 141 | + |
| 142 | +[!INCLUDE [tutorial-troubleshooting](tutorial-add-search-website-troubleshooting.md)] |
| 143 | + |
| 144 | +## Clean up resources |
| 145 | + |
| 146 | +To clean up the resources created in this tutorial, delete the resource group. |
| 147 | + |
| 148 | +1. In Visual Studio Code, open the [Activity bar](https://code.visualstudio.com/docs/getstarted/userinterface), and select the Azure icon. |
| 149 | + |
| 150 | +1. In the Side bar, **right-click on your Azure subscription** under the `Resource Groups` area and find the resource group you created for this tutorial. |
| 151 | +1. Right-click the resource group name then select **Delete**. |
| 152 | + This deletes both the Search and Static Web Apps resources. |
| 153 | +1. If you no longer want the GitHub fork of the sample, remember to delete that on GitHub. Go to your fork's **Settings** then delete the fork. |
0 commit comments