Search code examples
elasticsearchelasticsearch-6

script_score query does not support [source]


I'm using the official Docker image for Elasticsearch OSS (docker.elastic.co/elasticsearch/elasticsearch-oss:6.2.4) and can't seem to get script_score working at all. It seems like scripting isn't enabled.

For example, this:

POST http://localhost:9200/address/address/_search

{
    "query": {
        "function_score": {
            "query": {
                "match": {
                    "fullAddress": {
                        "query": "13 fake",
                        "operator": "and"
                    }
                }
            },
            "script_score": {
                "lang": "expression",
                "source": "doc['flatNumber'].length"
            }
        }
    }
}

gives me this:

{
    "error": {
        "root_cause": [
            {
                "type": "parsing_exception",
                "reason": "script_score query does not support [source]",
                "line": 13,
                "col": 15
            }
        ],
        "type": "parsing_exception",
        "reason": "script_score query does not support [source]",
        "line": 13,
        "col": 15
    },
    "status": 400
}

I tried enabling it:

PUT http://localhost:9200/_cluster/settings

{
    "persistent": {
        "script.engine.groovy.inline.aggs": "on"
    }
}

but to no avail:

{
    "error": {
        "root_cause": [
            {
                "type": "illegal_argument_exception",
                "reason": "persistent setting [script.engine.groovy.inline.aggs], not recognized"
            }
        ],
        "type": "illegal_argument_exception",
        "reason": "persistent setting [script.engine.groovy.inline.aggs], not recognized"
    },
    "status": 400
}

How do I get script_score working?


Solution

  • You're simply missing a script section in your script_score. Modify it like this and it will work:

        "script_score": {
            "script": {
                "lang": "expression",
                "source": "doc['flatNumber'].length"
            }
        }