Create or update synonyms set

Create or update synonyms set

New API reference

For the most up-to-date API details, refer to Synonyms APIs.

Creates or updates a synonyms set.

Synonyms sets are limited to a maximum of 10,000 synonym rules per set. If you need to manage more synonym rules, you can create multiple synonyms sets.

Request

PUT _synonyms/<synonyms_set>

Prerequisites

Requires the manage_search_synonyms cluster privilege.

Path parameters

<synonyms_set>

(Required, string) Synonyms set identifier to create. This identifier will be used by other Synonyms APIs to manage the synonyms set.

Request body

synonyms_set

(Required, array of synonym rules objects) The synonym rules definitions for the synonyms set.

Properties of synonyms_set objects

id

(Optional, string) The identifier associated to the synonym rule, that can be used to manage individual synonym rules via synonym rules APIs. In case a synonym rule id is not specified, an identifier will be created automatically by Elasticsearch.

synonyms

(Required, string) The synonym rule. This needs to be in Solr format. Some examples are:

  • “i-pod, i pod ⇒ ipod”,
  • “universe, cosmos”

Examples

The following example creates a new synonyms set called my-synonyms-set:

  1. resp = client.synonyms.put_synonym(
  2. id="my-synonyms-set",
  3. synonyms_set=[
  4. {
  5. "id": "test-1",
  6. "synonyms": "hello, hi"
  7. },
  8. {
  9. "synonyms": "bye, goodbye"
  10. },
  11. {
  12. "id": "test-2",
  13. "synonyms": "test => check"
  14. }
  15. ],
  16. )
  17. print(resp)
  1. response = client.synonyms.put_synonym(
  2. id: 'my-synonyms-set',
  3. body: {
  4. synonyms_set: [
  5. {
  6. id: 'test-1',
  7. synonyms: 'hello, hi'
  8. },
  9. {
  10. synonyms: 'bye, goodbye'
  11. },
  12. {
  13. id: 'test-2',
  14. synonyms: 'test => check'
  15. }
  16. ]
  17. }
  18. )
  19. puts response
  1. const response = await client.synonyms.putSynonym({
  2. id: "my-synonyms-set",
  3. synonyms_set: [
  4. {
  5. id: "test-1",
  6. synonyms: "hello, hi",
  7. },
  8. {
  9. synonyms: "bye, goodbye",
  10. },
  11. {
  12. id: "test-2",
  13. synonyms: "test => check",
  14. },
  15. ],
  16. });
  17. console.log(response);
  1. PUT _synonyms/my-synonyms-set
  2. {
  3. "synonyms_set": [
  4. {
  5. "id": "test-1",
  6. "synonyms": "hello, hi"
  7. },
  8. {
  9. "synonyms": "bye, goodbye"
  10. },
  11. {
  12. "id": "test-2",
  13. "synonyms": "test => check"
  14. }
  15. ]
  16. }

If any of the synonym rules included is not valid, the API will return an error.

  1. resp = client.synonyms.put_synonym(
  2. id="my-synonyms-set",
  3. synonyms_set=[
  4. {
  5. "synonyms": "hello => hi => howdy"
  6. }
  7. ],
  8. )
  9. print(resp)
  1. const response = await client.synonyms.putSynonym({
  2. id: "my-synonyms-set",
  3. synonyms_set: [
  4. {
  5. synonyms: "hello => hi => howdy",
  6. },
  7. ],
  8. });
  9. console.log(response);
  1. PUT _synonyms/my-synonyms-set
  2. {
  3. "synonyms_set": [
  4. {
  5. "synonyms": "hello => hi => howdy"
  6. }
  7. ]
  8. }
  1. {
  2. "error": {
  3. "root_cause": [
  4. {
  5. "type": "action_request_validation_exception",
  6. "reason": "Validation Failed: 1: More than one explicit mapping specified in the same synonyms rule: [hello => hi => howdy];",
  7. "stack_trace": ...
  8. }
  9. ],
  10. "type": "action_request_validation_exception",
  11. "reason": "Validation Failed: 1: More than one explicit mapping specified in the same synonyms rule: [hello => hi => howdy];",
  12. "stack_trace": ...
  13. },
  14. "status": 400
  15. }

Analyzer reloading

When an existing synonyms set is updated, the search analyzers that use the synonyms set are reloaded automatically for all indices. This would be equivalent to invoking Reload search analyzers API for all indices that use the synonyms set.

