Kumar Kisalay
Kumar Kisalay

Reputation: 73

Painless script to add new fields into _source object when querying into elasticsearch v6.0.1

I have an index with the field mapping with one property (id: integer). When I am querying into that index, I am able to get the correct response. Now, I want to add one extra fields into _source object at the query time using painless scripting. The elasticsearch version is 6.0.1.

I have already tried adding script as a field in the query block. But it throws an error:

{
  "error": {
    "root_cause": [
      {
        "type": "parsing_exception",
        "reason": "[term] malformed query, expected [END_OBJECT] but found [FIELD_NAME]",
        "line": 7,
        "col": 7
      }
    ],
    "type": "parsing_exception",
    "reason": "[term] malformed query, expected [END_OBJECT] but found [FIELD_NAME]",
    "line": 7,
    "col": 7
  },
  "status": 400
}
GET 20190719_candidate/candidate/_search
{
    "min_score": 0.001,
    "query": {
      "term": {
        "id": 1234
      }, 
      "script": {
        "script": {
          "inline": "doc['field_1'] = 'field_1_value'"
        }
      }
    }, 
    "from": 0,
    "size": 20
}

The expected result for _source object is:

{
  "id": "1234567",
  "field_1": "field_1_value"
}

Upvotes: 2

Views: 2503

Answers (2)

bugsb
bugsb

Reputation: 129

"root_cause": [ { "type": "parsing_exception", "reason": "[term] malformed query, expected [END_OBJECT] but found [FIELD_NAME]", "line": 7, "col": 7 } ],

the error says that you have a malformed query, you have missed a closing bracket in line 7 to close the "query" attribute.

you query should be like:

GET 20190719_candidate/candidate/_search
{
    "min_score": 0.001,
    "query": {
      "term": {
        "id": 1234
      }}, 
      "script": {
          "lang": "painless",
          "inline": "doc['field_1'] = 'field_1_value'"
      }, 
    "from": 0,
    "size": 20
}

Upvotes: 0

Vitorlui
Vitorlui

Reputation: 770

You are missing the structure:

GET 20190719_candidate/candidate/_search
{
    "min_score": 0.001,
    "query": {
      "term": {
        "id": 1234
      }, 
      "script_fields": {
           "test1":{
                 "script": {
                      "lang": "painless",                       
                      "source": "'field_1_value'"
                 }
            }
      }
    }, 
    "from": 0,
    "size": 20
}

Take a look in this example:

GET /_search
{
    "query" : {
        "match_all": {}
    },
    "script_fields" : {
        "test1" : {
            "script" : {
                "lang": "painless",
                "source": "doc['price'].value * 2"
            }
        },
        "test2" : {
            "script" : {
                "lang": "painless",
                "source": "doc['price'].value * params.factor",
                "params" : {
                    "factor"  : 2.0
                }
            }
        }
    }
}

source: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-body.html#request-body-search-script-fields

Upvotes: 1

Related Questions