Elasticsearch的PHP客戶端操作

w39發表於2021-09-09

叢集設定
$hosts = [
    '127.0.0.1:9200'
];
$client = ElasticsearchClientBuilder::create()->setHosts($hosts)->build();

//建立文件
$params = [
    'index' => 'my_index',
    'type' => 'my_type',
    'id' => 'my_id',
    'body' => ['testField' => 'abc']
];
$response = $client->index($params);

//獲取文件
$params = [
    'index' => 'my_index',
    'type' => 'my_type',
    'id' => 'my_id'
];
$response = $client->get($params);

//查詢文件
$params = [
    'index' => 'my_index',
    'type' => 'my_type',
    'body' => [
        'query' => [
            'match' => [
                'testField' => 'abc'
            ]
        ]
    ]
];
$response = $client->search($params);

//刪除文件
$params = [
    'index' => 'my_index',
    'type' => 'my_type',
    'id' => 'my_id'
];
$response = $client->delete($params);

//刪除索引
$deleteParams = [
    'index' => 'my_index'
];
$response = $client->indices()->delete($deleteParams);

//建立索引
$params = [
    'index' => 'my_index',
    'body' => [
        'settings' => [
            'number_of_shards' => 2,
            'number_of_replicas' => 0
        ]
    ]
];
$response = $client->indices()->create($params);

//忽略異常
$params = [
    'index'  => 'test_missing',
    'type'   => 'test',
    'id'     => 1,
    'client' => [ 'ignore' => [400, 404] ]
];
$response = $client->get($params);

//獲取響應詳細資訊
$params = [
    'index' => 'my_index',
    'type' => 'my_type',
    'id' => 1,
    'client' => [
        'verbose' => true,
        'ignore' => [400, 404]
    ]
];
$response = $client->get($params);

//啟用未來模式
$params = [
    'index' => 'my_index',
    'type' => 'my_type',
    'id' => 'my_id',
    'client' => [
        'future' => 'lazy'
    ]
];
$future = $client->get($params);
$response = $future->wait();

//空物件
$params = [
    'index' => 'megacorp',
    'type' => 'employee',
    'body' => [
        'query' => [
            'match_phrase' => [
                'about' => 'rock climbing'
            ]
        ],
        'highlight' => [
            'fields' => [
                ['about' => new stdClass()]
            ]
        ]
    ]
];
$response = $client->search($params);

$params = [
    'index' => 'my_index',
    'body' => [
        'mappings' => [
            'my_type' => [
                "include_in_all" => false,
                'properties' => [
                    'name' => [
                        'type' => 'string',
                        'analyzer' => 'ik'
                    ],
                    'price_now' => [
                        'type' => 'double',
                        'index' => 'not_analyzed'
                    ],
                    'image_thumb' => [
                        'type' => 'string',
                        'index' => 'not_analyzed'
                    ],
                    'sales_total' => [
                        'type' => 'integer',
                        'index' => 'not_analyzed'
                    ],
                    'comment' => [
                        'type' => 'integer',
                        'index' => 'not_analyzed'
                    ],
                    'create_date' => [
                        'type' => 'string',
                        'index' => 'not_analyzed'
                    ],
                    'status' => [
                        'type' => 'byte',
                        'index' => 'no'
                    ],
                    'sale' => [
                        'type' => 'byte',
                        'index' => 'no'
                    ]
                ]
            ]
        ]
    ]
];
$response = $client->indices()->create($params);

print_r($response);


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/854/viewspace-2801512/,如需轉載,請註明出處,否則將追究法律責任。

相關文章