文档

通过curl命令连接并使用搜索引擎

更新时间:

Lindorm搜索引擎提供了一系列兼容Elasticsearch RESTful风格的API,您可以通过curl命令调用这些API,管理搜索引擎中的搜索索引及文档。

前提条件

连接搜索引擎

执行以下命令,连接搜索引擎,查看集群中的索引信息。

curl -XGET "http://<url>/_cat/indices?v" -u <username>:<password>  

参数说明

参数

说明

url

搜索引擎(Elasticsearch兼容版本)的Elasticsearch兼容连接地址。如何获取,请参见Elasticsearch兼容版本

重要
  • 如果应用部署在ECS实例,建议您通过专有网络访问Lindorm实例,可获得更高的安全性和更低的网络延迟。

  • 如果应用部署在本地,在通过公网连接Lindorm实例前,需在控制台开通公网地址。开通方式:在控制台的左侧导航栏,选择数据库连接,单击搜索引擎页签,在页签右上角单击开通公网地址

  • 通过专有网络访问Lindorm实例,url请填写Elasticsearch兼容地址对应的专有网络地址。通过公网访问Lindorm实例,url请填写Elasticsearch兼容地址对应的公网地址。

username

访问搜索引擎(Elasticsearch兼容版本)的用户名和密码。

默认用户名和密码的获取方式:在控制台的左侧导航栏,选择数据库连接,单击搜索引擎页签,在搜索引擎页签可获取。

password

连接示例如下:

curl -XGET "http://ld-t4n5668xk31ui****-proxy-search-public.lindorm.rds.aliyuncs.com:30070/_cat/indices?v" -u <username>:<password>

连接成功后将显示如下结果,因还未创建索引,结果中不会显示任何具体的索引信息:

health status index uuid pri rep docs.count docs.deleted store.size pri.store.size

使用搜索引擎

以下为常用的curl命令。更多命令,请参见Elasticsearch官方文档

管理索引

  • 创建索引。

    在搜索引擎中创建索引lindorm_search。

    curl -XPUT "http://ld-t4n5668xk31ui****-proxy-search-public.lindorm.rds.aliyuncs.com:30070/lindorm_search" -u <username>:<password>

    创建成功将返回如下结果:

    {"acknowledged":true,"shards_acknowledged":true,"index":"lindorm_search"}
  • 设置索引结构。

    设置索引lindorm_search的结构为_mapping,类型为_doc,包含idnamedescribe字段。

    curl -XPUT "http://ld-t4n5668xk31ui****-proxy-search-public.lindorm.rds.aliyuncs.com:30070/lindorm_search/_doc/_mapping" -u <username>:<pasword> -H 'Content-Type: application/json' -d '
    {        
     "_doc":{
       "properties": {
            "id": {"type": "long"},
            "name":{"type":"keyword"},
            "describe": {"type": "text"}
          }
      }
    }'

    设置成功后将返回如下结果:

    {"_index":"lindorm_search","_type":"_doc","_id":"_mapping","_version":1,"result":"created","_shards":{"total":1,"successful":1,"failed":0},"_seq_no":0,"_primary_term":1}
  • 查看实例下的索引。

    curl  -XGET "http://ld-t4n5668xk31ui****-proxy-search-public.lindorm.rds.aliyuncs.com:30070/_cat/indices?v" -u <username>:<pasword>

    返回结果如下:

    health status index          uuid           pri rep docs.count docs.deleted store.size pri.store.size
    green  open   lindorm_search lindorm_search   1   0          0            0       208b           208b

    如果当前实例下无索引,则返回结果中无具体索引信息。

  • 删除索引。

    curl -XDELETE "http://ld-t4n5668xk31ui****-proxy-search-public.lindorm.rds.aliyuncs.com:30070/lindorm_search" -u <username>:<password>

    删除成功将返回如下结果:

    {"acknowledged":true}

管理文档

  • 创建单个文档。

    在索引lindorm_search中,创建ID为1的文档。

    curl -XPOST "http://ld-t4n5668xk31ui****-proxy-search-public.lindorm.rds.aliyuncs.com:30070/lindorm_search/_doc/1" -u <username>:<password> -H 'Content-Type: application/json' -d '
    { 
    "id":100,
    "name":"shenzhen",
    "describe":"just a test"
    }'
  • 创建多个文档。

    在索引lindorm_search中,创建ID为1和2的两个文档。

    curl -XPOST "http://ld-t4n5668xk31ui****-proxy-search-public.lindorm.rds.aliyuncs.com:30070/_bulk" -u <username>:<password> -H 'Content-Type: application/json' -d'
    { "index" : { "_index": "lindorm_search", "_type" : "_doc", "_id" : "1" } }
    {"id":200,"name":"shanghai","describe":"just"}
    { "index" : { "_index": "lindorm_search", "_type" : "_doc", "_id" : "2" } }
    {"id":300,"name":"beijing","describe":"dood luck"}
    '
  • 搜索文档。

    搜索ID为1的文档。

    curl  -XGET "http://ld-t4n5668xk31ui****-proxy-search-public.lindorm.rds.aliyuncs.com:30070/lindorm_search/_doc/1?pretty" -u <username>:<password>

    返回结果如下:

    {
      "_index" : "lindorm_search",
      "_id" : "1",
      "_version" : 1,
      "_seq_no" : 0,
      "_primary_term" : 1,
      "found" : true,
      "_source" : {
        "id" : 100,
        "name" : "shenzhen",
        "describe" : "just a test"
      }
    }

  • 本页导读 (1)
文档反馈