通过Lindorm Shell管理搜索索引

云原生多模数据库 Lindorm宽表支持搜索索引。在创建搜索索引并配置映射关联后,当您在宽表中写入数据,数据将自动同步至搜索引擎。您可以调用Elasticsearch API实现查询。本文介绍如何使用Lindorm Shell实现搜索索引的管理,以及结构化数据的实时检索查询功能。

前提条件

步骤一:创建宽

  1. 通过Lindorm Shell访问Lindorm宽表引擎

  2. Lindorm Shell中,通过HBase API在默认空间default下创建表名为testTable的宽表,并定义列族名为f1

    create 'testTable', 'f1'

步骤二:创建搜索索引

登录搜索引擎可视化用户界面,执行以下请求创建索引表democollection

PUT democollection
{
  "settings": {
    "index.number_of_shards": 4
  },
  "mappings": {
    "properties": {
      "name_s": {"type":"keyword"}
    }
  }
}

创建索引表时,若查询索引只需要获取数据id,不需要获取索引列原始值,可将代码mappings中的_source配置为false,以减少不必要的资源占用。示例如下:

PUT democollection
{
  "settings": {
    "index.number_of_shards": 4
  },
  "mappings": {
    "_source": {
      "enabled": false
    },
    "properties": {
      "name_s": {
        "type": "keyword"
      }
    }
  }
}

步骤三:映射宽表列和索引列

定义宽表列和搜索索引列的映射关系,确保宽表列的数据能正确同步到对应的搜索索引列,以及通过搜索索引查询出的数据符合预期。本文示例将宽表f1:name列映射到索引表的name_s列。

  1. Lindorm Shell解压文件的bin目录中创建JSON文件(文件名为schema),将以下代码复制至JSON文件中。

    {
      "sourceNamespace": "default",
      "sourceTable": "testTable",
      "targetIndexName": "democollection",
      "indexType": "ES",
      "rowkeyFormatterType": "STRING",
      "fields": [
        {
          "source": "f1:name",
          "targetField": "name_s",
          "type": "STRING"
        }
      ]
    }
    说明
    • 请确保列映射中的宽表、索引表的列名和数据类型与创建宽表、索引表时定义的列名和数据类型一致。

    • JSON文件中的参数说明请参见配置列映射

  2. 使用Lindorm Shell执行以下命令完成宽表列和索引表列的映射关系。

    alter_external_index 'testTable', 'schema.json'
    说明

    关于宽表列和索引表列的映射管理操作请参见管理列映射

步骤四:向宽表写入数据

在表testTable中写入一条数据。

put 'testTable', 'row1', 'f1:name', 'foo'
说明

宽表和索引表的列映射关系配置完成后,实时写入宽表中的数据将自动同步到索引表中。对于宽表中的历史数据,需要手动执行全量构建索引才可以完成数据同步,具体操作请参见构建全量数据索引

步骤五:使用搜索索引查询

  1. 查询索引数据。

    通过搜索引擎可视化用户界面,使用Elasticsearch API 查询索引数据,获取搜索索引表的主键ID。

    GET /democollection/_search
    {
      "size": 10,
      "query": {
        "match": {
          "name_s": "foo"
        }
      }
    }

    返回结果:

    {
      "took": 3,
      "timed_out": false,
      "_shards": {
        "total": 4,
        "successful": 4,
        "skipped": 0,
        "failed": 0
      },
      "hits": {
        "total": {
          "value": 1,
          "relation": "eq"
        },
        "max_score": 0.2876821,
        "hits": [
          {
            "_index": "democollection",
            "_id": "row1",
            "_score": 0.2876821,
            "_source": {
              "name_s": "foo",
              "update_version_l": 1745397126559
            }
          }
        ]
      }
    }

    从返回结果中获取搜索索引表的主键ID(_id)。

  1. 使用Lindorm Shell执行以下命令查询宽表数据。

    get 'testTable','row1'

    返回结果如下:

    COLUMN                                         CELL
     f1:name                                        timestamp=1745397126559, value=foo
    1 row(s)