2

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"
}

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

"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
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.