简体   繁体   中英

Official Elasticsearch PHP Module enable TTL

I know how i could activate the ttl feature with curl in php, but i wonder if the offical Elasticsearch PHP Library ( https://github.com/elasticsearch/elasticsearch-php ) does this feature support as well. I've already dig trough the code of the Lib but was not able to figure it out.

Thanks for your help!

I've created my own method to enable ttl feature on a index type. I've also filed an issue on the official Github Repo: https://github.com/elasticsearch/elasticsearch-php/issues/62

class ElasticSearchClient extends \Elasticsearch\Client {
  public function enableTtl($params) {
    $index = $this->extractArgument($params, 'index');
    $type = $this->extractArgument($params, 'type');
    $body = json_encode(array($type => array('_ttl' => array('enabled' => true))));
    /** @var callback $endpointBuilder */
    $endpointBuilder = $this->dicEndpoints;

    /** @var \Elasticsearch\Endpoints\Update $endpoint */
    $endpoint = $endpointBuilder('Indices\Mapping\Put');
    $endpoint->setIndex($index)
        ->setType($type)
        ->setBody($body);
    $endpoint->setParams($params);
    $response = $endpoint->performRequest();
    return $response['data'];
  }
} 

Well, IMHO you need to update the elasticsearch mapping using the putMapping() method. No need for a special method call.

Here is an example that worked in our system.

    $params['index'] = 'yourindexname';
    $params['type'] = 'yourtypename';

    $mapping = [
        '_ttl' => [
            'enabled' => 'true',
            'default' => '14d',
        ],
        'properties' => [
            [... add your properties here ...]
        ]
    ];

    $params['body']['yourtypename'] = $mapping;
    $this->getClient()->indices()->putMapping($params);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM