3

I've tried to retrieve array values from my data by using script_fields, but I received inconsistent results.

My mapping:

curl -XPUT localhost:9200/test/user/_mapping -d '
{
  "user": {
    "properties": {
      "id": {
        "type": "string",
        "index": "not_analyzed"
      },
      "other_ids": {
        "type": "string",
        "index": "not_analyzed"
      }
    }
  }
}
'

Some test data:

curl -XPUT localhost:9200/test/user/id1 -d '
{
  "id": "id1",
  "other_ids": [
    "id2",
    "id3"
  ]
}
'

curl -XPUT localhost:9200/test/user/id2 -d '
{
  "id": "id2",
  "other_ids": [
    "id1"
  ]
}
'

curl -XPUT localhost:9200/test/user/id3 -d '
{
  "id": "id3",
  "other_ids": [
    "id1",
    "id2"
  ]
}
'

The "simplified" query:

curl -XGET 'localhost:9200/test/user/_search?pretty=1'  -d '
{
  "fields": [
    "_source"
  ],
  "query": {
    "match_all": {}
  },
  "script_fields": {
    "other_id_script": {
      "lang": "groovy",
      "script": "doc[\"other_ids\"].values"
    }
  }
}
'

The relevant parts from the answer:

  "hits" : {
    "total" : 3,
    "max_score" : 1.0,
    "hits" : [ {
      "_source":{"id":"id1","other_ids":["id2","id3"]},
      "fields" : {
        "other_id_script" : [ [ "id1", "id2" ] ]
      }
    }, {
      "_source":{"id":"id2","other_ids":["id1"]},
      "fields" : {
        "other_id_script" : [ [ "id1", "id2" ] ]
      }
    }, {
      "_source":{"id":"id3","other_ids":["id1","id2"]},
      "fields" : {
        "other_id_script" : [ [ "id1", "id2" ] ]
      }
    } ]
  }

As we can see the values of other_id_script are different from the current doc's other_ids values in the first two cases.

Is it an error, or am I missed something?

(The whole script can be found here: https://gist.github.com/baloghz/c27e39ad419a6f4684ab)

3
  • i'm unable to replicate it in elasticsearch version 1.3.0 , java 1.7.0_65 Commented Nov 10, 2014 at 19:56
  • Thanks for trying! I have elasticsearch version 1.3.0 , java 1.7.0_67-b01. What kind of settings can cause this kind of strange results (some kind of cache related maybe)? Commented Nov 11, 2014 at 9:58
  • I'm noticing the same problem. Were you able to figure out how to fix this? Commented May 2, 2015 at 0:39

1 Answer 1

2

It looks like this problem was addressed in Elasticsearch issue #8576, was fixed by commit d22645c, and is live in versions 1.3.6, 1.4.1, and 1.5.0.

The author of the issue also gave a workaround:

"script": "doc[\"other_ids\"].values.take(100)"
Sign up to request clarification or add additional context in comments.

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.