powershell 如何使用API在OpenSearch中创建索引模式?

wwwo4jvm  于 2024-01-08  发布在  Shell
关注(0)|答案(1)|浏览(228)

我想使用Opensearch API创建一个索引模式。我遇到的问题是消息正文类型。运行脚本时收到以下消息
我在运行OpenSearch1.3

  1. Invoke-RestMethod: {"statusCode":400,"error":"Bad Request","message":"[request body.attributes]: expected value of type [object] but got [undefined]"}

个字符
我尝试了各种方法来定义对象,但都没有成功。|convertfrom-json)我得到一个不同的错误消息。

  1. Invoke-RestMethod: {"statusCode":400,"error":"Bad Request","message":"[request body]: expected a plain object value, but found [null] instead."}


最新消息:我最初确实尝试使用创建索引模式API来实现弹性,但这会导致创建一个新的索引,而不是一个opensearch index_pattern。

  1. $body=@{
  2. "index_pattern"= @{
  3. "title"= "ai-summary"
  4. }
  5. }
  6. $uri = "$baseuri/api/index_patterns/$indexid"
  7. Invoke-RestMethod -Method Post -Uri $uri -Headers @{"osd-xsrf"="true"} -ContentType "application/json" -WebSession $S1 -Body ($body | ConvertTo-Json) -Verbose -Credential $credObject

xn1cxnb4

xn1cxnb41#

我认为问题在于,当使用saved_objects端点时,您需要在POST数据中使用"attributes"而不是"index_pattern"。(请注意您的错误消息,“[request body.attributes]:expected value of type [object] but got [undefined]"。)因此,您的body将是:

  1. {
  2. "attributes": {
  3. "title": "indexname-*",
  4. "timeFieldName": "@timestamp"
  5. }
  6. }

字符串
我不熟悉您使用的语言,但类似以下shell脚本的东西对我有用(在OpenSearch 2.5下,但我相信它也可以在OpenSearch 1.3下工作)。

  1. #! /bin/bash
  2. ESHOST=my-host
  3. ID=my-index-pattern-id
  4. PATTERN='my-indexname*'
  5. TIMESTAMP=timestamp
  6. curl "https://$ESHOST/_dashboards/api/saved_objects/index-pattern/$ID" \
  7. -H 'osd-xsrf: true' \
  8. -H 'Content-Type: application/json' \
  9. --data-binary @- \
  10. <<EOF
  11. {
  12. "attributes": {
  13. "title": "$PATTERN",
  14. "timeFieldName": "$TIMESTAMP"
  15. }
  16. }
  17. EOF


正如你所提到的,OpenSearch似乎不支持Kibana's index_patterns API。(或者至少我也不能让它工作。)我也不确定saved_objects端点是否做了index_patterns API所做的一切。至少当我使用OpenSearch Jmeter 板访问我创建的索引模式时,它会用索引的字段更新索引模式。

展开查看全部

相关问题