Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/ahoo-wang/elasticsearch-script-velocity

Bring Velocity to EalsticSearch
https://github.com/ahoo-wang/elasticsearch-script-velocity

elasticsearch script search-template velocity

Last synced: 4 months ago
JSON representation

Bring Velocity to EalsticSearch

Awesome Lists containing this project

README

        

# Velocity for Elasticsearch

[![License](https://img.shields.io/badge/license-Apache%202-4EB1BA.svg)](https://www.apache.org/licenses/LICENSE-2.0.html)
[![GitHub release](https://img.shields.io/github/release/Ahoo-Wang/elasticsearch-script-velocity.svg)](https://github.com/Ahoo-Wang/elasticsearch-script-velocity/releases)
[![codecov](https://codecov.io/gh/Ahoo-Wang/elasticsearch-script-velocity/graph/badge.svg?token=QK8XZXHBZN)](https://codecov.io/gh/Ahoo-Wang/elasticsearch-script-velocity)
![Integration Test Status](https://github.com/Ahoo-Wang/elasticsearch-script-velocity/actions/workflows/integration-test.yml/badge.svg)

*Search Template* is a highly valuable feature in Elasticsearch. It allows the pre-definition of the query structure for search requests, with the ability to pass search parameters during the actual request. This not only makes the request body more concise but also helps avoid errors that may occur when concatenating query structures on the client side.

When search optimization is necessary, modifications to search scripts can be made directly on the Elasticsearch server without the need to redeploy the client. This significantly enhances the efficiency of search optimization.

However, the [default scripting languages](https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-scripting.html#scripting-available-languages) (`mustache`/`painless`/`expression`) supported by *Elasticsearch* have relatively limited syntax logic, lacking support for any conditional statements. This imposes significant constraints on the use of *Search Template*.

By introducing *Velocity* into *Elasticsearch*, support for any conditional statements is enabled, making the usage of *Search Template* more flexible. This provides users with greater customization capabilities for powerful and flexible searches.

## Version Convention

> version = `elasticsearchVersion`-`pluginVersion`

## Install

> optional 1 - use elasticsearch-plugin to install
>
> Tip: Replace [version] in the link with your Elasticsearch version

```shell script
./bin/elasticsearch-plugin install https://github.com/Ahoo-Wang/elasticsearch-script-velocity/releases/download/v[version]/elasticsearch-script-velocity-[version].zip
```
> optional 2 - download pre-build package from here: [Releases](https://github.com/Ahoo-Wang/elasticsearch-script-velocity/releases)

1. create plugin folder `cd your-es-root/plugins/ && mkdir elasticsearch-script-velocity`
2. unzip plugin to folder `your-es-root/plugins/elasticsearch-script-velocity`

## Use

### Store script

[create-stored-script-api](https://www.elastic.co/guide/en/elasticsearch/reference/current/create-stored-script-api.html)

```http request
POST _scripts/templateid
{
"script": {
"lang": "velocity",
"source": {
"query": {
"match": {
"title": "$query_string"
}
}
}
}
}
```
### Get Stored Script

[get-stored-script-api](https://www.elastic.co/guide/en/elasticsearch/reference/current/get-stored-script-api.html)

```http request
GET _scripts/templateid
```

> Response:

```json
{
"_id" : "templateid",
"found" : true,
"script" : {
"lang" : "velocity",
"source" : """{"query":{"match":{"title":"$query_string"}}}""",
"options" : {
"content_type" : "application/json; charset=UTF-8"
}
}
}
```

### Delete a stored script

[delete-stored-script-api](https://www.elastic.co/guide/en/elasticsearch/reference/current/delete-stored-script-api.html)

```http request
DELETE _scripts/templateid
```

### Search template

[search-template](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-template.html)

```http request
GET _search/template
{
"id": "templateid",
"params": {
"query_string": "search for these words"
}
}
```

### Validating a search template

```http request
GET _render/template/templateid
{
"params": {
"query_string": "search for these words"
}
}
```

> Response:

```json
{
"template_output" : {
"query" : {
"match" : {
"title" : "search for these words"
}
}
}
}
```

## Benchmark

```
Benchmark Mode Cnt Score Error Units
VelocityScriptEngineBenchmark.executeSearchTemplate thrpt 1020003.447 ops/s
```