1

スクリプトで検索クエリを書くとき、「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"
        }
      }
    }
  }
}'

_id または _parent フィールドにアクセスするにはどうすればよいですか?

「ctx」オブジェクトは、検索クエリで使用できないようです (更新 API 要求でアクセスできるのに、なぜですか?)。

私は mvel の代わりに python 言語を使用していますが、どちらも同じ問題を提起しています。

4

1 に答える 1

5

デフォルトでは、ドキュメント ID と親 ID の両方が uid 形式で索引付けされます: type#id. Elasticsearch には、uid 文字列からタイプと ID を抽出するために使用できるメソッドがいくつか用意されています。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
于 2013-03-21T03:44:32.640 に答える