Search code examples
elasticsearchelasticsearch-painless

ES7 Update new completion with context field based on indexed values for every document using Update by query endpoint


I am trying to update a value from an ES index that is mapped as a completion field with a context for filtering.

I want to insert values based on the values that I already have in a document.

I tried this, which doesn't work as as expected because it just takes the string, not the value, which was expected, but I tried it anyhow:

{
    "script": {
        "inline": "ctx._source.suggest_test = params.newProp",
        "params": {
            "newProp": {
                "input": "ctx._source.search_text",
                "contexts": {
                    "type": ["ctx._source.type"]
                }
            }
        }
    }
}

and this, multiple versions of it, all of them returning errors

{
    "script": "ctx._source.search_text_completion = {\"input\":[ctx._source.search_text],\"contexts\":{\"type\": [ctx._source.type]}}"
}

Is there a way to do this or do I have to reindex everything?


Solution

  • You have to do it like this (with square brackets):

    "script": "ctx._source.search_text_completion = ['input':[ctx._source.search_text],'contexts':['type': [ctx._source.type]]]"