Skip to content
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

Add predicate token filter docs #8272 #8279

Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion _analyzers/token-filters/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ Normalization | `arabic_normalization`: [ArabicNormalizer](https://lucene.apache
`pattern_replace` | N/A | Matches a pattern in the provided regular expression and replaces matching substrings. Uses [Java regular expression syntax](https://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html).
`phonetic` | N/A | Uses a phonetic encoder to emit a metaphone token for each token in the token stream. Requires installing the `analysis-phonetic` plugin.
`porter_stem` | [PorterStemFilter](https://lucene.apache.org/core/9_10_0/analysis/common/org/apache/lucene/analysis/en/PorterStemFilter.html) | Uses the [Porter stemming algorithm](https://tartarus.org/martin/PorterStemmer/) to perform algorithmic stemming for the English language.
`predicate_token_filter` | N/A | Removes tokens that don’t match the specified predicate script. Supports inline Painless scripts only.
[`predicate_token_filter`]({{site.url}}{{site.baseurl}}/analyzers/token-filters/predicate-token-filter/) | N/A | Removes tokens that don’t match the specified predicate script. Supports inline Painless scripts only.
kolchfa-aws marked this conversation as resolved.
Show resolved Hide resolved
`remove_duplicates` | [RemoveDuplicatesTokenFilter](https://lucene.apache.org/core/9_10_0/analysis/common/org/apache/lucene/analysis/miscellaneous/RemoveDuplicatesTokenFilter.html) | Removes duplicate tokens that are in the same position.
`reverse` | [ReverseStringFilter](https://lucene.apache.org/core/9_10_0/analysis/common/org/apache/lucene/analysis/reverse/ReverseStringFilter.html) | Reverses the string corresponding to each token in the token stream. For example, the token `dog` becomes `god`.
`shingle` | [ShingleFilter](https://lucene.apache.org/core/9_10_0/analysis/common/org/apache/lucene/analysis/shingle/ShingleFilter.html) | Generates shingles of lengths between `min_shingle_size` and `max_shingle_size` for tokens in the token stream. Shingles are similar to n-grams but apply to words instead of letters. For example, two-word shingles added to the list of unigrams [`contribute`, `to`, `opensearch`] are [`contribute to`, `to opensearch`].
Expand Down
82 changes: 82 additions & 0 deletions _analyzers/token-filters/predicate-token-filter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
---
layout: default
title: Predicate token filter
parent: Token filters
nav_order: 340
---

# Predicate token filter

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line 10, second sentence: "in the analysis predicate context" => "in the context of the analysis predicate"?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Analysis predicate context is the proper term

The `predicate_token_filter` evaluates whether tokens should be kept or discarded, depending on the conditions defined in a custom script. The tokens are evaulated in the analysis predicate context. This filter supports only inline Painless scripts.

Check failure on line 10 in _analyzers/token-filters/predicate-token-filter.md

View workflow job for this annotation

GitHub Actions / style-job

[vale] reported by reviewdog 🐶 [OpenSearch.Spelling] Error: evaulated. If you are referencing a setting, variable, format, function, or repository, surround it with tic marks. Raw Output: {"message": "[OpenSearch.Spelling] Error: evaulated. If you are referencing a setting, variable, format, function, or repository, surround it with tic marks.", "location": {"path": "_analyzers/token-filters/predicate-token-filter.md", "range": {"start": {"line": 10, "column": 155}}}, "severity": "ERROR"}
kolchfa-aws marked this conversation as resolved.
Show resolved Hide resolved

## Parameters

The `predicate_token_filter` has one required parameter: `script`. This parameter provides a condition that is used to evaluate if the token should be kept.
kolchfa-aws marked this conversation as resolved.
Show resolved Hide resolved

## Example

The following example request creates a new index named `predicate_index` and configures an analyzer with a `predicate_token_filter`. The filter specifies to output tokens only if they are longer than 7 characters:
kolchfa-aws marked this conversation as resolved.
Show resolved Hide resolved

```json
PUT /predicate_index
{
"settings": {
"analysis": {
"filter": {
"my_predicate_filter": {
"type": "predicate_token_filter",
"script": {
"source": "token.term.length() > 7"
}
}
},
"analyzer": {
"predicate_analyzer": {
"tokenizer": "standard",
"filter": [
"lowercase",
"my_predicate_filter"
]
}
}
}
}
}
```
{% include copy-curl.html %}

## Generated tokens

Use the following request to examine the tokens generated using the analyzer:

```json
POST /predicate_index/_analyze
{
"text": "The OpenSearch community is growing rapidly",
"analyzer": "predicate_analyzer"
}
```
{% include copy-curl.html %}

The response contains the generated tokens:

```json
{
"tokens": [
{
"token": "opensearch",
"start_offset": 4,
"end_offset": 14,
"type": "<ALPHANUM>",
"position": 1
},
{
"token": "community",
"start_offset": 15,
"end_offset": 24,
"type": "<ALPHANUM>",
"position": 2
}
]
}
```
Loading