-
Notifications
You must be signed in to change notification settings - Fork 24.7k
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 script_filter tokenfilter #33431
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
docs/reference/analysis/tokenfilters/predicate-tokenfilter.asciidoc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
[[analysis-predicatefilter-tokenfilter]] | ||
=== Predicate Token Filter Script | ||
|
||
The predicate_token_filter token filter takes a predicate script, and removes tokens that do | ||
not match the predicate. | ||
|
||
[float] | ||
=== Options | ||
[horizontal] | ||
script:: a predicate script that determines whether or not the current token will | ||
be emitted. Note that only inline scripts are supported. | ||
|
||
[float] | ||
=== Settings example | ||
|
||
You can set it up like: | ||
|
||
[source,js] | ||
-------------------------------------------------- | ||
PUT /condition_example | ||
{ | ||
"settings" : { | ||
"analysis" : { | ||
"analyzer" : { | ||
"my_analyzer" : { | ||
"tokenizer" : "standard", | ||
"filter" : [ "my_script_filter" ] | ||
} | ||
}, | ||
"filter" : { | ||
"my_script_filter" : { | ||
"type" : "predicate_token_filter", | ||
"script" : { | ||
"source" : "token.getTerm().length() > 5" <1> | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
-------------------------------------------------- | ||
// CONSOLE | ||
|
||
<1> This will emit tokens that are more than 5 characters long | ||
|
||
And test it like: | ||
|
||
[source,js] | ||
-------------------------------------------------- | ||
POST /condition_example/_analyze | ||
{ | ||
"analyzer" : "my_analyzer", | ||
"text" : "What Flapdoodle" | ||
} | ||
-------------------------------------------------- | ||
// CONSOLE | ||
// TEST[continued] | ||
|
||
And it'd respond: | ||
|
||
[source,js] | ||
-------------------------------------------------- | ||
{ | ||
"tokens": [ | ||
{ | ||
"token": "Flapdoodle", <1> | ||
"start_offset": 5, | ||
"end_offset": 15, | ||
"type": "<ALPHANUM>", | ||
"position": 1 <2> | ||
} | ||
] | ||
} | ||
-------------------------------------------------- | ||
// TESTRESPONSE | ||
|
||
<1> The token 'What' has been removed from the tokenstream because it does not | ||
match the predicate. | ||
<2> The position and offset values are unaffected by the removal of earlier tokens |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
...on/src/main/java/org/elasticsearch/analysis/common/PredicateTokenFilterScriptFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package org.elasticsearch.analysis.common; | ||
|
||
import org.apache.lucene.analysis.FilteringTokenFilter; | ||
import org.apache.lucene.analysis.TokenStream; | ||
import org.elasticsearch.common.settings.Settings; | ||
import org.elasticsearch.index.IndexSettings; | ||
import org.elasticsearch.index.analysis.AbstractTokenFilterFactory; | ||
import org.elasticsearch.script.Script; | ||
import org.elasticsearch.script.ScriptService; | ||
import org.elasticsearch.script.ScriptType; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* A factory for creating FilteringTokenFilters that determine whether or not to | ||
* accept their underlying token by consulting a script | ||
*/ | ||
public class PredicateTokenFilterScriptFactory extends AbstractTokenFilterFactory { | ||
|
||
private final AnalysisPredicateScript.Factory factory; | ||
|
||
public PredicateTokenFilterScriptFactory(IndexSettings indexSettings, String name, Settings settings, ScriptService scriptService) { | ||
super(indexSettings, name, settings); | ||
Settings scriptSettings = settings.getAsSettings("script"); | ||
Script script = Script.parse(scriptSettings); | ||
if (script.getType() != ScriptType.INLINE) { | ||
throw new IllegalArgumentException("Cannot use stored scripts in tokenfilter [" + name + "]"); | ||
} | ||
this.factory = scriptService.compile(script, AnalysisPredicateScript.CONTEXT); | ||
} | ||
|
||
@Override | ||
public TokenStream create(TokenStream tokenStream) { | ||
return new ScriptFilteringTokenFilter(tokenStream, factory.newInstance()); | ||
} | ||
|
||
private static class ScriptFilteringTokenFilter extends FilteringTokenFilter { | ||
|
||
final AnalysisPredicateScript script; | ||
final AnalysisPredicateScript.Token token; | ||
|
||
ScriptFilteringTokenFilter(TokenStream in, AnalysisPredicateScript script) { | ||
super(in); | ||
this.script = script; | ||
this.token = new AnalysisPredicateScript.Token(this); | ||
} | ||
|
||
@Override | ||
protected boolean accept() throws IOException { | ||
token.updatePosition(); | ||
return script.execute(token); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