@@ -788,15 +788,42 @@ protected function convertHitsToNodes(array $hits)
788788 */
789789 public function cacheLifetime (): int
790790 {
791- $ this ->request ->aggregation ('minHiddenBeforeDateTime ' , [
792- 'min ' => [
793- 'field ' => '_hiddenBeforeDateTime '
794- ]
795- ]);
791+ $ minTimestamps = array_filter ([
792+ $ this ->getNearestFutureDate ('_hiddenBeforeDateTime ' ),
793+ $ this ->getNearestFutureDate ('_hiddenAfterDateTime ' )
794+ ], function ($ value ) {
795+ return $ value != 0 ;
796+ });
797+
798+ if (empty ($ minTimestamps )) {
799+ return 0 ;
800+ }
801+
802+ $ minTimestamp = min ($ minTimestamps );
803+
804+ return $ minTimestamp - $ this ->now ->getTimestamp ();
805+ }
796806
797- $ this ->request ->aggregation ('minHiddenAfterDateTime ' , [
807+ /**
808+ * @param string $dateField
809+ * @return mixed
810+ * @throws QueryBuildingException
811+ * @throws \Flowpack\ElasticSearch\Exception
812+ */
813+ protected function getNearestFutureDate (string $ dateField )
814+ {
815+
816+ $ convertDateResultToTimestamp = function (array $ dateResult ): int {
817+ if (!isset ($ dateResult ['value_as_string ' ])) {
818+ return 0 ;
819+ }
820+ return (new \DateTime ($ dateResult ['value_as_string ' ]))->getTimestamp ();
821+ };
822+
823+ $ this ->request ->queryFilter ('range ' , [$ dateField => ['gt ' => 'now ' ]], 'must ' );
824+ $ this ->request ->aggregation ('min ' . $ dateField , [
798825 'min ' => [
799- 'field ' => ' _hiddenAfterDateTime '
826+ 'field ' => $ dateField
800827 ]
801828 ]);
802829
@@ -814,31 +841,10 @@ public function cacheLifetime(): int
814841 unset($ mustNot [1 ]);
815842
816843 $ requestArray = Arrays::setValueByPath ($ requestArray , 'query.bool.filter.bool.must_not ' , array_values ($ mustNot ));
817- $ response = $ this ->elasticSearchClient ->getIndex ()->request ('GET ' , '/_search ' , [], $ requestArray );
818844
819- $ result = $ response ->getTreatedContent ();
845+ $ result = $ this -> elasticSearchClient -> getIndex ()-> request ( ' GET ' , ' /_search ' , [], $ requestArray ) ->getTreatedContent ();
820846
821- $ convertDateResultToTimestamp = function (array $ dateResult ): int {
822- if (!isset ($ dateResult ['value_as_string ' ])) {
823- return 0 ;
824- }
825- return (new \DateTime ($ dateResult ['value_as_string ' ]))->getTimestamp ();
826- };
827-
828- $ minTimestamps = array_filter ([
829- $ convertDateResultToTimestamp (Arrays::getValueByPath ($ result , 'aggregations.minHiddenBeforeDateTime ' )),
830- $ convertDateResultToTimestamp (Arrays::getValueByPath ($ result , 'aggregations.minHiddenAfterDateTime ' ))
831- ], function ($ value , $ _ ) {
832- return $ value != 0 || $ value > $ this ->now ->getTimestamp ();
833- }, ARRAY_FILTER_USE_BOTH );
834-
835- if (empty ($ minTimestamps )) {
836- return 0 ;
837- }
838-
839- $ minTimestamp = min ($ minTimestamps );
840-
841- return $ minTimestamp - $ this ->now ->getTimestamp ();
847+ return $ convertDateResultToTimestamp (Arrays::getValueByPath ($ result , 'aggregations.min ' . $ dateField ));
842848 }
843849
844850 /**
0 commit comments