创建或更新同义词集

编辑

创建或更新一个同义词集。

同义词集每个集合最多限制为 10,000 条同义词规则。如果需要管理更多的同义词规则,可以创建多个同义词集。

请求

编辑

PUT _synonyms/<synonyms_set>

前提条件

编辑

需要 manage_search_synonyms 集群权限。

路径参数

编辑
<synonyms_set>
(必需,字符串)要创建的同义词集标识符。此标识符将由其他 同义词 API 用来管理同义词集。

请求体

编辑
synonyms_set
(必需,同义词规则对象数组)同义词集的同义词规则定义。
synonyms_set 对象的属性
id
(可选,字符串)与同义词规则关联的标识符,可以通过 同义词规则 API 来管理单个同义词规则。如果未指定同义词规则 ID,Elasticsearch 将自动创建一个标识符。
synonyms

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

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

示例

编辑

以下示例创建一个名为 my-synonyms-set 的新同义词集

resp = client.synonyms.put_synonym(
    id="my-synonyms-set",
    synonyms_set=[
        {
            "id": "test-1",
            "synonyms": "hello, hi"
        },
        {
            "synonyms": "bye, goodbye"
        },
        {
            "id": "test-2",
            "synonyms": "test => check"
        }
    ],
)
print(resp)
response = client.synonyms.put_synonym(
  id: 'my-synonyms-set',
  body: {
    synonyms_set: [
      {
        id: 'test-1',
        synonyms: 'hello, hi'
      },
      {
        synonyms: 'bye, goodbye'
      },
      {
        id: 'test-2',
        synonyms: 'test => check'
      }
    ]
  }
)
puts response
const response = await client.synonyms.putSynonym({
  id: "my-synonyms-set",
  synonyms_set: [
    {
      id: "test-1",
      synonyms: "hello, hi",
    },
    {
      synonyms: "bye, goodbye",
    },
    {
      id: "test-2",
      synonyms: "test => check",
    },
  ],
});
console.log(response);
PUT _synonyms/my-synonyms-set
{
  "synonyms_set": [
    {
      "id": "test-1",
      "synonyms": "hello, hi"
    },
    {
      "synonyms": "bye, goodbye"
    },
    {
      "id": "test-2",
      "synonyms": "test => check"
    }
  ]
}

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

