创建或更新同义词规则

编辑

为同义词集创建或更新同义词规则。

请求

编辑

PUT _synonyms/<synonyms_set>/<synonym_rule>

前提条件

编辑

需要 manage_search_synonyms 集群权限。

路径参数

编辑
<synonyms_set>
(必需,字符串)要更新的同义词集标识符。
<synonym_rule>
(必需,字符串)要创建或更新的同义词规则标识符。

请求体

编辑
synonyms

(必需,字符串)同义词规则定义。它需要采用 Solr 格式。一些例子是

  • "i-pod, i pod ⇒ ipod",
  • "universe, cosmos"

示例

编辑

以下示例更新名为 test-1 的现有同义词规则,该规则用于同义词集 my-synonyms-set

resp = client.synonyms.put_synonym_rule(
    set_id="my-synonyms-set",
    rule_id="test-1",
    synonyms="hello, hi, howdy",
)
print(resp)
response = client.synonyms.put_synonym_rule(
  set_id: 'my-synonyms-set',
  rule_id: 'test-1',
  body: {
    synonyms: 'hello, hi, howdy'
  }
)
puts response
const response = await client.synonyms.putSynonymRule({
  set_id: "my-synonyms-set",
  rule_id: "test-1",
  synonyms: "hello, hi, howdy",
});
console.log(response);
PUT _synonyms/my-synonyms-set/test-1
{
  "synonyms": "hello, hi, howdy"
}
{
  "result": "updated",
  "reload_analyzers_details": {
    "_shards": {
      "total": 2,
      "successful": 1,
      "failed": 0
    },
    "reload_details": [
      {
        "index": "test-index",
        "reloaded_analyzers": [
          "my_search_analyzer"
        ],
        "reloaded_node_ids": [
          "1wYFZzq8Sxeu_Jvt9mlbkg"
        ]
      }
    ]
  }
}

所有使用此同义词集的分析器都将 自动重新加载 以反映新规则。

如果包含的任何同义词规则无效,API 将返回错误。

resp = client.synonyms.put_synonym_rule(
    set_id="my-synonyms-set",
    rule_id="test-1",
    synonyms="hello => hi => howdy",
)
print(resp)
const response = await client.synonyms.putSynonymRule({
  set_id: "my-synonyms-set",
  rule_id: "test-1",
  synonyms: "hello => hi => howdy",
});
console.log(response);
PUT _synonyms/my-synonyms-set/test-1
{
  "synonyms": "hello => hi => howdy"
}
{
  "error": {
    "root_cause": [
      {
        "type": "action_request_validation_exception",
        "reason": "Validation Failed: 1: More than one explicit mapping specified in the same synonyms rule: [hello => hi => howdy];",
        "stack_trace": ...
      }
    ],
    "type": "action_request_validation_exception",
    "reason": "Validation Failed: 1: More than one explicit mapping specified in the same synonyms rule: [hello => hi => howdy];",
    "stack_trace": ...
  },
  "status": 400
}