Elasticsearch 6.0 and joining queries

Hello,

I trying create relation parent-child as in docs (https://www.elastic.co/guide/en/elasticsearch/reference/current/parent-join.html) but when i querying:

{
  "query": {
    "parent_id": { 
      "type": "answer",
      "id": "1"
    }
  },
  "aggs": {
    "parents": {
      "terms": {
        "field": "my_join_field#question", 
        "size": 10
      }
    }
  },
  "script_fields": {
    "parent": {
      "script": {
         "source": "doc['my_join_field#question']" 
      }
    }
  }
}

I got error:

{
  "error" : {
    "root_cause" : [
      {
        "type" : "script_exception",
        "reason" : "compile error",
        "script_stack" : [
          "doc[my_join_field#question]",
          "                 ^---- HERE"
        ],
        "script" : "doc[my_join_field#question]",
        "lang" : "painless"
      }
    ],
    "type" : "search_phase_execution_exception",
    "reason" : "all shards failed",
    "phase" : "query",
    "grouped" : true,
    "failed_shards" : [
      {
        "shard" : 0,
        "index" : "my_index",
        "node" : "s4udXhJ0QJu6D5s7LhHnCA",
        "reason" : {
          "type" : "script_exception",
          "reason" : "compile error",
          "script_stack" : [
            "doc[my_join_field#question]",
            "                 ^---- HERE"
          ],
          "script" : "doc[my_join_field#question]",
          "lang" : "painless",
          "caused_by" : {
            "type" : "illegal_argument_exception",
            "reason" : "unexpected character [#].",
            "caused_by" : {
              "type" : "lexer_no_viable_alt_exception",
              "reason" : null
            }
          }
        }
      }
    ]
  },
  "status" : 500
}

What i'm doing wrong?

I am not able to reproduce the issue. I tried with the mapping and documents provided in the docs and it worked fine. Can you post the full list of actions that you did to encounter this exception ?

Elasticsearch info

curl -XGET 'http://localhost:9200/?pretty'
{
  "name" : "s4udXhJ",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "wOJP0hvERu2G6bUZAPqKcQ",
  "version" : {
    "number" : "6.0.0",
    "build_hash" : "8f0685b",
    "build_date" : "2017-11-10T18:41:22.859Z",
    "build_snapshot" : false,
    "lucene_version" : "7.0.1",
    "minimum_wire_compatibility_version" : "5.6.0",
    "minimum_index_compatibility_version" : "5.0.0"
  },
  "tagline" : "You Know, for Search"
}

Index

curl -XPUT 'http://localhost:9200/test_index' -H "Content-Type: application/json" -d '{
"settings" : {
  "analysis" : {
    "char_filter" : {
        "space_hashtags" : {
            "type" : "mapping",
            "mappings" : ["#=>|#"]
        }
    },
    "filter" : {
        "hashtag_as_alphanum" : {
            "type" : "standard",
            "type_table": ["# => ALPHANUM", "@ => ALPHANUM"]
        }
    },
    "analyzer" : {
        "hashtag" : {
            "type" : "custom",
            "tokenizer" : "whitespace",
            "filter" : ["lowercase", "hashtag_as_alphanum"]
        }
    }
}
},
"mappings" : {
"item" : {
    "properties" : {
        "text" : { "type" : "text", "analyzer":  "hashtag" },
			"type": {"type": "join", "relations": {"user": "post"}}
    }
}
}
}'

Items

curl -XPUT 'http://localhost:9200/test_index/item/1?pretty' -H 'Content-Type: application/json' -d '
{
  "text": "daniel",
  "type": "user"
}'

curl -XPUT 'http://localhost:9200/test_index/item/2?pretty' -H 'Content-Type: application/json' -d '
{
  "text": "marta",
  "type": "user" 
}'

curl -XPUT 'http://localhost:9200/test_index/item/3?routing=1&pretty' -H 'Content-Type: application/json' -d '
{
  "text": "post1",
  "type": {"name": "post", "parent": 1} 
}'

curl -XPUT 'http://localhost:9200/test_index/item/4?routing=1&pretty' -H 'Content-Type: application/json' -d '
{
  "text": "post2",
  "type": {"name": "post", "parent": 1} 
}'

curl -XPUT 'http://localhost:9200/test_index/item/5?routing=1&pretty' -H 'Content-Type: application/json' -d '
{
  "text": "post3",
  "type": {"name": "post", "parent": 2} 
}'

curl -XPUT 'http://localhost:9200/test_index/item/6?routing=1&pretty' -H 'Content-Type: application/json' -d '
{
  "text": "post4",
  "type": {"name": "post", "parent": 2} 
}'

curl -XPUT 'http://localhost:9200/test_index/item/7?routing=1&pretty' -H 'Content-Type: application/json' -d '
{
  "text": "post5",
  "type": {"name": "post", "parent": 2} 
}'

Query

curl -XGET 'http://localhost:9200/test_index/_search' -H 'Content-Type: application/json' -d '
{
  "query": {
    "parent_id": { 
      "type": "post"
    }
  },
  "aggs": {
    "parents": {
      "terms": {
        "field": "type#user", 
        "size": 10
      }
    }
  },
  "script_fields": {
    "parent": {
      "script": {
         "source": "doc['type#user']" 
      }
    }
  }
}'

Result

{
  "error" : {
    "root_cause" : [
      {
        "type" : "script_exception",
        "reason" : "compile error",
        "script_stack" : [
          "doc[type#user]",
          "        ^---- HERE"
        ],
        "script" : "doc[type#user]",
        "lang" : "painless"
      }
    ],
    "type" : "search_phase_execution_exception",
    "reason" : "all shards failed",
    "phase" : "query",
    "grouped" : true,
    "failed_shards" : [
      {
        "shard" : 0,
        "index" : "test_index",
        "node" : "s4udXhJ0QJu6D5s7LhHnCA",
        "reason" : {
          "type" : "script_exception",
          "reason" : "compile error",
          "script_stack" : [
            "doc[type#user]",
            "        ^---- HERE"
          ],
          "script" : "doc[type#user]",
          "lang" : "painless",
          "caused_by" : {
            "type" : "illegal_argument_exception",
            "reason" : "unexpected character [#].",
            "caused_by" : {
              "type" : "lexer_no_viable_alt_exception",
              "reason" : null
            }
          }
        }
      }
    ]
  },
  "status" : 500
}

If you use curl you need to encode the single quote. "'" is not defined in the json spec so you have to replace ' with the unicode point \u0027:

doc[\u0027type#user\u0027]

Note that the conversion is done automatically when you use the COPY AS CURL button in the documentation.

You are right! Thank you for help!