转载

php es Elasticsearch 索引增删改查

温馨提示:
本文最后更新于 2024年09月07日,已超过 38 天没有更新。若文章内的图片失效(无法正常加载),请留言反馈或直接联系我

php es Elasticsearch 索引增删改查


新增索引

 
$params = [
    'index' => 'products'
];
// 创建一个简单的索引
try {
    $response = app('es')->indices()->create($params);
    print_r($response);
} catch (\Exception $e) {
    echo 'error: ' . $e->getMessage();
}

查看索引

 
$params = [
    'index' => 'products'
];
// 创建一个简单的索引
try {
    $response = app('es')->indices()->get($params);
    print_r($response);
} catch (\Exception $e) {
    echo 'error: ' . $e->getMessage();
}

删除索引

 
$params = [
    'index' => 'products'
];
// 创建一个简单的索引
try {
    $response = app('es')->indices()->delete($params);
    print_r($response);
} catch (\Exception $e) {
    echo 'error: ' . $e->getMessage();
}

索引添加类型字段

 
$params = [
            'index' => 'products',
            'body' => [
                "properties" => [
                    "type" => ["type" => "keyword"],
                    "product_core" => ["type" => "text"],
                    "title" => ["type" => "text", "analyzer" => "ik_smart"],
                    "bar_code" => ["type" => "text"],
                    "category" => ["type" => "keyword"],
                    "category_path" => ["type" => "keyword"],
                    "status" => ["type" => "boolean"],
                    "audit_status" => ["type" => "integer"],
                    "shop_name" => ["type" => "text", "analyzer" => "ik_smart"],
                    "description" => ["type" => "text", "analyzer" => "ik_smart"],
                    "rating" => ["type" => "float"],
                    "sold_count" => ["type" => "integer"],
                    "review_count" => ["type" => "integer"],
                    "price" => ["type" => "scaled_float", "scaling_factor" => 100],
                    "image" => ["type" => "text"],
                    "skus" => [
                        "type" => "nested",
                        "properties" => [
                            "title" => ["type" => "text", "analyzer" => "ik_smart"],
                            "description" => ["type" => "text", "analyzer" => "ik_smart"],
                            "price" => ["type" => "scaled_float", "scaling_factor" => 100]
                        ]
                    ],
                    "properties" => [
                        "type" => "nested",
                        "properties" => [
                            "name" => ["type" => "keyword"],
                            "value" => ["type" => "keyword"]
                        ]
                    ]
                ]
            ]
        ];
// 创建一个简单的索引
try {
    $response = app('es')->indices()->putMapping($params);
    print_r($response);
} catch (\Exception $e) {
    echo 'error: ' . $e->getMessage();
}
正文到此结束
该篇文章的评论功能已被站长关闭