elasticsearch的基本操作3


#[RequestMapping(path:'safeDeleteDoc',methods:'get')]
    public function safeDeleteDoc(ElasticsearchService $es)
    {
        //删除前先判断是否存在(推荐)
        $client = $es->getClient();
    
        $exists = $client->exists([
            'index' => 'novel',
            'id'    => '1'
        ]);
    
        if (!$exists) {
            return ['message' => '文档不存在'];
        }
    
        return $client->delete([
            'index' => 'novel',
            'id'    => '1'
        ]);
    }
    #[RequestMapping(path:'deleteByQuery',methods:'get')]
    public function deleteByQuery(ElasticsearchService $es)
    {
        //根据条件删除(_delete_by_query)
        //删除 name 包含“测试”的文档
        $client = $es->getClient();
    
        return $client->deleteByQuery([
            'index' => 'novel',
            'conflicts' => 'proceed',//生产环境建议(非常重要)条件删除一定要加 conflicts=proceed
            'body'  => [
                'query' => [
                    'match' => [
                        'name' => '测试'
                    ]
                ]
            ]
        ]);
        //删除 author = 某个作者
        $client->deleteByQuery([
            'index' => 'novel',
            'body'  => [
                'query' => [
                    'term' => [
                        'author' => '猫腻'
                    ]
                ]
            ]
        ]);
    }
    #[RequestMapping(path:'bulkDelete',methods:'get')]
    public function bulkDelete(ElasticsearchService $es)
    {
        //批量删除(多条 ID)
        //大量删除用 _delete_by_query,不要用循环
        /*不要用foreach ($ids as $id) {
            $client->delete([...]);
        }
        要用
        $client->bulk([...]);
        // 或
        $client->deleteByQuery([...]);
        */
        
        $client = $es->getClient();
    
        $ids = ['10', '11', '12'];
    
        $params = ['body' => []];
    
        foreach ($ids as $id) {
            $params['body'][] = [
                'delete' => [
                    '_index' => 'novel',
                    '_id'    => $id
                ]
            ];
        }
        //删除整个索引(⚠️ 不是删除文档)
        $client->indices()->delete([
            'index' => 'novel'
        ]);
        
        return $client->bulk($params);
        
    }

test2026-08-12 21:21


验证是否写入成功

$client->get([
    'index' => 'novel',
    'id'    => '1'
]);
或搜索

$client->search([
    'index' => 'novel',
    'body'  => [
        'query' => [
            'match' => [
                'name' => '斗破'
            ]
        ]
    ]
]);

test2026-08-12 21:22


#[RequestMapping(path:'searchquery',methods:'get')]
    public function searchquery(ElasticsearchService $es)
    {
        //搜索
        //支持 ik 分词
        //name 是 text + ik_max_word
        $client = $es->getClient();
        /*$rs = $client->search([
            'index' => 'novel',
            'body'  => [
                'query' => [
                    'match' => [
                        'name' => '悍刀行'
                    ]
                ]
            ]
        ]);*/
        //精确查询(keyword,不分词)author 是 keyword
        /*$rs = $client->search([
            'index' => 'novel',
            'body'  => [
                'query' => [
                    'term' => [
                        'author' => '萧鼎'
                    ]
                ]
            ]
        ]);
        */
        //多条件查询(bool 查询,重点)
        //作者 + 名称 + 热度
        /*$rs = $client->search([
            'index' => 'novel',
            'body' => [
                'query' => [
                    'bool' => [
                        'must' => [
                            ['term' => ['author' => '萧鼎']],
                            ['match' => ['name' => '诛仙']]
                        ],
                        'filter' => [
                            ['range' => ['count' => ['gte' => 800000]]]
                        ]
                    ]
                ]
            ]
        ]);*/
        //分页 + 排序(接口必用)
        /*$rs = $client->search([
            'index' => 'novel',
            'from'  => 0,   // 第几页
            'size'  => 10,  // 每页条数
            'body'  => [
                'sort' => [
                    ['count' => ['order' => 'desc']]
                ],
                'query' => [
                    'match' => [
                        'name' => '诛仙'
                    ]
                ]
            ]
        ]);*/
        //高亮搜索(前端最爱)
        /*$rs = $client->search([
            'index' => 'novel',
            'body'  => [
                'query' => [
                    'match' => [
                        'name' => '诛仙'
                    ]
                ],
                'highlight' => [
                    'fields' => [
                        'name' => new stdClass()
                    ],
                    'pre_tags'  => '<em>',
                    'post_tags' => '</em>'
                ]
            ]
        ]);
        return $rs['hits']['hits'];
        */
        //聚合查询(统计)
        //统计每个作者的小说数量
        $rs = $client->search([
            'index' => 'novel',
            'size'  => 0,
            'body'  => [
                'aggs' => [
                    'author_count' => [
                        'terms' => [
                            'field' => 'author'
                        ]
                    ]
                ]
            ]
        ]);
        return $rs;
    }
    #[RequestMapping(path:'search',methods:'get')]
    public function search(ElasticsearchService $es,RequestInterface $request,ResponseInterface $response)
    {
        //一个完整搜索接口(推荐你直接用)
        $keyword = $request->input('keyword', '');
        $page = (int)$request->input('page', 1);
        $size = (int)$request->input('size', 10);
    
        $from = ($page - 1) * $size;
    
        $client = $es->getClient();
    
        return $client->search([
            'index' => 'novel',
            'from'  => $from,
            'size'  => $size,
            'body'  => [
                'query' => [
                    'bool' => [
                        'must' => [
                            'multi_match' => [
                                'query'  => $keyword,
                                'fields' => ['name^3', 'descr']
                            ]
                        ]
                    ]
                ],
                'highlight' => [
                    'fields' => [
                        'name'  => new stdClass(),
                        'descr' => new stdClass()
                    ]
                ]
            ]
        ]);
    }

test2026-08-12 21:47


#term查询
POST /novel/_search
{
  "from": 0,
  "size": 5,
  "query": {
    "term": {
      "name": "鬼吹灯"
    }
  }

}

#terms查询
POST /novel/_search
{
  "query": {
    "terms": {
      "name": ["鬼吹灯","斗破苍穹"]
    }
  }
}



test2026-08-12 22:16



#match_all查询
POST /novel/_search
{
  "query": {
    "match_all": {}
  }
}

#match查询
POST /novel/_search
{
  "query": {
    "match": {
      "name": "鬼吹灯"
    }
  }
}

#布尔match查询
POST /novel/_search
{
  "query": {
    "match": {
      "name": {
        "query": "鬼吹灯 雪中",
        "operator": "or"
      }
    }
  }
}


#multi_match查询
POST /novel/_search
{
  "query": {
    "multi_match": {
      "query": "过吹灯",
      "fields": ["name","author"]
    }
  }
}



test2026-08-12 22:32