要在 /music 索引下创建一个类型,可插入一个文档。在第一个示例中,您的文档包含数据(包含一行)“deck the halls” 的歌词,这是一首最初由威尔士诗人 john ceirog hughes 于 1885 年编写的传统的圣诞歌曲。
要将包含 “deck the halls” 的文档插入索引中,可运行以下命令(将该命令和本教程的其他 curl 命令都键入到一行中):
curl -xput "http://localhost:9200/music/songs/1" -d ' { "name": "deck the halls", "year": 1885, "lyrics": "fa la la la la" }'
前面的命令使用 put 动词将一个文档添加到 /songs 文档类型,并为该文档分配 id 1。url 路径显示为 index/doctype/id。
查看文档
要查看该文档,可使用简单的 get 命令:
curl -xget "http://localhost:9200/music/songs/1"
elasticsearch 使用您之前 put 进索引中的 json 内容作为响应:
{"_index":"music","_type":"songs","_id":"1","_version":1,"found":true,"_source": { "name": "deck the halls", "year": 1885, "lyrics": "fa la la la la" }}
更新文档
如果您认识到日期写错了,并想将它更改为 1886 怎么办?可运行以下命令来更新文档:
curl -xput "http://localhost:9200/music/lyrics/1" -d '{ "name": "deck the halls", "year": 1886, "lyrics": "fa la la la la" }'
{ "artist": "wallace saunders", "year": 1909, "styles": ["traditional"], "album": "unknown", "name": "ballad of casey jones", "lyrics": "come all you rounders if you want to hear the story of a brave engineer casey jones was the rounder's name.... come all you rounders if you want to hear the story of a brave engineer casey jones was the rounder's name on the six-eight wheeler, boys, he won his fame the caller called casey at half past four he kissed his wife at the station door he mounted to the cabin with the orders in his hand and he took his farewell trip to that promis'd land chorus: casey jones--mounted to his cabin casey jones--with his orders in his hand casey jones--mounted to his cabin and he took his... land" }
{ "artist": "clarence ashley", "year": 1920 "name": "walking boss", "styles": ["folk","protest"], "album": "traditional", "lyrics": "walkin' boss walkin' boss walkin' boss i don't belong to you i belong i belong i belong to that steel driving crew well you work one day work one day work one day then go lay around the shanty two" }
{"took":107,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":2,"max _score":0.15625,"hits":[{"_index":"music","_type":"songs","_id":"2","_ score":0.15625,"_source":{"artist": "wallace saunders","year": 1909,"styles": ["traditional"],"album": "unknown","name": "ballad of casey jones","lyrics": "come all you rounders if you want to hear the story of a brave engineer casey jones was the rounder's name.... come all you rounders if you want to hear the story of a brave engineer casey jones was the rounder's name on the six-eight wheeler, boys, he won his fame the caller called casey at half past four he kissed his wife at the station door he mounted to the cabin with the orders in his hand and he took his farewell trip to that promis'd land chorus: casey jones--mounted to his cabin casey jones--with his orders in his hand casey jones--mounted to his cabin and he took his... land" }},{"_index":"music","_type":"songs","_id":"3","_score":0.06780553,"_source":{"artist": "clarence ashley","year": 1920,"name": "walking boss","styles": ["folk","protest"],"album": "traditional","lyrics": "walkin' boss walkin' boss walkin' boss i don't belong to you i belong i belong i belong to that steel driving crew well you work one day work one day work one day then go lay around the shanty two"}}]}}
这一行显示了搜索 api 的简单用法。使用 preparesearch 方法指定一个索引(在本例中为 music),然后执行查询。查询基本上显示为 “give me all of the records in the music index.”。另外,将文档类型设置为 lyrics,但在这个简单用例中没有必要这么做,因为索引仅包含一种文档类型。在更大的应用程序,需要执行这种设置。这个 api 调用类似于您之前看到的 curl -xget "http://localhost:9200/music/lyrics/_search" 调用。