#!/bin/bash
# Script to add content_md field to Elasticsearch indices
# Run this on the production server before deploying the feature

# ES Configuration - update these for your environment
ES_HOST="https://shamraindex:9200"
ES_USER="elastic"
# ES_PASSWORD should be set as environment variable or read from file

if [ -z "$ES_PASSWORD" ]; then
    echo "Error: ES_PASSWORD environment variable not set"
    echo "Usage: ES_PASSWORD=your_password ./add_content_md_field.sh"
    exit 1
fi

echo "Adding content_md field to arabic_research index..."
curl -s -u "$ES_USER:$ES_PASSWORD" -X PUT "$ES_HOST/arabic_research/_mapping" \
  -H 'Content-Type: application/json' \
  -d '{
    "properties": {
      "content_md": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 32766
          }
        }
      }
    }
  }'
echo ""

echo "Adding content_md field to english_research index..."
curl -s -u "$ES_USER:$ES_PASSWORD" -X PUT "$ES_HOST/english_research/_mapping" \
  -H 'Content-Type: application/json' \
  -d '{
    "properties": {
      "content_md": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 32766
          }
        }
      }
    }
  }'
echo ""

echo "Verifying mappings..."
echo "Arabic research mapping:"
curl -s -u "$ES_USER:$ES_PASSWORD" "$ES_HOST/arabic_research/_mapping" | grep -o '"content_md"' && echo " - content_md field found" || echo " - content_md field NOT found"

echo "English research mapping:"
curl -s -u "$ES_USER:$ES_PASSWORD" "$ES_HOST/english_research/_mapping" | grep -o '"content_md"' && echo " - content_md field found" || echo " - content_md field NOT found"

echo ""
echo "Done! The content_md field has been added to both indices."
