-
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.
Browse files
Browse the repository at this point in the history
* SCRIPTING: Move terms_set Context to its Own Class * Extracted TermsSetQueryScript * Kept mechanics close to what they were with SearchScript
- Loading branch information
1 parent
cffe1cd
commit 03add58
Showing
6 changed files
with
240 additions
and
14 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
105 changes: 105 additions & 0 deletions
105
server/src/main/java/org/elasticsearch/script/ParameterMap.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,105 @@ | ||
/* | ||
* 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.script; | ||
|
||
import java.util.Collection; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.elasticsearch.common.logging.DeprecationLogger; | ||
|
||
public final class ParameterMap implements Map<String, Object> { | ||
|
||
private static final DeprecationLogger DEPRECATION_LOGGER = | ||
new DeprecationLogger(LogManager.getLogger(ParameterMap.class)); | ||
|
||
private final Map<String, Object> params; | ||
|
||
private final Map<String, String> deprecations; | ||
|
||
ParameterMap(Map<String, Object> params, Map<String, String> deprecations) { | ||
this.params = params; | ||
this.deprecations = deprecations; | ||
} | ||
|
||
@Override | ||
public int size() { | ||
return params.size(); | ||
} | ||
|
||
@Override | ||
public boolean isEmpty() { | ||
return params.isEmpty(); | ||
} | ||
|
||
@Override | ||
public boolean containsKey(final Object key) { | ||
return params.containsKey(key); | ||
} | ||
|
||
@Override | ||
public boolean containsValue(final Object value) { | ||
return params.containsValue(value); | ||
} | ||
|
||
@Override | ||
public Object get(final Object key) { | ||
String deprecationMessage = deprecations.get(key); | ||
if (deprecationMessage != null) { | ||
DEPRECATION_LOGGER.deprecated(deprecationMessage); | ||
} | ||
return params.get(key); | ||
} | ||
|
||
@Override | ||
public Object put(final String key, final Object value) { | ||
return params.put(key, value); | ||
} | ||
|
||
@Override | ||
public Object remove(final Object key) { | ||
return params.remove(key); | ||
} | ||
|
||
@Override | ||
public void putAll(final Map<? extends String, ?> m) { | ||
params.putAll(m); | ||
} | ||
|
||
@Override | ||
public void clear() { | ||
params.clear(); | ||
} | ||
|
||
@Override | ||
public Set<String> keySet() { | ||
return params.keySet(); | ||
} | ||
|
||
@Override | ||
public Collection<Object> values() { | ||
return params.values(); | ||
} | ||
|
||
@Override | ||
public Set<Entry<String, Object>> entrySet() { | ||
return params.entrySet(); | ||
} | ||
} |
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
112 changes: 112 additions & 0 deletions
112
server/src/main/java/org/elasticsearch/script/TermsSetQueryScript.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,112 @@ | ||
/* | ||
* 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.script; | ||
|
||
import java.io.IOException; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import org.apache.lucene.index.LeafReaderContext; | ||
import org.elasticsearch.index.fielddata.ScriptDocValues; | ||
import org.elasticsearch.search.lookup.LeafSearchLookup; | ||
import org.elasticsearch.search.lookup.SearchLookup; | ||
|
||
public abstract class TermsSetQueryScript { | ||
|
||
public static final String[] PARAMETERS = {}; | ||
|
||
public static final ScriptContext<Factory> CONTEXT = new ScriptContext<>("terms_set", Factory.class); | ||
|
||
private static final Map<String, String> DEPRECATIONS; | ||
|
||
static { | ||
Map<String, String> deprecations = new HashMap<>(); | ||
deprecations.put( | ||
"doc", | ||
"Accessing variable [doc] via [params.doc] from within a terms-set-query-script " + | ||
"is deprecated in favor of directly accessing [doc]." | ||
); | ||
deprecations.put( | ||
"_doc", | ||
"Accessing variable [doc] via [params._doc] from within a terms-set-query-script " + | ||
"is deprecated in favor of directly accessing [doc]." | ||
); | ||
DEPRECATIONS = Collections.unmodifiableMap(deprecations); | ||
} | ||
|
||
/** | ||
* The generic runtime parameters for the script. | ||
*/ | ||
private final Map<String, Object> params; | ||
|
||
/** | ||
* A leaf lookup for the bound segment this script will operate on. | ||
*/ | ||
private final LeafSearchLookup leafLookup; | ||
|
||
public TermsSetQueryScript(Map<String, Object> params, SearchLookup lookup, LeafReaderContext leafContext) { | ||
this.params = new ParameterMap(params, DEPRECATIONS); | ||
this.leafLookup = lookup.getLeafSearchLookup(leafContext); | ||
} | ||
|
||
/** | ||
* Return the parameters for this script. | ||
*/ | ||
public Map<String, Object> getParams() { | ||
this.params.putAll(leafLookup.asMap()); | ||
return params; | ||
} | ||
|
||
/** | ||
* The doc lookup for the Lucene segment this script was created for. | ||
*/ | ||
public Map<String, ScriptDocValues<?>> getDoc() { | ||
return leafLookup.doc(); | ||
} | ||
|
||
/** | ||
* Set the current document to run the script on next. | ||
*/ | ||
public void setDocument(int docid) { | ||
leafLookup.setDocument(docid); | ||
} | ||
|
||
/** | ||
* Return the result as a long. This is used by aggregation scripts over long fields. | ||
*/ | ||
public long runAsLong() { | ||
return execute().longValue(); | ||
} | ||
|
||
public abstract Number execute(); | ||
|
||
/** | ||
* A factory to construct {@link TermsSetQueryScript} instances. | ||
*/ | ||
public interface LeafFactory { | ||
TermsSetQueryScript newInstance(LeafReaderContext ctx) throws IOException; | ||
} | ||
|
||
/** | ||
* A factory to construct stateful {@link TermsSetQueryScript} factories for a specific index. | ||
*/ | ||
public interface Factory { | ||
LeafFactory newFactory(Map<String, Object> params, SearchLookup lookup); | ||
} | ||
} |
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