PUT hamlet_1/_doc/_bulk
{"index":{"_index":"hamlet_1","_id":"C0"}}{"name":"HAMLET","relationship":[{"name":"HORATIO","type":"friend"},{"name":"GERTRUDE","type":"mother"}]}{"index":{"_index":"hamlet_1","_id":"C1"}}{"name":"KING CLAUDIUS","relationship":[{"name":"HAMLET","type":"nephew"}]}
第1題,題解
創建索引
PUT hamlet_1
{"settings":{"number_of_shards": 1,"number_of_replicas": 0}}
POST hamlet_2/_search
{"query":{"nested":{"path":"relationship","query":{"bool":{"must":[{"match":{"relationship.type":"friend"}},{"match":{"relationship.name":"Gertrude"}}]}}}}}
Add more documents to hamlet_2 by running the following command
用下面命令給hamlet_2多塞點數據
POST _bulk
{"index":{"_index":"hamlet_2", "_id":"LO"}}{"line_number":"1.4.1","speaker":"HAMLET","text_entry":"The air bites shrewdly; it is very cold."}{"index":{"_index":"hamlet_2","_id":"L1"}}{"line_number":"1.4.2","speaker":"HORATIO","text_entry":"It is a nipping and an eager air."}{"index":{"_index":"hamlet_2","_id":"L2"}}{"line_number":"1.4.3","speaker":"HAMLET","text_entry":"What hour now?"}
Create the index hamlet_3 with only one primary shard and no replicas
創建一個1分片0副本的索引hamlet_3
Copy the mapping of hamlet_2 into hamlet_3, but also add a join field to define a relation between a character (the parent) and a line (the child). The name of such field is “character_or_line”
Create a script named init_lines and save it into the cluster state. The script:
has a parameter named characterId,
adds the field character_or_line to the document,
sets the value of character_or_line.name to “line” ,
sets the value of character_or_line.parent to the value of the characterId parameter
Update the document with id C0 (i.e., the character document of Hamlet) by adding the field character_or_line and setting its character_or_line.name value to “character”
Update the documents in hamlet_3 that have “HAMLET” as a speaker, by running the init_lines script with characterId set to “C0”
這里如果不加routing的設置直接進行更新,可能會報這個錯:大意是對于父子關聯的字段,routing是必須存在的。{"took":10,"timed_out":false,"total":1,"updated":0,"deleted":0,"batches":1,"version_conflicts":0,"noops":0,"retries":{"bulk":0,"search":0},"throttled_millis":0,"requests_per_second":-1,"throttled_until_millis":0,"failures":[{"index":"hamlet_3","type":"_doc","id":"C2","cause":{"type":"mapper_parsing_exception","reason":"failed to parse","caused_by":{"type":"illegal_argument_exception","reason":"[routing] is missing for join field [character_or_line]"}},"status":400}]}
校驗數據:GET hamlet_3/_doc/C2
返回值
{"_index":"hamlet_3","_type":"_doc","_id":"C2","_version":4,"_seq_no":5,"_primary_term":1,"_routing":"C0","found":true,"_source":{"character_or_line":{"parent":"C0","name":"line"},"line_number":"1.2.1","text_entry":"Though yet of Hamlet our dear brothers death","speaker":"KING CLAUDIUS"}}
創建指定routing用的pipelinePUT _ingest/pipeline/set_routing
{"description":"assign the routing attribute for doc","processors":[{"script":{"lang":"painless","source":"ctx._routing = 'C0'"}}]}