elasticsearch的基本操作2


#创建索引,指定数据结构
PUT /novel
{
  "settings": {
    "analysis": {
      "analyzer": {
        "ik": {
          "type": "ik_max_word"
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "name": {
        "type": "text",
        "analyzer": "ik_max_word"
      },
      "author": {
        "type": "keyword"
      },
      "descr": {
        "type": "text",
        "analyzer": "ik_max_word"
      },
      "count": {
        "type": "long"
      },
      "onsale": {
        "type": "date",
        "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
      }
    }
  }
}



#添加文档,自动生成id
POST /novel/_doc
{
  "name": "盘龙",
  "author": "我是西红柿",
  "count": 100000,
  "onsale": "2021-01-01",
  "descr": "嘻嘻哈哈嘻嘻哈啊信息哈哈"
}

#添加文档,设置id
POST /novel/_doc/2
{
  "name": "盘龙古",
  "author": "我是西红柿",
  "count": 100000,
  "onsale": "2021-01-01",
  "descr": "嘻嘻哈哈嘻嘻哈啊信息哈哈"
}



test2026-08-12 18:56


test2026-08-12 18:57


#修改文档,基于doc方式
POST /novel/_update/1
{
  "doc": {
    "count": 123456788
  }
}

test2026-08-12 19:00


#根据id删除文档
DELETE /novel/_doc/bsmc9Z8BAVE9hxbCin6d

test2026-08-12 19:18


PHP hyperf创建ES索引

<?php

declare(strict_types=1);

namespace AppController;

use HyperfElasticsearchClientBuilderFactory;
#[Controller]
class UserController
{
    private ClientBuilderFactory $factory;

    public function __construct(ClientBuilderFactory $factory)
    {
        $this->factory = $factory;
    }
    #[ReqestMapping(path:'list13',methods:'get')]
    public function list13()
    {
        $client = $this->factory
            ->create()
            ->setHosts(['http://127.0.0.1:9200'])
            ->build();

        $exists = $client->indices()->exists(['index' => 'novel']);

        return [
            'exists' => $exists,
        ];
    }
}

test2026-08-12 19:55


test2026-08-12 20:05