You need to enable JavaScript to run this app.
导航
Enhancements 插件使用说明
最近更新时间:2024.12.05 12:37:16首次发布时间:2024.12.05 12:37:16

opensearch-enhancements/opendistro-enhancements 是云搜索服务的一个系统内置插件。

  • opensearch-enhancements 包含 NGram-Type 字段类型,在模糊搜索的场景中推荐使用 Ngram 通配符查询,可以在字符串的任意位置查询到匹配项,使用方式可参考使用 NGram-Type 插件模糊查询
  • opendistro-enhancements 包含 Flat_Object 字段类型,Flat_Object 的优势是针对 json 类型的字段时无需为每个字段定义类型,这样既避免了元数据的膨胀,又简化了用户的配置。本文主要介绍 Flat_Object 的简单使用方式。

适用版本

目前支持 ES 7.10.2、OpenSearch 2.9.0 版本。

  • ES 7.10.2 版本插件名称为 opendistro-enhancements。
  • OpenSearch 2.9.0 版本插件名称为 opensearch-enhancements。

前提条件

Enhancements 插件默认未安装,如果需要使用,请提前自行安装。如何安装,请参见安装系统内置插件

步骤一:设置 mappings

PUT my-index
{
   "mappings": {
      "properties": {
         "issue": {
            "properties": {
               "labels": {
                  "type": "flat_object"
               }
            }
         }
      }
   }
}
  • labels 中的 type:必填,设置字段类型为 flat_object

步骤二:写入文档

PUT my-index/_doc/2
{
   "issue": {
      "number": 456,
      "labels": {
         "author": "Liu",
         "version": "2.1",
         "backport": [
            "2.0",
            "1.3"
         ],
         "category": {
            "type": "API",
            "level": "enhancement"
         },
         "createdDate": "2023-02-01",
         "comment": [
            [
               "Mike",
               "LGTM"
            ],
            [
               "John",
               "Approved"
            ]
         ],
         "views": 3333,
         "priority": 1.5
      }
   }
}

步骤三:查询文档

match + 带路径查询

POST my-index/_search
{
   "_source": true,
   "query": {
      "match": {
         "issue.labels.version": "2.1"
      }
   }
}

查询成功时,返回如下类似信息:

{
   "took": 5251,
   "timed_out": false,
   "_shards": {
      "total": 1,
      "successful": 1,
      "skipped": 0,
      "failed": 0
   },
   "hits": {
      "total": {
         "value": 1,
         "relation": "eq"
      },
      "max_score": 0.46223074,
      "hits": [
         {
            "_index": "my-index",
            "_id": "2",
            "_score": 0.46223074,
            "_source": {
               "issue": {
                  "number": 456,
                  "labels": {
                     "author": "Liu",
                     "version": "2.1",
                     "backport": [
                        "2.0",
                        "1.3"
                     ],
                     "category": {
                        "type": "API",
                        "level": "enhancement"
                     },
                     "createdDate": "2023-02-01",
                     "comment": [
                        [
                           "Mike",
                           "LGTM"
                        ],
                        [
                           "John",
                           "Approved"
                        ]
                     ],
                     "views": 3333,
                     "priority": 1.5
                  }
               }
            }
         }
      ]
   }
}

match + 不带路径查询

POST my-index/_search
{
   "_source": true,
   "query": {
      "match": {
         "issue.labels": "2.1"
      }
   }
}

查询成功时,返回如下类似信息:

{
   "took": 117,
   "timed_out": false,
   "_shards": {
      "total": 1,
      "successful": 1,
      "skipped": 0,
      "failed": 0
   },
   "hits": {
      "total": {
         "value": 1,
         "relation": "eq"
      },
      "max_score": 0.46223074,
      "hits": [
         {
            "_index": "my-index",
            "_id": "2",
            "_score": 0.46223074,
            "_source": {
               "issue": {
                  "number": 456,
                  "labels": {
                     "author": "Liu",
                     "version": "2.1",
                     "backport": [
                        "2.0",
                        "1.3"
                     ],
                     "category": {
                        "type": "API",
                        "level": "enhancement"
                     },
                     "createdDate": "2023-02-01",
                     "comment": [
                        [
                           "Mike",
                           "LGTM"
                        ],
                        [
                           "John",
                           "Approved"
                        ]
                     ],
                     "views": 3333,
                     "priority": 1.5
                  }
               }
            }
         }
      ]
   }
}