resp = client.synonyms.put_synonym(
    id="my-synonyms-set",
    synonyms_set=[
        {
            "synonyms": "hello => hi => howdy"
        }
    ],
)
print(resp)
const response = await client.synonyms.putSynonym({
  id: "my-synonyms-set",
  synonyms_set: [
    {
      synonyms: "hello => hi => howdy",
    },
  ],
});
console.log(response);
PUT _synonyms/my-synonyms-set
{
  "synonyms_set": [
    {
      "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
}

分析器重载

编辑

当现有同义词集更新时,使用该同义词集的 搜索分析器 会自动为所有索引重新加载。这相当于为所有使用同义词集的索引调用 重新加载搜索分析器 API

例如,创建一个带有同义词集的索引并更新它

resp = client.synonyms.put_synonym(
    id="my-synonyms-set",
    synonyms_set=[
        {
            "id": "test-1",
            "synonyms": "hello, hi"
        }
    ],
)
print(resp)

resp1 = client.indices.create(
    index="test-index",
    settings={
        "analysis": {
            "filter": {
                "synonyms_filter": {
                    "type": "synonym_graph",
                    "synonyms_set": "my-synonyms-set",
                    "updateable": True
                }
            },
            "analyzer": {
                "my_index_analyzer": {
                    "type": "custom",
                    "tokenizer": "standard",
                    "filter": [
                        "lowercase"
                    ]
                },
                "my_search_analyzer": {
                    "type": "custom",
                    "tokenizer": "standard",
                    "filter": [
                        "lowercase",
                        "synonyms_filter"
                    ]
                }
            }
        }
    },
    mappings={
        "properties": {
            "title": {
                "type": "text",
                "analyzer": "my_index_analyzer",
                "search_analyzer": "my_search_analyzer"
            }
        }
    },
)
print(resp1)

resp2 = client.synonyms.put_synonym(
    id="my-synonyms-set",
    synonyms_set=[
        {
            "id": "test-1",
            "synonyms": "hello, hi, howdy"
        }
    ],
)
print(resp2)
response = client.synonyms.put_synonym(
  id: 'my-synonyms-set',
  body: {
    synonyms_set: [
      {
        id: 'test-1',
        synonyms: 'hello, hi'
      }
    ]
  }
)
puts response

response = client.indices.create(
  index: 'test-index',
  body: {
    settings: {
      analysis: {
        filter: {
          synonyms_filter: {
            type: 'synonym_graph',
            synonyms_set: 'my-synonyms-set',
            updateable: true
          }
        },
        analyzer: {
          my_index_analyzer: {
            type: 'custom',
            tokenizer: 'standard',
            filter: [
              'lowercase'
            ]
          },
          my_search_analyzer: {
            type: 'custom',
            tokenizer: 'standard',
            filter: [
              'lowercase',
              'synonyms_filter'
            ]
          }
        }
      }
    },
    mappings: {
      properties: {
        title: {
          type: 'text',
          analyzer: 'my_index_analyzer',
          search_analyzer: 'my_search_analyzer'
        }
      }
    }
  }
)
puts response

response = client.synonyms.put_synonym(
  id: 'my-synonyms-set',
  body: {
    synonyms_set: [
      {
        id: 'test-1',
        synonyms: 'hello, hi, howdy'
      }
    ]
  }
)
puts response
const response = await client.synonyms.putSynonym({
  id: "my-synonyms-set",
  synonyms_set: [
    {
      id: "test-1",
      synonyms: "hello, hi",
    },
  ],
});
console.log(response);

const response1 = await client.indices.create({
  index: "test-index",
  settings: {
    analysis: {
      filter: {
        synonyms_filter: {
          type: "synonym_graph",
          synonyms_set: "my-synonyms-set",
          updateable: true,
        },
      },
      analyzer: {
        my_index_analyzer: {
          type: "custom",
          tokenizer: "standard",
          filter: ["lowercase"],
        },
        my_search_analyzer: {
          type: "custom",
          tokenizer: "standard",
          filter: ["lowercase", "synonyms_filter"],
        },
      },
    },
  },
  mappings: {
    properties: {
      title: {
        type: "text",
        analyzer: "my_index_analyzer",
        search_analyzer: "my_search_analyzer",
      },
    },
  },
});
console.log(response1);

const response2 = await client.synonyms.putSynonym({
  id: "my-synonyms-set",
  synonyms_set: [
    {
      id: "test-1",
      synonyms: "hello, hi, howdy",
    },
  ],
});
console.log(response2);
PUT _synonyms/my-synonyms-set
{
    "synonyms_set": [
        {
            "id": "test-1",
            "synonyms": "hello, hi"
        }
    ]
}

PUT /test-index
{
  "settings": {
    "analysis": {
      "filter": {
        "synonyms_filter": {
          "type": "synonym_graph",
          "synonyms_set": "my-synonyms-set",
          "updateable": true
        }
      },
      "analyzer": {
        "my_index_analyzer": {
          "type": "custom",
          "tokenizer": "standard",
          "filter": ["lowercase"]
        },
        "my_search_analyzer": {
          "type": "custom",
          "tokenizer": "standard",
          "filter": ["lowercase", "synonyms_filter"]
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "title": {
        "type": "text",
        "analyzer": "my_index_analyzer",
        "search_analyzer": "my_search_analyzer"
      }
    }
  }
}

PUT _synonyms/my-synonyms-set
{
    "synonyms_set": [
        {
            "id": "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"
        ]
      }
    ]
  }
}