-
Notifications
You must be signed in to change notification settings - Fork 24.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This allows users to filter out tokens from a TokenStream using painless scripts, instead of having to write specialised Java code and packaging it up into a plugin. The commit also refactors the AnalysisPredicateScript.Token class so that it wraps and makes read-only an AttributeSource.
- Loading branch information
1 parent
a55fa4f
commit f598297
Showing
8 changed files
with
341 additions
and
44 deletions.
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.