elasticsearch/elasticsearch — pull request #4161 by brwe (2013-11-13 13:27:29+00:00)
Title: make term statistics and term vectors accessible in scripts

Below is a minimal example which you can run to see how it works in principle. Documentation lists all options.

At this point I would first like to make sure that the general concept is OK. After that I will add complete javadoc etc.

closes #3772

Minimal example:

```
curl -XDELETE "http://localhost:9200/paytest"

curl -XPUT "http://localhost:9200/paytest " -d'
{
"mappings": {
"test": {
"_all": {
"auto_boost": true,
"enabled": true
},
"properties": {
"text": {
"index_analyzer": "fulltext_analyzer",
"store": "yes",
"type": "string",
"term_vector":"with_positions_offsets_payloads"
}
}
}
},
"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
}
}
}'

curl -XPOST "http://localhost:9200/paytest/test/1" -d'
{
"text": "the+1 quick+2 brown+3 fox+4 is quick+10"
}'

curl -XPOST "http://localhost:9200/paytest/test/2" -d'
{
"text": "the+1 quick+2 red+3 fox+4"
}'

curl -XPOST "http://localhost:9200/paytest/_refresh"

#get the total term frequency for "quick"
curl -XPOST "http://localhost:9200/paytest/_search " -d'
{
"script_fields": {
"tv": {
"script": "_tv[\"text\"][\"quick\"].totalTermFrequency()"
}
}
}'

# get the term frequencies
curl -XPOST "http://localhost:9200/paytest/_search " -d'
{
"script_fields": {
"tv": {
"script": "_tv[\"text\"][\"quick\"].freq()"
}
}
}'

# get the payloads which are floats in this case
curl -XPOST "http://localhost:9200/paytest/_search " -d'
{
"script_fields": {
"tv": {
"script": "_tv[\"text\"][\"red\"].payloadsAsFloat(-1)"
}
}
}'

#compute very simple score: just use the term frequency
curl -XPOST "http://localhost:9200/paytest/_search " -d'
{
"script_fields": {
"tv": {
"script": "_tv[\"text\"][\"quick\"].freq()"
}
},
"query": {
"function_score": {
"functions": [
{
"script_score": {
"script": "_tv[\"text\"][\"quick\"].freq()"
}
}
]
}
}
}'

```

and so on. see documentation for all options.