我试图通过PowerShell将一些数据推送到ElasticSearch。我将数据转换为JSON,然后同时尝试Invoke-WebRequest
和Invoke-RestMethod
,但始终会在格式不正确的数据或不支持的内容类型上出错。我还没有创建索引,因为我相信它会为我创建它。
任何人都可以帮助解决我错过/做错的事情吗?
示例代码:
$data = @()
$CustomObject = [pscustomobject]@{
SqlInstance = "myserver1"
Database = "mydb"
Schema = "versioning"
Name = "DataVersionHistory"
IndexSpaceUsed = 0
DataSpaceUsed = 0
RowCount = 0
};
$data += $CustomObject;
$CustomObject = [pscustomobject]@{
SqlInstance = "myserver1"
Database = "mydb"
Schema = "versioning"
Name = "VersionHistory"
IndexSpaceUsed = 10
DataSpaceUsed = 25
RowCount = 3000
};
$data += $CustomObject;
$myJson = ConvertTo-Json -InputObject $data ;
Invoke-RestMethod -Uri http://localhost:9200/myindex/mytype/_bulk?pretty `
-Method POST -Body $myJson -ContentType "application/json"
答案 0 :(得分:0)
批量请求不是实际的json。您应该使用以下符号:
curl -XPOST 'localhost:9200/_bulk?pretty' -H 'Content-Type: application/json' -d'
{ "index" : { "_index" : "test", "_type" : "type1", "_id" : "1" } }
{ "field1" : "value1" }
{ "delete" : { "_index" : "test", "_type" : "type1", "_id" : "2" } }
{ "create" : { "_index" : "test", "_type" : "type1", "_id" : "3" } }
{ "field1" : "value3" }
{ "update" : {"_id" : "1", "_type" : "type1", "_index" : "test"} }
{ "doc" : {"field2" : "value2"} }
'
答案 1 :(得分:0)
$data = @()
$CustomObject = [pscustomobject]@{
SqlInstance = "myserver1"
Database = "mydb"
Schema = "versioning"
Name = "DataVersionHistory"
IndexSpaceUsed = 0
DataSpaceUsed = 0
RowCount = 0
};
$data += $CustomObject;
$CustomObject = [pscustomobject]@{
SqlInstance = "myserver1"
Database = "mydb"
Schema = "versioning"
Name = "VersionHistory"
IndexSpaceUsed = 10
DataSpaceUsed = 25
RowCount = 3000
};
$data += $CustomObject
此时,您拥有数组$data
中包含的对象集合。
注意:对于批量API,json数据的最后一行必须以换行符\ n结尾。所以我使用here-string将换行符\n
附加到每个json元素。
还注意到我删除了漂亮的印刷品。这是为了发挥批量api。
doc_as_upsert
确保如果文档存在,请不要单独添加,否则添加为新文件。
$json_col = @()
$data |
ForEach-Object {
#convert object to json
$json_element = @{doc = $_; doc_as_upsert = $true}
| ConvertTo-Json -Depth 1 -Compress
#construt here string with literal \n
$json_col += @"
$json_element\n
"@
}
Invoke-RestMethod -Method Post -Uri "http://localhost:9200/myindex/mytype/_bulk?" -Body $json_col -ErrorAction Stop