1

when writing a search query with a script, I can access fields using "doc['myfield']"

curl -XPOST 'http://localhost:9200/index1/type1/_search' -d '
{
  "query": {
    "filtered": {
      "query": {
        "match_all": {}
      },
      "filter": {
        "script": {
          "script": "doc[\"myfield\"].value>0",
          "params": {},
          "lang":"python"
        }
      }
    }
  }
}'

how do I go about accessing the _id or _parent fields?

The "ctx" object does not seem to be available in a search query (while it is accessible in an update API request, why?).

Mind you, I am using the python language instead of mvel, but both of them pose the same question.

1
  • can you please tell me to acces doc["my_field"] what code you have written also should i have to add anything in the yml file or should i have to make a different file inside my config/scripts folder ?? Please tell me i am searching for a very long time into this ?? Commented Sep 14, 2015 at 12:45

1 Answer 1

5

By default, both document id and parent id are indexed in uid format: type#id. Elasticsearch provides a few methods that can be used to extract type and id from uid string. Here is an example of using these methods in MVEL:

curl -XDELETE localhost:9200/test
curl -XPUT localhost:9200/test -d '{
    "settings": {
        "index.number_of_shards": 1,
        "index.number_of_replicas": 0
    },
    "mappings": {
        "doc": {
            "properties": {
                "name": {
                    "type": "string"
                }
            }
        },
        "child_doc": {
            "_parent": {
                "type": "doc"
            },
            "properties": {
                "name": {
                    "type": "string"
                }
            }
        }
    }
}'
curl -XPUT "localhost:9200/test/doc/1" -d '{"name": "doc 1"}'
curl -XPUT "localhost:9200/test/child_doc/1-1?parent=1" -d '{"name": "child 1-1 of doc 1"}'
curl -XPOST "localhost:9200/test/_refresh"
echo
curl "localhost:9200/test/child_doc/_search?pretty=true" -d '{
    "script_fields": {
        "uid_in_script": {
            "script": "doc[\"_uid\"].value"
        },
        "id_in_script": {
            "script": "org.elasticsearch.index.mapper.Uid.idFromUid(doc[\"_uid\"].value)"
        },
        "parent_uid_in_script": {
            "script": "doc[\"_parent\"].value"
        },
        "parent_id_in_script": {
            "script": "org.elasticsearch.index.mapper.Uid.idFromUid(doc[\"_parent\"].value)"
        },
        "parent_type_in_script": {
            "script": "org.elasticsearch.index.mapper.Uid.typeFromUid(doc[\"_parent\"].value)"
        }
    }
}'
echo
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.