创建或更新同义词集编辑

创建或更新同义词集。

同义词集最多包含 10,000 条同义词规则。超过 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 的新同义词集

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
PUT _synonyms/my-synonyms-set
{
  "synonyms_set": [
    {
      "id": "test-1",
      "synonyms": "hello, hi"
    },
    {
      "synonyms": "bye, goodbye"
    },
    {
      "id": "test-2",
      "synonyms": "test => check"
    }
  ]
}

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

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

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

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
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"
        ]
      }
    ]
  }
}