代码之家  ›  专栏  ›  技术社区  ›  Thomas Spycher

官方Elasticsearch PHP模块启用TTL

  •  2
  • Thomas Spycher  · 技术社区  · 11 年前

    我知道如何在php中使用curl激活ttl功能,但我想知道官方的Elasticsearch php库( https://github.com/elasticsearch/elasticsearch-php )也支持此功能。我已经深挖了Lib的代码,但没能弄清楚。

    谢谢你的帮助!

    2 回复  |  直到 11 年前
        1
  •  0
  •   Thomas Spycher    10 年前

    我已经创建了自己的方法来在索引类型上启用ttl功能。我还在官方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'];
      }
    } 
    
        2
  •  0
  •   Paul Weber    9 年前

    嗯,IMHO需要使用putMapping()方法更新弹性搜索映射。不需要特殊的方法调用。

    下面是一个在我们的系统中运行的示例。

        $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);