ElasticSearch full-text search engine

Source: Internet
Author: User
Provides various official and user-released code examples. For code reference, you are welcome to learn about the ElasticSearch full-text search engine. It is a good search framework! It is used for searching websites, which can relieve the pressure on the database!
What we brought to you before is the use of curl for implementation. If you are interested, please take a look.
Http://www.thinkphp1.cn/topic/29297.html
Today, we bring you the php api provided on the official website.
Integrate thinkphp3.2

Download the file and decompress it
ThinkPHP \ Library \ Vendor \ namespace Home\Controller;

use Think\Controller;

class EcController extends Controller
{

public function _initialize()
{
Vendor('Elasticsearch.autoload');
$params['hosts'] = array(
'127.0.0.1:9200'
);
$this->client = new \Elasticsearch\Client($params);
}

public function create_index()
{
$indexParams['index'] = 'my_index';
$indexParams['type'] = 'my_index';
$indexParams['body']['settings']['number_of_shards'] = 2;
$indexParams['body']['settings']['number_of_replicas'] = 0;
$this->client->create($indexParams);
}

public function add_document()
{
$params = array();
$params['body'] = array(
'testField' => 'dfdsfdsf'
);
$params['index'] = 'my_index';
$params['type'] = 'my_index';
$params['id'] = 'w1231313';
$ret = $this->client->index($params);
}

public function delete_index()
{
$deleteParams['index'] = 'my_index';
$this->client->indices()->delete($deleteParams);
}

public function delete_document()
{
$deleteParams = array();
$deleteParams['index'] = 'my_index';
$deleteParams['type'] = 'my_index';
$deleteParams['id'] = 'AU4Kmmj-WOmOrmyOj2qf';
$retDelete = $this->client->delete($deleteParams);
}
public function update_document()
{
$updateParams = array();
$updateParams['index'] = 'my_index';
$updateParams['type'] = 'my_index';
$updateParams['id'] = 'my_id';
$updateParams['body']['doc']['asas'] = '111111';
$response = $this->client->update($updateParams);

}
public function search()
{
$searchParams['index'] = 'my_index';
$searchParams['type'] = 'my_index';
$searchParams['from'] = 0;
$searchParams['size'] = 100;
$searchParams['sort'] = array(
'_score' => array(
'order' => 'desc'
)
);
// $searchParams['body']['query']['match']['testField'] = 'abc';
$retDoc = $this->client->search($searchParams);
print_r($retDoc);
}

public function get_document()
{
$getParams = array();
$getParams['index'] = 'my_index';
$getParams['type'] = 'my_index';
$getParams['id'] = 'AU4Kn-knWOmOrmyOj2qg';
$retDoc = $this->client->get($getParams);
print_r($retDoc);
}
}
?>

Elasticsearch.zip (1.53 MB download: 266 times)

AD: truly free, domain name + VM + enterprise mailbox = 0 RMB

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.