云搜索服务 CSS-使用示例:步骤三:使用同义词搜索

时间:2023-11-01 16:18:11

步骤三:使用同义词搜索

Elasticsearch 7.x之前的版本和之后的版本,命令有差别,所以分开举例。

  • 7.x之前的版本
    1. 创建索引“myindex”,配置分词策略。
      PUT myindex{  "settings": {    "analysis": {      "filter": {        "my_synonym": {          "type": "dynamic_synonym"        }      },      "analyzer": {        "ik_synonym": {          "filter": [            "my_synonym"          ],          "type": "custom",          "tokenizer": "ik_smart"        }      }    }  },  "mappings": {    "mytype" :{      "properties": {        "desc": {          "type": "text",          "analyzer": "ik_synonym"        }      }    }  }}
    2. 导入数据,将文本信息导入“myindex”索引中。
      PUT /myindex/mytype/1{    "desc": "我今天获奖了我很开心"}
    3. 使用同义词“高兴”进行文本搜索,并查看搜索结果。
      GET /myindex/_search{  "query": {    "match": {      "desc": "高兴"    }  }}

      搜索结果:

      {  "took" : 2,  "timed_out" : false,  "_shards" : {    "total" : 5,    "successful" : 5,    "skipped" : 0,    "failed" : 0  },  "hits" : {    "total" : 1,    "max_score" : 0.49445358,    "hits" : [      {        "_index" : "myindex",        "_type" : "mytype",        "_id" : "1",        "_score" : 0.49445358,        "_source" : {          "desc" : "我今天获奖了我很开心"        }      }    ]  }}
  • 7.x及之后的版本
    1. 创建索引“myindex”,配置分词策略。
      PUT myindex{    "settings": {        "analysis": {            "filter": {                "my_synonym": {                    "type": "dynamic_synonym"                }            },            "analyzer": {                "ik_synonym": {                    "filter": [                        "my_synonym"                    ],                    "type": "custom",                    "tokenizer": "ik_smart"                }            }        }    },    "mappings": {        "properties": {            "desc": {                "type": "text",                "analyzer": "ik_synonym"            }        }    }}
    2. 导入数据,将文本信息导入“myindex”索引中。
      PUT /myindex/_doc/1{    "desc": "我今天获奖了我很开心"}
    3. 使用同义词“高兴”进行文本搜索,并查看搜索结果。
      GET /myindex/_search{  "query": {    "match": {      "desc": "高兴"    }  }}

      搜索结果:

      {  "took" : 1,  "timed_out" : false,  "_shards" : {    "total" : 1,    "successful" : 1,    "skipped" : 0,    "failed" : 0  },  "hits" : {    "total" : {      "value" : 1,      "relation" : "eq"    },    "max_score" : 0.1519955,    "hits" : [      {        "_index" : "myindex",        "_type" : "_doc",        "_id" : "1",        "_score" : 0.1519955,        "_source" : {          "desc" : "我今天获奖了我很开心"        }      }    ]  }}
support.huaweicloud.com/usermanual-css/css_01_0036.html