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


#[RequestMapping(path:'addindex',methods:'get')]
    public function addindex(ElasticsearchService $es){
        //添加索引
        $es->getClient()->indices()->create([
            'index' => 'book2',
            'body' => [
                'settings' => [
                    'analysis' => [
                        'analyzer' => [
                            'ik' => [
                                'type' => 'ik_max_word'
                            ]
                        ]
                    ]
                ],
                'mappings' => [
                    'properties' => [
                        'name' => [
                            'type' => 'text',
                            'analyzer' => 'ik_max_word'
                        ],
                        'author' => [
                            'type' => 'keyword'
                        ],
                        'count' => [
                            'type' => 'long'
                        ]
                    ]
                ]
            ]
        ]);
        
    }

test2026-08-12 21:01


#[RequestMapping(path:'addDoc',methods:'get')]
    public function addDoc(ElasticsearchService $es)
    {
        //添加文档
        $client = $es->getClient();
    
        $response = $client->index([
            'index' => 'novel',
            'id'    => '1',
            'body'  => [
                'name'   => '斗破苍穹',
                'author' => '天蚕土豆',
                'descr'  => '少年萧炎在斗气大陆的成长故事',
                'count'  => 1234567,
                'onsale' => '2020-01-01 10:00:00'
            ]
        ]);
    
        return $response;
    }

test2026-08-12 21:04


自动生成 ID(不传 id)

$client->index([
    'index' => 'novel',
    'body'  => [
        'name'   => '全职高手',
        'author' => '蝴蝶蓝',
        'count'  => 500000
    ]
]);

test2026-08-12 21:05


#[RequestMapping(path:'bulkAdd',methods:'get')]
    public function bulkAdd(ElasticsearchService $es)
    {
        //批量添加文档
        $client = $es->getClient();
    
        $params = ['body' => []];
    
        $data = [
            ['name' => '鬼吹灯', 'author' => '天下霸唱', 'count' => 800000],
            ['name' => '诛仙', 'author' => '萧鼎', 'count' => 1200000],
            ['name' => '雪中悍刀行', 'author' => '烽火戏诸侯', 'count' => 950000],
        ];
    
        foreach ($data as $i => $doc) {
            $params['body'][] = [
                'index' => [
                    '_index' => 'novel',
                    '_id'    => $i + 2
                ]
            ];
            $params['body'][] = $doc;
        }
    
        return $client->bulk($params);
    }

test2026-08-12 21:07


#[RequestMapping(path:'safeAdd',methods:'get')]
    public function safeAdd(ElasticsearchService $es)
    {
        //判断索引是否存在,不存在先建
        $client = $es->getClient();
    
        if (!$client->indices()->exists(['index' => 'novel'])) {
            // 建索引(你前面的代码)
        }
    
        return $client->index([
            'index' => 'novel',
            'id'    => '100',
            'body'  => [
                'name' => '测试小说',
                'count' => 1
            ]
        ]);
    }

test2026-08-12 21:09