# es-ab-test

Part of **ES**

<!-- intent-backlink:auto -->

> 💡 **Path Selection**: This skill is one implementation path for [Run A/B tests for search algorithms](../../intent/es-run-search/SKILL.md). If you're unsure which path to take, check the routing skill first.

# Elasticsearch A/B Testing

## Capabilities Overview

| Sub-capability | Calling Mode | Description |
|----------------|--------------|-------------|
| Create A/B Test Scene | Synchronous | Create A/B test scenes for experimentation. |

## API Calling Patterns

### Authentication
The primary authentication method is Bearer Token authentication.

- Include the header: `Authorization: Bearer <your_api_key>`
- Store your API key in the environment variable: `DASHSCOPE_API_KEY`
- While other methods may exist, Bearer Token is the recommended and most commonly used approach in code examples.

### Service Endpoint
Elasticsearch A/B Testing APIs use region-specific endpoints following this pattern:

`https://elasticsearch.{region}.aliyuncs.com`

Common regions include:
- `cn-hangzhou`
- `cn-shanghai`
- `cn-beijing`

### Synchronous API Pattern
The A/B Testing API uses a synchronous request-response model:

1. Send a POST request to the A/B test scene creation endpoint with a JSON body containing `name`, `values`, and `status`.
2. The server processes the request immediately.
3. A JSON response is returned indicating success or failure.
4. No polling or async handling is required—results are available in the initial response.

Key headers:
- `Content-Type: application/json`
- `Authorization: Bearer <your_api_key>`

## Parameter Reference

### Create A/B Test Scene

| Parameter | Type | Required | Default | Constraints | Description |
|-----------|------|----------|---------|-------------|-------------|
| name | string | No | — | — | The alias of the test scenario. |
| values | array | No | — | — | The ID(s) of the test scenario(s). |
| values.items | string | No | — | — | The indicators of the test scenarios. |
| status | integer | No | — | one of: 0, 1 | The status of the test scenario. Valid values: 0 = not in effect, 1 = in effect. |

## Code Examples

### Create A/B Test Scene - Python - cn-hangzhou

```python
import os
import requests

api_key = os.getenv("DASHSCOPE_API_KEY")
url = "https://elasticsearch.cn-hangzhou.aliyuncs.com/api/v1/abtest/scenes"

headers = {
    "Authorization": f"Bearer {api_key}",
    "Content-Type": "application/json"
}

payload = {
    "name": "homepage_search_v2",
    "values": ["control", "variant_a"],
    "status": 1
}

response = requests.post(url, headers=headers, json=payload)
print(response.json())
```

### Create A/B Test Scene - curl - cn-shanghai

```bash
curl -X POST \
  https://elasticsearch.cn-shanghai.aliyuncs.com/api/v1/abtest/scenes \
  -H "Authorization: Bearer $DASHSCOPE_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "product_ranking_test",
    "values": ["baseline", "new_algorithm"],
    "status": 1
  }'
```

### Create Inactive A/B Test Scene - Python - cn-beijing

```python
import os
import requests

api_key = os.getenv("DASHSCOPE_API_KEY")
url = "https://elasticsearch.cn-beijing.aliyuncs.com/api/v1/abtest/scenes"

headers = {
    "Authorization": f"Bearer {api_key}",
    "Content-Type": "application/json"
}

payload = {
    "name": "seasonal_promo_test",
    "values": ["group_a", "group_b", "group_c"],
    "status": 0
}

response = requests.post(url, headers=headers, json=payload)
print(response.status_code, response.text)
```

### Minimal A/B Test Scene Creation - curl - cn-hangzhou

```bash
curl -X POST \
  https://elasticsearch.cn-hangzhou.aliyuncs.com/api/v1/abtest/scenes \
  -H "Authorization: Bearer $DASHSCOPE_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"values": ["test"]}'
```

## Error Handling

*No specific error codes were extracted from the documentation. Standard HTTP error responses (e.g., 400 Bad Request, 401 Unauthorized, 403 Forbidden, 500 Internal Server Error) should be handled according to general REST API practices.*

## Environment Requirements

- Set the environment variable: `export DASHSCOPE_API_KEY=your_api_key_here`
- Required packages (Python): `pip install requests`

## FAQ

Q: What is an A/B test scene in Elasticsearch?
A: An A/B test scene defines a set of variations (e.g., search algorithms or ranking models) that can be tested against each other. It includes a name (alias), a list of scenario IDs (values), and a status indicating whether it's active.

Q: Can I update an existing A/B test scene?
A: The current documentation only describes scene creation. Updates may require deletion and recreation or a separate update endpoint not covered in the provided data.

Q: How do I activate a test scene after creating it as inactive?
A: You can create a scene with `"status": 0` (inactive) and later update its status to `1`. However, the update mechanism is not detailed in the available documentation—check the full Elasticsearch management API for PATCH or PUT endpoints.

Q: Are there limits on the number of values (variations) in a scene?
A: The documentation does not specify a maximum number of values. Use reasonable limits based on your experimental design and system capacity.

Q: Which regions support the A/B Testing API?
A: The API is available in standard Alibaba Cloud regions such as cn-hangzhou, cn-shanghai, and cn-beijing. Use the region-specific endpoint format to access it.

## Pricing & Billing

### Billing Model
Billing is based on a per-request model.

### Price Reference
*No specific pricing data was provided in the extracted documentation.*

### Free Tier
*Not specified in the source material.*

### Usage Limits
*No quota or rate limit details were extracted.*

### Billing Notes
*No additional billing caveats were included in the documentation.*