elastic / elasticsearch-php

Official PHP client for Elasticsearch.
https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/index.html
MIT License
22 stars 971 forks source link

How to use #853

Closed titoff002 closed 5 years ago

titoff002 commented 5 years ago

Hello,

Question : how to use this elasticsearch tag "script": with elasticsearch- php ?

POST /index/type/_search
{
  "query": {
     "filtered": {
       "filter": {
         "script": {
           "script": "foreach(attr :  _source['attributes']) {if ( attr['key']=='age') { return attr['value'] > ageValue;} } return false;",
           "params" : {
                "ageValue"  : 23
            }

         }
       },
       "query": {
         "match_all": {}
       }
     }
  }
}
sn01615 commented 5 years ago
        $q = '';
        if ($wd)
            $q .= sprintf(' +(title:(%s) tags:(%s)^0.2 recommend_text:(%s)^0.2)', $wd, $wd, $wd); // search
        if ($tag)
            $q = sprintf('+tags:("%s")', $tag); // full match
        if ($sexuality > 0)
            $q .= sprintf(' +sexuality:%s', $sexuality); // number match
        if ($length) {// range math
            if ($length == '*-20')
                $q .= sprintf(' +length:[* TO 200000]');
        }
        $params = [
            'index' => 'novel',
            'type' => 'novel',
            "q" => $q,
        ];
        // order
        if ($order == 'update') {
            $params['body']['sort'] = ['novel_updated_time' => 'desc',];
        } elseif ($order == 'collect') {
            $params['body']['sort'] = ['collect_times' => 'desc',];
        } elseif ($order == 'hot') {
            $params['body']['sort'] = ['read_times' => 'desc',];
        }
        if ($size > 0) {
            $params['size'] = $size;
        }
        if ($page > 1) {
            $params['from'] = max(0, ($page - 1) * $size);
        }

        $response = $client->search($params);
        $hits = $response['hits'] ?? [];
        $total = $hits['total']['value'] ?? 0;
        $list = [];
        foreach ($hits['hits'] ?? [] as $hit) {
            $list[] = $hit['_source'];
        }

        return [$total, $list];