Skip to content

Add random sort aggregation plugin #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion _aggregations/bucket/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ OpenSearch supports the following bucket aggregations:
- [Multi-terms]({{site.url}}{{site.baseurl}}/aggregations/bucket/multi-terms/)
- [Nested]({{site.url}}{{site.baseurl}}/aggregations/bucket/nested/)
- [Range]({{site.url}}{{site.baseurl}}/aggregations/bucket/range/)
- [Random sort]({{site.url}}{{site.baseurl}}/aggregations/bucket/random-sort/)
- [Reverse nested]({{site.url}}{{site.baseurl}}/aggregations/bucket/reverse-nested/)
- [Sampler]({{site.url}}{{site.baseurl}}/aggregations/bucket/sampler/)
- [Significant terms]({{site.url}}{{site.baseurl}}/aggregations/bucket/significant-terms/)
- [Significant text]({{site.url}}{{site.baseurl}}/aggregations/bucket/significant-text/)
- [Terms]({{site.url}}{{site.baseurl}}/aggregations/bucket/terms/)
- [Terms]({{site.url}}{{site.baseurl}}/aggregations/bucket/terms/)
100 changes: 100 additions & 0 deletions _aggregations/bucket/random-sort.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
---
layout: default
title: Random sort
parent: Bucket aggregations
nav_order: 165
---

# Random sort

The random sort plugin allows you to sort search results randomly. This can be useful for implementing features like randomized search results or A/B testing.

## Installation

The random sort plugin is not installed by default. To install it, use the plugin install command:

```bash
bin/opensearch-plugin install random-sort
```

## Usage

To use random sorting, add a `sort` parameter to your search request with the following syntax:

```json
{
"sort": [
{
"_script": {
"type": "number",
"script": {
"lang": "randomsort",
"source": "random_sort"
},
"order": "asc"
}
}
]
}
```

You can optionally provide a `seed` parameter to ensure consistent randomization across requests:

```json
{
"sort": [
{
"_script": {
"type": "number",
"script": {
"lang": "randomsort",
"source": "random_sort",
"params": {
"seed": 1234
}
},
"order": "asc"
}
}
]
}
```

## Example

Here's a complete example of a search request using random sort:

```json
GET /my-index/_search
{
"size": 10,
"query": {
"match_all": {}
},
"sort": [
{
"_script": {
"type": "number",
"script": {
"lang": "randomsort",
"source": "random_sort",
"params": {
"seed": 1234
}
},
"order": "asc"
}
}
]
}
```

This will return 10 documents from `my-index`, sorted in a random order. The randomization will be consistent across requests as long as the same seed is used.

## How it works

The random sort plugin implements a custom script engine that generates a random number for each document. This number is then used as the sort key. When a seed is provided, it ensures that the same sequence of random numbers is generated for each document across multiple requests.

## Performance considerations

While random sorting can be useful, it may impact query performance, especially on large datasets. The plugin has to generate a random number for each matching document, which can be computationally expensive. Use this feature judiciously and consider its impact on your overall system performance.
68 changes: 68 additions & 0 deletions _plugins/search-history.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
---
layout: default
title: Search history
nav_order: 140
has_children: false
parent: Plugins
---

# Search history plugin

The search history plugin allows you to save and retrieve a user's search history in OpenSearch. This can be useful for enabling features like recent searches or personalized search suggestions.

## Installation

The search history plugin is included with OpenSearch by default. No additional installation steps are required.

## Configuration

The plugin has two main configuration settings that can be adjusted in `opensearch.yml`:

```yaml
search.history.max_size: 100 # Maximum number of searches to store per user
search.history.retention_days: 30 # Number of days to retain search history
```

## Usage

The plugin provides REST APIs for saving and retrieving search history:

### Save search history

```
POST /_search_history
{
"query": "my search query",
"indices": ["index1", "index2"],
"hit_count": 53
}
```

This saves the search query, indices searched, and number of hits to the user's search history.

### Retrieve search history

```
GET /_search_history?from=0&size=20
```

This retrieves the user's search history, paginated by the `from` and `size` parameters.

### Delete search history

```
DELETE /_search_history?query=my+search+query
```

This deletes matching entries from the user's search history.

## Security

The plugin uses the authenticated user's ID to segregate and secure search history data. Users can only access their own search history.

## Limitations

- The plugin currently stores search history data in-memory and is not designed for production use with large volumes of users/searches.
- Search history is not persisted across cluster restarts.

For more details on the plugin implementation, see the [SearchHistoryPlugin](https://github.com/opensearch-project/OpenSearch/blob/main/plugins/search-history/src/main/java/org/opensearch/plugin/searchhistory/SearchHistoryPlugin.java) and [SearchHistoryService](https://github.com/opensearch-project/OpenSearch/blob/main/plugins/search-history/src/main/java/org/opensearch/plugin/searchhistory/SearchHistoryService.java) classes.