2011-03-07 13:09:07 +02:00
|
|
|
/*
|
2011-12-06 02:42:25 +02:00
|
|
|
* Licensed to ElasticSearch and Shay Banon under one
|
2011-03-07 13:09:07 +02:00
|
|
|
* or more contributor license agreements. See the NOTICE file
|
|
|
|
* distributed with this work for additional information
|
2011-12-06 02:42:25 +02:00
|
|
|
* regarding copyright ownership. ElasticSearch licenses this
|
2011-03-07 13:09:07 +02:00
|
|
|
* 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;
|
|
|
|
|
2014-01-02 13:54:40 +01:00
|
|
|
import org.elasticsearch.search.lookup.IndexLookup;
|
make term statistics accessible in scripts
term statistics can be accessed via the _shard variable.
Below is a minimal example. See documentation on details.
```
DELETE paytest
PUT paytest
{
"mappings": {
"test": {
"_all": {
"auto_boost": true,
"enabled": true
},
"properties": {
"text": {
"index_analyzer": "fulltext_analyzer",
"store": "yes",
"type": "string"
}
}
}
},
"settings": {
"analysis": {
"analyzer": {
"fulltext_analyzer": {
"filter": [
"my_delimited_payload_filter"
],
"tokenizer": "whitespace",
"type": "custom"
}
},
"filter": {
"my_delimited_payload_filter": {
"delimiter": "+",
"encoding": "float",
"type": "delimited_payload_filter"
}
}
},
"index": {
"number_of_replicas": 0,
"number_of_shards": 1
}
}
}
POST paytest/test/1
{
"text": "the+1 quick+2 brown+3 fox+4 is quick+10"
}
POST paytest/test/2
{
"text": "the+1 quick+2 red+3 fox+4"
}
POST paytest/_refresh
POST paytest/_search
{
"script_fields": {
"ttf": {
"script": "_shard[\"text\"][\"quick\"].ttf()"
}
}
}
POST paytest/_search
{
"script_fields": {
"freq": {
"script": "_shard[\"text\"][\"quick\"].freq()"
}
}
}
POST paytest/test/2/_termvector
POST paytest/_search
{
"script_fields": {
"payloads": {
"script": "term = _shard[\"text\"].get(\"red\",_PAYLOADS);payloads = []; for(pos : term){payloads.add(pos.payloadAsFloat(-1));} return payloads;"
}
}
}
POST paytest/_search
{
"script_fields": {
"tv": {
"script": "_shard[\"text\"][\"quick\"].freq()"
}
},
"query": {
"function_score": {
"functions": [
{
"script_score": {
"script": "_shard[\"text\"][\"quick\"].freq()"
}
}
]
}
}
}
```
closes #3772
2014-01-02 11:17:33 +01:00
|
|
|
|
2012-10-26 17:11:55 +02:00
|
|
|
import org.apache.lucene.index.AtomicReaderContext;
|
2011-03-07 13:09:07 +02:00
|
|
|
import org.apache.lucene.search.Scorer;
|
2013-08-14 18:21:09 +02:00
|
|
|
import org.elasticsearch.index.fielddata.ScriptDocValues;
|
2011-03-07 13:09:07 +02:00
|
|
|
import org.elasticsearch.search.lookup.DocLookup;
|
|
|
|
import org.elasticsearch.search.lookup.FieldsLookup;
|
|
|
|
import org.elasticsearch.search.lookup.SearchLookup;
|
|
|
|
import org.elasticsearch.search.lookup.SourceLookup;
|
|
|
|
|
2011-06-24 09:39:37 +03:00
|
|
|
import java.util.Map;
|
|
|
|
|
2011-03-07 13:09:07 +02:00
|
|
|
/**
|
|
|
|
* A base class for any script type that is used during the search process (custom score, facets, and so on).
|
2011-12-06 02:42:25 +02:00
|
|
|
* <p/>
|
2011-03-07 13:09:07 +02:00
|
|
|
* <p>If the script returns a specific numeric type, consider overriding the type specific base classes
|
|
|
|
* such as {@link AbstractDoubleSearchScript}, {@link AbstractFloatSearchScript} and {@link AbstractLongSearchScript}
|
|
|
|
* for better performance.
|
2011-12-06 02:42:25 +02:00
|
|
|
* <p/>
|
2011-03-07 13:09:07 +02:00
|
|
|
* <p>The use is required to implement the {@link #run()} method.
|
|
|
|
*/
|
|
|
|
public abstract class AbstractSearchScript extends AbstractExecutableScript implements SearchScript {
|
|
|
|
|
|
|
|
private SearchLookup lookup;
|
|
|
|
|
|
|
|
private float score = Float.NaN;
|
|
|
|
|
2011-05-20 18:15:56 +03:00
|
|
|
/**
|
|
|
|
* Returns the current score and only applicable when used as a scoring script in a custom score query!.
|
|
|
|
* For other cases, use {@link #doc()} and get the score from it.
|
|
|
|
*/
|
2011-03-07 13:09:07 +02:00
|
|
|
protected final float score() {
|
|
|
|
return score;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the doc lookup allowing to access field data (cached) values as well as the current document score
|
|
|
|
* (where applicable).
|
|
|
|
*/
|
|
|
|
protected final DocLookup doc() {
|
|
|
|
return lookup.doc();
|
|
|
|
}
|
|
|
|
|
2013-08-14 18:21:09 +02:00
|
|
|
/**
|
|
|
|
* Returns field data strings access for the provided field.
|
|
|
|
*/
|
|
|
|
protected ScriptDocValues.Strings docFieldStrings(String field) {
|
|
|
|
return (ScriptDocValues.Strings) doc().get(field);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns field data double (floating point) access for the provided field.
|
|
|
|
*/
|
|
|
|
protected ScriptDocValues.Doubles docFieldDoubles(String field) {
|
|
|
|
return (ScriptDocValues.Doubles) doc().get(field);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns field data long (integers) access for the provided field.
|
|
|
|
*/
|
|
|
|
protected ScriptDocValues.Longs docFieldLongs(String field) {
|
|
|
|
return (ScriptDocValues.Longs) doc().get(field);
|
|
|
|
}
|
|
|
|
|
2011-03-07 13:09:07 +02:00
|
|
|
/**
|
|
|
|
* Allows to access the actual source (loaded and parsed).
|
|
|
|
*/
|
|
|
|
protected final SourceLookup source() {
|
|
|
|
return lookup.source();
|
|
|
|
}
|
make term statistics accessible in scripts
term statistics can be accessed via the _shard variable.
Below is a minimal example. See documentation on details.
```
DELETE paytest
PUT paytest
{
"mappings": {
"test": {
"_all": {
"auto_boost": true,
"enabled": true
},
"properties": {
"text": {
"index_analyzer": "fulltext_analyzer",
"store": "yes",
"type": "string"
}
}
}
},
"settings": {
"analysis": {
"analyzer": {
"fulltext_analyzer": {
"filter": [
"my_delimited_payload_filter"
],
"tokenizer": "whitespace",
"type": "custom"
}
},
"filter": {
"my_delimited_payload_filter": {
"delimiter": "+",
"encoding": "float",
"type": "delimited_payload_filter"
}
}
},
"index": {
"number_of_replicas": 0,
"number_of_shards": 1
}
}
}
POST paytest/test/1
{
"text": "the+1 quick+2 brown+3 fox+4 is quick+10"
}
POST paytest/test/2
{
"text": "the+1 quick+2 red+3 fox+4"
}
POST paytest/_refresh
POST paytest/_search
{
"script_fields": {
"ttf": {
"script": "_shard[\"text\"][\"quick\"].ttf()"
}
}
}
POST paytest/_search
{
"script_fields": {
"freq": {
"script": "_shard[\"text\"][\"quick\"].freq()"
}
}
}
POST paytest/test/2/_termvector
POST paytest/_search
{
"script_fields": {
"payloads": {
"script": "term = _shard[\"text\"].get(\"red\",_PAYLOADS);payloads = []; for(pos : term){payloads.add(pos.payloadAsFloat(-1));} return payloads;"
}
}
}
POST paytest/_search
{
"script_fields": {
"tv": {
"script": "_shard[\"text\"][\"quick\"].freq()"
}
},
"query": {
"function_score": {
"functions": [
{
"script_score": {
"script": "_shard[\"text\"][\"quick\"].freq()"
}
}
]
}
}
}
```
closes #3772
2014-01-02 11:17:33 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Allows to access statistics on terms and fields.
|
|
|
|
*/
|
2014-01-02 13:54:40 +01:00
|
|
|
protected final IndexLookup indexLookup() {
|
|
|
|
return lookup.indexLookup();
|
make term statistics accessible in scripts
term statistics can be accessed via the _shard variable.
Below is a minimal example. See documentation on details.
```
DELETE paytest
PUT paytest
{
"mappings": {
"test": {
"_all": {
"auto_boost": true,
"enabled": true
},
"properties": {
"text": {
"index_analyzer": "fulltext_analyzer",
"store": "yes",
"type": "string"
}
}
}
},
"settings": {
"analysis": {
"analyzer": {
"fulltext_analyzer": {
"filter": [
"my_delimited_payload_filter"
],
"tokenizer": "whitespace",
"type": "custom"
}
},
"filter": {
"my_delimited_payload_filter": {
"delimiter": "+",
"encoding": "float",
"type": "delimited_payload_filter"
}
}
},
"index": {
"number_of_replicas": 0,
"number_of_shards": 1
}
}
}
POST paytest/test/1
{
"text": "the+1 quick+2 brown+3 fox+4 is quick+10"
}
POST paytest/test/2
{
"text": "the+1 quick+2 red+3 fox+4"
}
POST paytest/_refresh
POST paytest/_search
{
"script_fields": {
"ttf": {
"script": "_shard[\"text\"][\"quick\"].ttf()"
}
}
}
POST paytest/_search
{
"script_fields": {
"freq": {
"script": "_shard[\"text\"][\"quick\"].freq()"
}
}
}
POST paytest/test/2/_termvector
POST paytest/_search
{
"script_fields": {
"payloads": {
"script": "term = _shard[\"text\"].get(\"red\",_PAYLOADS);payloads = []; for(pos : term){payloads.add(pos.payloadAsFloat(-1));} return payloads;"
}
}
}
POST paytest/_search
{
"script_fields": {
"tv": {
"script": "_shard[\"text\"][\"quick\"].freq()"
}
},
"query": {
"function_score": {
"functions": [
{
"script_score": {
"script": "_shard[\"text\"][\"quick\"].freq()"
}
}
]
}
}
}
```
closes #3772
2014-01-02 11:17:33 +01:00
|
|
|
}
|
2011-03-07 13:09:07 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Allows to access the *stored* fields.
|
|
|
|
*/
|
|
|
|
protected final FieldsLookup fields() {
|
|
|
|
return lookup.fields();
|
|
|
|
}
|
|
|
|
|
|
|
|
void setLookup(SearchLookup lookup) {
|
|
|
|
this.lookup = lookup;
|
|
|
|
}
|
|
|
|
|
2011-12-06 02:42:25 +02:00
|
|
|
@Override
|
|
|
|
public void setScorer(Scorer scorer) {
|
2011-03-07 13:09:07 +02:00
|
|
|
lookup.setScorer(scorer);
|
|
|
|
}
|
|
|
|
|
2011-12-06 02:42:25 +02:00
|
|
|
@Override
|
2012-10-26 17:11:55 +02:00
|
|
|
public void setNextReader(AtomicReaderContext context) {
|
|
|
|
lookup.setNextReader(context);
|
2011-03-07 13:09:07 +02:00
|
|
|
}
|
|
|
|
|
2011-12-06 02:42:25 +02:00
|
|
|
@Override
|
|
|
|
public void setNextDocId(int doc) {
|
2011-03-07 13:09:07 +02:00
|
|
|
lookup.setNextDocId(doc);
|
|
|
|
}
|
|
|
|
|
2011-12-06 02:42:25 +02:00
|
|
|
@Override
|
|
|
|
public void setNextSource(Map<String, Object> source) {
|
2011-06-24 09:39:37 +03:00
|
|
|
lookup.source().setNextSource(source);
|
|
|
|
}
|
|
|
|
|
2011-12-06 02:42:25 +02:00
|
|
|
@Override
|
|
|
|
public void setNextScore(float score) {
|
2011-03-07 13:09:07 +02:00
|
|
|
this.score = score;
|
|
|
|
}
|
|
|
|
|
2011-12-06 02:42:25 +02:00
|
|
|
@Override
|
|
|
|
public float runAsFloat() {
|
2011-03-07 13:09:07 +02:00
|
|
|
return ((Number) run()).floatValue();
|
|
|
|
}
|
|
|
|
|
2011-12-06 02:42:25 +02:00
|
|
|
@Override
|
|
|
|
public long runAsLong() {
|
2011-03-07 13:09:07 +02:00
|
|
|
return ((Number) run()).longValue();
|
|
|
|
}
|
|
|
|
|
2011-12-06 02:42:25 +02:00
|
|
|
@Override
|
|
|
|
public double runAsDouble() {
|
2011-03-07 13:09:07 +02:00
|
|
|
return ((Number) run()).doubleValue();
|
|
|
|
}
|
|
|
|
}
|