For example, creating an index with a synonyms set and updating it:

  1. resp = client.synonyms.put_synonym(
  2. id="my-synonyms-set",
  3. synonyms_set=[
  4. {
  5. "id": "test-1",
  6. "synonyms": "hello, hi"
  7. }
  8. ],
  9. )
  10. print(resp)
  11. resp1 = client.indices.create(
  12. index="test-index",
  13. settings={
  14. "analysis": {
  15. "filter": {
  16. "synonyms_filter": {
  17. "type": "synonym_graph",
  18. "synonyms_set": "my-synonyms-set",
  19. "updateable": True
  20. }
  21. },
  22. "analyzer": {
  23. "my_index_analyzer": {
  24. "type": "custom",
  25. "tokenizer": "standard",
  26. "filter": [
  27. "lowercase"
  28. ]
  29. },
  30. "my_search_analyzer": {
  31. "type": "custom",
  32. "tokenizer": "standard",
  33. "filter": [
  34. "lowercase",
  35. "synonyms_filter"
  36. ]
  37. }
  38. }
  39. }
  40. },
  41. mappings={
  42. "properties": {
  43. "title": {
  44. "type": "text",
  45. "analyzer": "my_index_analyzer",
  46. "search_analyzer": "my_search_analyzer"
  47. }
  48. }
  49. },
  50. )
  51. print(resp1)
  52. resp2 = client.synonyms.put_synonym(
  53. id="my-synonyms-set",
  54. synonyms_set=[
  55. {
  56. "id": "test-1",
  57. "synonyms": "hello, hi, howdy"
  58. }
  59. ],
  60. )
  61. print(resp2)
  1. response = client.synonyms.put_synonym(
  2. id: 'my-synonyms-set',
  3. body: {
  4. synonyms_set: [
  5. {
  6. id: 'test-1',
  7. synonyms: 'hello, hi'
  8. }
  9. ]
  10. }
  11. )
  12. puts response
  13. response = client.indices.create(
  14. index: 'test-index',
  15. body: {
  16. settings: {
  17. analysis: {
  18. filter: {
  19. synonyms_filter: {
  20. type: 'synonym_graph',
  21. synonyms_set: 'my-synonyms-set',
  22. updateable: true
  23. }
  24. },
  25. analyzer: {
  26. my_index_analyzer: {
  27. type: 'custom',
  28. tokenizer: 'standard',
  29. filter: [
  30. 'lowercase'
  31. ]
  32. },
  33. my_search_analyzer: {
  34. type: 'custom',
  35. tokenizer: 'standard',
  36. filter: [
  37. 'lowercase',
  38. 'synonyms_filter'
  39. ]
  40. }
  41. }
  42. }
  43. },
  44. mappings: {
  45. properties: {
  46. title: {
  47. type: 'text',
  48. analyzer: 'my_index_analyzer',
  49. search_analyzer: 'my_search_analyzer'
  50. }
  51. }
  52. }
  53. }
  54. )
  55. puts response
  56. response = client.synonyms.put_synonym(
  57. id: 'my-synonyms-set',
  58. body: {
  59. synonyms_set: [
  60. {
  61. id: 'test-1',
  62. synonyms: 'hello, hi, howdy'
  63. }
  64. ]
  65. }
  66. )
  67. puts response
  1. const response = await client.synonyms.putSynonym({
  2. id: "my-synonyms-set",
  3. synonyms_set: [
  4. {
  5. id: "test-1",
  6. synonyms: "hello, hi",
  7. },
  8. ],
  9. });
  10. console.log(response);
  11. const response1 = await client.indices.create({
  12. index: "test-index",
  13. settings: {
  14. analysis: {
  15. filter: {
  16. synonyms_filter: {
  17. type: "synonym_graph",
  18. synonyms_set: "my-synonyms-set",
  19. updateable: true,
  20. },
  21. },
  22. analyzer: {
  23. my_index_analyzer: {
  24. type: "custom",
  25. tokenizer: "standard",
  26. filter: ["lowercase"],
  27. },
  28. my_search_analyzer: {
  29. type: "custom",
  30. tokenizer: "standard",
  31. filter: ["lowercase", "synonyms_filter"],
  32. },
  33. },
  34. },
  35. },
  36. mappings: {
  37. properties: {
  38. title: {
  39. type: "text",
  40. analyzer: "my_index_analyzer",
  41. search_analyzer: "my_search_analyzer",
  42. },
  43. },
  44. },
  45. });
  46. console.log(response1);
  47. const response2 = await client.synonyms.putSynonym({
  48. id: "my-synonyms-set",
  49. synonyms_set: [
  50. {
  51. id: "test-1",
  52. synonyms: "hello, hi, howdy",
  53. },
  54. ],
  55. });
  56. console.log(response2);
  1. PUT _synonyms/my-synonyms-set
  2. {
  3. "synonyms_set": [
  4. {
  5. "id": "test-1",
  6. "synonyms": "hello, hi"
  7. }
  8. ]
  9. }
  10. PUT /test-index
  11. {
  12. "settings": {
  13. "analysis": {
  14. "filter": {
  15. "synonyms_filter": {
  16. "type": "synonym_graph",
  17. "synonyms_set": "my-synonyms-set",
  18. "updateable": true
  19. }
  20. },
  21. "analyzer": {
  22. "my_index_analyzer": {
  23. "type": "custom",
  24. "tokenizer": "standard",
  25. "filter": ["lowercase"]
  26. },
  27. "my_search_analyzer": {
  28. "type": "custom",
  29. "tokenizer": "standard",
  30. "filter": ["lowercase", "synonyms_filter"]
  31. }
  32. }
  33. }
  34. },
  35. "mappings": {
  36. "properties": {
  37. "title": {
  38. "type": "text",
  39. "analyzer": "my_index_analyzer",
  40. "search_analyzer": "my_search_analyzer"
  41. }
  42. }
  43. }
  44. }
  45. PUT _synonyms/my-synonyms-set
  46. {
  47. "synonyms_set": [
  48. {
  49. "id": "test-1",
  50. "synonyms": "hello, hi, howdy"
  51. }
  52. ]
  53. }

The reloading result is included as part of the response:

  1. {
  2. "result": "updated",
  3. "reload_analyzers_details": {
  4. "_shards": {
  5. "total": 2,
  6. "successful": 1,
  7. "failed": 0
  8. },
  9. "reload_details": [
  10. {
  11. "index": "test-index",
  12. "reloaded_analyzers": [
  13. "my_search_analyzer"
  14. ],
  15. "reloaded_node_ids": [
  16. "1wYFZzq8Sxeu_Jvt9mlbkg"
  17. ]
  18. }
  19. ]
  20. }
  21. }