|
| 1 | +#!/bin/bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +# CUJ 7: Create and Configure a Search Index |
| 5 | +# Tests that content can be indexed into Solr 9. |
| 6 | +# Accepts MODULE env var: apachesolr or search_api_solr |
| 7 | + |
| 8 | +SITE_ENV="${TERMINUS_SITE}.${MULTIDEV}" |
| 9 | +MODULE="${MODULE:?MODULE env var must be set}" |
| 10 | + |
| 11 | +echo "=== CUJ 7: Create and Configure Search Index ($MODULE) ===" |
| 12 | + |
| 13 | +if [ "$MODULE" = "apachesolr" ]; then |
| 14 | + # Apache Solr Search: no explicit index creation needed. |
| 15 | + # Mark all content for reindexing and run indexer. |
| 16 | + |
| 17 | + echo "--- Step 1: Mark all content for reindexing ---" |
| 18 | + terminus drush "$SITE_ENV" -- solr-mark-all |
| 19 | + |
| 20 | + echo "--- Step 2: Index content ---" |
| 21 | + terminus drush "$SITE_ENV" -- solr-index |
| 22 | + |
| 23 | + echo "--- Step 3: Verify index count ---" |
| 24 | + INDEX_STATS=$(terminus drush "$SITE_ENV" -- ev " |
| 25 | + \$env_id = apachesolr_default_environment(); |
| 26 | + \$env = apachesolr_environment_load(\$env_id); |
| 27 | + try { |
| 28 | + \$solr = apachesolr_get_solr(\$env_id); |
| 29 | + \$response = \$solr->getLuke(); |
| 30 | + echo 'INDEX_COUNT:' . \$response->index->numDocs; |
| 31 | + } catch (Exception \$e) { |
| 32 | + echo 'INDEX_ERROR:' . \$e->getMessage(); |
| 33 | + } |
| 34 | + " 2>&1) |
| 35 | + |
| 36 | + if echo "$INDEX_STATS" | grep -q "INDEX_COUNT:"; then |
| 37 | + COUNT=$(echo "$INDEX_STATS" | grep -o 'INDEX_COUNT:[0-9]*' | cut -d: -f2) |
| 38 | + if [ "$COUNT" -gt 0 ]; then |
| 39 | + echo "Index contains $COUNT documents." |
| 40 | + else |
| 41 | + echo "::error::Index is empty after indexing" |
| 42 | + exit 1 |
| 43 | + fi |
| 44 | + else |
| 45 | + echo "::error::Failed to get index stats: $INDEX_STATS" |
| 46 | + exit 1 |
| 47 | + fi |
| 48 | + |
| 49 | +elif [ "$MODULE" = "search_api_solr" ]; then |
| 50 | + # Search API Solr: create index, add fields, index content. |
| 51 | + |
| 52 | + echo "--- Step 1: Create Search API index ---" |
| 53 | + INDEX_RESULT=$(terminus drush "$SITE_ENV" -- ev " |
| 54 | + \$index = entity_create('search_api_index', array( |
| 55 | + 'name' => 'Site Content', |
| 56 | + 'machine_name' => 'site_content', |
| 57 | + 'server' => 'pantheon_solr9', |
| 58 | + 'item_type' => 'node', |
| 59 | + 'enabled' => 1, |
| 60 | + 'options' => array( |
| 61 | + 'index_directly' => 1, |
| 62 | + 'fields' => array( |
| 63 | + 'title' => array('type' => 'text'), |
| 64 | + 'body:value' => array('type' => 'text'), |
| 65 | + 'type' => array('type' => 'string'), |
| 66 | + 'status' => array('type' => 'boolean'), |
| 67 | + ), |
| 68 | + ), |
| 69 | + )); |
| 70 | + \$index->save(); |
| 71 | + echo \$index->machine_name ? 'INDEX_CREATED' : 'INDEX_FAILED'; |
| 72 | + " 2>&1) |
| 73 | + |
| 74 | + if echo "$INDEX_RESULT" | grep -q "INDEX_CREATED"; then |
| 75 | + echo "Search API index created." |
| 76 | + else |
| 77 | + echo "::error::Failed to create Search API index: $INDEX_RESULT" |
| 78 | + exit 1 |
| 79 | + fi |
| 80 | + |
| 81 | + echo "--- Step 2: Index content ---" |
| 82 | + terminus drush "$SITE_ENV" -- search-api-index site_content |
| 83 | + |
| 84 | + echo "--- Step 3: Verify index status ---" |
| 85 | + STATUS=$(terminus drush "$SITE_ENV" -- search-api-status site_content 2>&1) |
| 86 | + echo "$STATUS" |
| 87 | + |
| 88 | + if echo "$STATUS" | grep -q "100%"; then |
| 89 | + echo "Index is 100% complete." |
| 90 | + else |
| 91 | + # Check if items were indexed even if not 100% |
| 92 | + INDEXED=$(echo "$STATUS" | grep -o 'Indexed[[:space:]]*[0-9]*' | grep -o '[0-9]*') |
| 93 | + if [ -n "$INDEXED" ] && [ "$INDEXED" -gt 0 ]; then |
| 94 | + echo "Warning: Index not 100% but $INDEXED items indexed." |
| 95 | + else |
| 96 | + echo "::error::No items indexed: $STATUS" |
| 97 | + exit 1 |
| 98 | + fi |
| 99 | + fi |
| 100 | + |
| 101 | +else |
| 102 | + echo "::error::Unknown module: $MODULE" |
| 103 | + exit 1 |
| 104 | +fi |
| 105 | + |
| 106 | +echo "=== CUJ 7 PASSED: $MODULE index created and content indexed ===" |
0 commit comments