Painless null pointer exception

Here is My query , I am using elastic search 6.2 and my java version is 1.9

GET /users_small/user/_search
{
  "_source": [
    "id",
    "username",
    "full_name",
    "picture",
    "message_privacy"
  ],
  "from": 0,
  "size": 1000,
  "sort": [
    {
      "username.raw": {
        "order": "asc"
      }
    }
  ],
  "query": {
    "bool": {
      "must": [
         {
          "terms": {
            "id": [
              5
            ]
          }
        },
        {
          "terms": {
            "message_privacy": [
              0,
              1
            ]
          }
        },
         {
          "terms": {
            "id": [
              5
            ]
          }
        },
        {
          "script": {
            "script": {
              "source": "List followings = new ArrayList(params._source['followings']); List followers = new ArrayList(params._source['followers']);  if(followers.contains(params.keyword)) { return true } else if(followings.contains(params.keyword)) { return true; } else { return false; }",
              "lang": "painless",
              "params": {
                "keyword": 14
              }
            }
          }
        }
      ],
      "must_not": [
        {
          "terms": {
            "id": [
              "7195",
              "1154",
              "3464",
              "8562",
              "2874",
              "6522",
              "8721",
              "9196",
              "3318",
              "13986",
              "8693",
              "7201",
              "9546",
              "7240",
              "18042",
              "6013",
              "6999",
              "19419",
              "3984",
              "17688",
              "9244",
              "28335",
              "850",
              "28777",
              "8625",
              8625
            ]
          }
        }
      ],
      "filter": []
    }
  }
}

but i am getting following error

{
  "took": 11,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 4,
    "skipped": 0,
    "failed": 1,
    "failures": [
      {
        "shard": 1,
        "index": "users_small",
        "node": "mK14IuVHTDKgHLnYYoXGqQ",
        "reason": {
          "type": "script_exception",
          "reason": "runtime error",
          "script_stack": [
            "followings = new ArrayList(params._source['followings']); List ",
            "                                 ^---- HERE"
          ],
          "script": "List followings = new ArrayList(params._source['followings']); List followers = new ArrayList(params._source['followers']);  if(followers.contains(params.keyword)) { return true } else if(followings.contains(params.keyword)) { return true; } else { return false; }",
          "lang": "painless",
          "caused_by": {
            "type": "null_pointer_exception",
            "reason": null
          }
        }
      }
    ]
  },
  "hits": {
    "total": 0,
    "max_score": null,
    "hits": []
  }
}

Are you sure that all your documents contain a non-null followings array?

Unfortunately, _source is not accessible in script queries, you would have to use doc values for that. One thing I don't quite understand from your example is why use script here. You would get much better performance if you replace it with 2 term queries.

Yes atleast every document has empty node like "following" : []

doc value is also not working , it gives us to allow metadata = true which can cause memory issues

You should use a not-analyzed version of this field for that. You might have followings.keywords for example if you used default settings and is using a recent version of elasticsearch.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.