elasticsearch/docs/reference/synonyms/apis/put-synonym-rule.asciidoc
Kathleen DeRusso a2dc45bc02
[Synonyms] Mark Synonyms as GA (#103223)
* Remove beta from synonyms docs and json definitions

---------

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
2023-12-14 14:53:43 -05:00

171 lines
3.6 KiB
Text

[[put-synonym-rule]]
=== Create or update synonym rule
++++
<titleabbrev>Create or update synonym rule</titleabbrev>
++++
Creates or updates a synonym rule for a synonym set.
[[put-synonym-rule-request]]
==== {api-request-title}
`PUT _synonyms/<synonyms_set>/<synonym_rule>`
[[put-synonym-rule-prereqs]]
==== {api-prereq-title}
Requires the `manage_search_synonyms` cluster privilege.
[[put-synonym-rule-path-params]]
==== {api-path-parms-title}
`<synonyms_set>`::
(Required, string)
Synonyms set identifier to update.
`<synonym_rule>`::
(Required, string)
Synonym rule identifier to create or update.
[[put-synonym-rule-request-body]]
==== {api-request-body-title}
`synonyms`::
(Required, string)
The synonym rule definition.
This needs to be in <<analysis-synonym-graph-define-synonyms,Solr format>>. Some examples are:
* "i-pod, i pod => ipod",
* "universe, cosmos"
[[put-synonym-rule-example]]
==== {api-examples-title}
The following example updates an existing synonym rule called `test-1` for the synonyms set `my-synonyms-set`:
////
[source,console]
----
PUT _synonyms/my-synonyms-set
{
"synonyms_set": [
{
"id": "test-1",
"synonyms": "hello, hi"
},
{
"synonyms": "bye, goodbye"
},
{
"id": "test-2",
"synonyms": "test => check"
}
]
}
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"
}
}
}
}
----
// TESTSETUP
////
[source,console]
----
PUT _synonyms/my-synonyms-set/test-1
{
"synonyms": "hello, hi, howdy"
}
----
[source,console-result]
----
{
"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"
]
}
]
}
}
----
// TESTRESPONSE[s/1wYFZzq8Sxeu_Jvt9mlbkg/$body.reload_analyzers_details.reload_details.0.reloaded_node_ids.0/]
All analyzers using this synonyms set will be <<synonyms-set-analyzer-reloading,reloaded automatically>> to reflect the new rule.
If any of the synonym rules included is invalid, the API will return an error.
[source,console]
----
PUT _synonyms/my-synonyms-set/test-1
{
"synonyms": "hello => hi => howdy"
}
----
// TEST[catch:bad_request]
[source,console-result]
----
{
"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
}
----
// TESTRESPONSE[s/"stack_trace": \.\.\./"stack_trace": $body.$_path/]