elasticsearch--php检索

来源:互联网 发布:写钢琴谱的软件 编辑:程序博客网 时间:2024/06/05 18:50

在搭好ES的环境之后,就可以使用他来检索了。这个时候是默认已经装好elasticsearch-php的扩展了。

require_once ('../vendor/autoload.php') ;use \ElasticSearch\Client;

在用之前首先要做好准备↑。

和其他的检索一样,首先就是要创建连接。

$es = Client::connection(array(    'servers' => '127.0.0.1:9200',    'protocol' => 'http',    'index' => 'ht_email_index',    'type' => 'Email'));

service是es服务器的地址然后协议、索引名。type,相当于选数据库和表。

接下来构造检索条件。

es的检索条件可以用数组来表示。

$query = array();if (!empty($subject)) {  $query[]= array('match' => array('subject' => $subject));}if (!empty($cc)) {  $query[] = array('match' => array('cc_user' => $cc));}

如果是多条件检索,就将条件追加到$query后面。

接下来写检索语句:

 $search = array("query" =>                array("bool" =>                  array("must" => $query )));
因为是多条件检索,所以这里用到bool查询,must表示与。如果条件之间是或的关系,那么要用should。

在这里再说一下范围查询。

比如要查询某一时间段内的数据,那么需要对查询结果进行过滤,像这样:

$search = array("query" =>              array("filtered" =>                 array(                          "query" =>                           array("bool" =>                            array("must" => $query )),                          "filter" =>                           array("range" =>                             array("date" =>                              array(                                    "gt" => $date_from,                                    "lt" => $date_to                                  )))              )));

用到filter、range。需要说明一点的是,像日期字段(string),要映射为"not_analyzed"才能检索出准确的结果。

因为es检索出的结果写在hits里,hits默认显示检索出的前10条结果,因此要来设置翻页来宣誓全部。

<pre name="code" class="php">$pagesize = 10; //每页显示的数据条数$page = isset($_GET['page'])?intval($_GET['page']):1;  //获取页数信息$offset = ($page - 1) * $pagesize; $params =  $search;$params['size'] = $pagesize;$params['from'] = $offset;$result = $es->search($params);


构造好检索语句后,最后一句便是执行查询。

举个栗子吧,比如一检索结果是这样的:

{   "took":      6,   "timed_out": false,   "_shards": { ... },   "hits": {      "total":      3,      "max_score":  1,      "hits": [         {            "_index":         "megacorp",            "_type":          "employee",            "_id":            "3",            "_score":         1,            "_source": {               "first_name":  "Douglas",               "last_name":   "Fir",               "age":         35,               "about":       "I like to build cabinets",               "interests": [ "forestry" ]            }         },         {            "_index":         "megacorp",            "_type":          "employee",            "_id":            "1",            "_score":         1,            "_source": {               "first_name":  "John",               "last_name":   "Smith",               "age":         25,               "about":       "I love to go rock climbing",               "interests": [ "sports", "music" ]            }         },         {            "_index":         "megacorp",            "_type":          "employee",            "_id":            "2",            "_score":         1,            "_source": {               "first_name":  "Jane",               "last_name":   "Smith",               "age":         32,               "about":       "I like to collect rock albums",               "interests": [ "music" ]            }         }      ]   }}
在检索结果中取出我们要用的。
<pre name="code" class="php">$hits   = $result["hits"]["hits"];
然后再需要什么拿什么就好了。


一个简单的php检索就是这样。其中歌颂参数都可以根据自己的需要变换,具体的请参照官方文档。

或者中文权威指南。





0 0
原创粉丝点击