~/snippets/aws-curl-opensearch
Published on

Use curl to query AWS OpenSearch

554 words3 min read
curl -X GET "https://...us-west-2.es.amazonaws.com" --aws-sigv4 "aws:amz:us-west-2:es" --user "$AWS_ACCESS_KEY_ID:$AWS_SECRET_ACCESS_KEY" -H "x-amz-security-token:$AWS_SESSION_TOKEN"

If you want to PUT a document with curl you need some data, and the x-amz-content-sha256 header for Amazon OpenSearch Serverless. See below for a full example that inserts some vectors and performs an approximate nearest neighbor search.

curl \
    --verbose \
    --request PUT \
    --url "$ENDPOINT/vectors" \
    --aws-sigv4 "aws:amz:$AWS_REGION:$SERVICE" \
    --user "$AWS_ACCESS_KEY_ID:$AWS_SECRET_ACCESS_KEY" \
    --header "x-amz-security-token:$AWS_SESSION_TOKEN" \
    --header "Content-Type: application/json; charset=utf-8" \
    --header "x-amz-content-sha256: UNSIGNED_PAYLOAD" \
    --data-binary @- << EOF
{
    "settings": {
        "index.knn": "true"
    },
    "mappings": {
        "properties": {
            "values": {
                "type": "knn_vector",
                "dimension": 3
            }
        }
    }
}
EOF


curl \
    --verbose \
    --request PUT \
    --url "$ENDPOINT/vectors/_doc/vec1" \
    --aws-sigv4 "aws:amz:$AWS_REGION:$SERVICE" \
    --user "$AWS_ACCESS_KEY_ID:$AWS_SECRET_ACCESS_KEY" \
    --header "x-amz-security-token:$AWS_SESSION_TOKEN" \
    --header "Content-Type: application/json; charset=utf-8" \
    --header "x-amz-content-sha256: UNSIGNED_PAYLOAD" \
    --data-binary @- << EOF
{
    "values": [0.1, 0.2, 0.3],
    "metadata": {
        "genre":"drama"
    }
}
EOF



curl \
    --verbose \
    --request PUT \
    --url "$ENDPOINT/vectors/_doc/vec2" \
    --aws-sigv4 "aws:amz:$AWS_REGION:$SERVICE" \
    --user "$AWS_ACCESS_KEY_ID:$AWS_SECRET_ACCESS_KEY" \
    --header "x-amz-security-token:$AWS_SESSION_TOKEN" \
    --header "Content-Type: application/json; charset=utf-8" \
    --header "x-amz-content-sha256: UNSIGNED_PAYLOAD" \
    --data-binary @- << EOF
{
    "values": [0.2, 0.3, 0.4],
    "metadata": {
        "genre":"action"
    }
}
EOF

curl \
    --verbose \
    --request POST \
    --url "$ENDPOINT/vectors/_search" \
    --aws-sigv4 "aws:amz:$AWS_REGION:$SERVICE" \
    --user "$AWS_ACCESS_KEY_ID:$AWS_SECRET_ACCESS_KEY" \
    --header "x-amz-security-token:$AWS_SESSION_TOKEN" \
    --header "Content-Type: application/json; charset=utf-8" \
    --header "x-amz-content-sha256: UNSIGNED_PAYLOAD" \
    --data-binary @- << EOF
{
    "query": {
        "knn": {
            "values": {
                "vector": [0.1, 0.2, 0.3],
                "k": 1
            }
        }
    }
}
EOF

curl \
    --verbose \
    --request DELETE \
    --url "$ENDPOINT/vectors" \
    --aws-sigv4 "aws:amz:$AWS_REGION:$SERVICE" \
    --user "$AWS_ACCESS_KEY_ID:$AWS_SECRET_ACCESS_KEY" \
    --header "x-amz-security-token:$AWS_SESSION_TOKEN" \
    --header "Content-Type: application/json; charset=utf-8" \

References