<commit_before><?php

/**
 * @package plugins.beacon
 * @subpackage api.filters
 */
class KalturaBeaconFilter extends KalturaBeaconBaseFilter
{
	/**
	 * @var KalturaBeaconIndexType
	 */
	public $indexTypeEqual;
	
	public function getCoreFilter()
	{
		return null;
	}
	
	public function getListResponse(KalturaFilterPager $pager)
	{
		$searchObject = $this->createSearchObject();
		$searchMgr = new kBeaconSearchQueryManger();
		
		$relatedObjectType = $this->relatedObjectTypeEqual;
		if(!$relatedObjectType)
		{
			$relatedObjectTypes = explode(",", $this->relatedObjectTypeIn);
			$relatedObjectType = $relatedObjectTypes[0];
		}
		
		$indexName = kBeacon::ELASTIC_BEACONS_INDEX_NAME;
		$indexType = null;
		if($relatedObjectType && $relatedObjectType != "") 
		{
			$indexName = kBeacon::$searchIndexNameByBeaconObjectType[$relatedObjectType];
			$indexType = kBeacon::$indexTypeByBeaconObjectType[$relatedObjectType];
		}
		
		$searchQuery = $searchMgr->buildSearchQuery($indexName, $indexType, $searchObject, $pager->pageSize, $pager->calcOffset());
		$elasticQueryResponse = $searchMgr->search($searchQuery);
		$responseArray = $searchMgr->getHitsFromElasticResponse($elasticQueryResponse);
		$totalCount = $searchMgr->getTotalCount($elasticQueryResponse);
		
		$response = new KalturaBeaconListResponse();
		$response->objects = KalturaBeaconArray::fromDbArray($responseArray);
		$response->totalCount = $totalCount;
		return $response;
	}
	
	protected function createSearchObject()
	{
		$searchObject = array();
		
		$searchObject[kESearchQueryManager::TERMS_KEY] = $this->getSearchTerms();
		$searchObject[kESearchQueryManager::RANGE_KEY] = $this->getSearchRangeTerms();
		$searchObject[kESearchQueryManager::ORDER_KEY] = $this->getOrderByObject();
		
		return $searchObject;
	}
	
	private function getSearchTerms()
	{
		$terms = array();
		
		$terms[kBeacon::FIELD_OBJECT_ID] = elasticSearchUtils::formatSearchTerm($this->objectIdIn);
		$terms[kBeacon::FIELD_EVENT_TYPE] = elasticSearchUtils::formatSearchTerm($this->eventTypeIn);
		$terms[kBeacon::FIELD_PARTNER_ID] = kCurrentContext::getCurrentPartnerId();
		
		if(isset($this->indexTypeEqual))
			$terms[kBeacon::FIELD_IS_LOG] = ($this->indexTypeEqual == KalturaBeaconIndexType::LOG) ? true : false; 
		
		return $terms;
	}
	
	private function getSearchRangeTerms()
	{
		$range = array();
		
		$range[kBeacon::FIELD_UPDATED_AT][kESearchQueryManager::GTE_KEY] = $this->updatedAtGreaterThanOrEqual;
		$range[kBeacon::FIELD_UPDATED_AT][kESearchQueryManager::LTE_KEY] = $this->updatedAtLessThanOrEqual;
		
		return $range;
	}
	
	private function getOrderByObject()
	{
		if (!$this->orderBy)
			return array();
		
		$orderObject = array();
		$orderByMap = $this->getOrderByMap();
		
		$order_arr = explode(",", $this->orderBy);
		foreach ($order_arr as $order) 
		{
			if (!$order || !isset($orderByMap[$order]))
				continue;
			
			$order = $orderByMap[$order];
			list ($field_name, $ascending) = baseObjectFilter::getFieldAndDirection($order);
			$orderObject[$field_name] = $ascending;
		}
		
		return $orderObject;
	}
}<commit_msg>NO-PLAT: change code to support elastic 6<commit_after><?php

/**
 * @package plugins.beacon
 * @subpackage api.filters
 */
class KalturaBeaconFilter extends KalturaBeaconBaseFilter
{
	/**
	 * @var KalturaBeaconIndexType
	 */
	public $indexTypeEqual;
	
	public function getCoreFilter()
	{
		return null;
	}
	
	public function getListResponse(KalturaFilterPager $pager)
	{
		$searchObject = $this->createSearchObject();
		$searchMgr = new kBeaconSearchQueryManger();
		
		$relatedObjectType = $this->relatedObjectTypeEqual;
		if(!$relatedObjectType)
		{
			$relatedObjectTypes = explode(",", $this->relatedObjectTypeIn);
			$relatedObjectType = $relatedObjectTypes[0];
		}
		
		$indexName = kBeacon::ELASTIC_BEACONS_INDEX_NAME;
		$indexType = null;
		if($relatedObjectType && $relatedObjectType != "") 
		{
			$indexName = kBeacon::$searchIndexNameByBeaconObjectType[$relatedObjectType];
			$indexType = kBeacon::$indexTypeByBeaconObjectType[$relatedObjectType];
		}
		
		$searchQuery = $searchMgr->buildSearchQuery($indexName, $indexType, $searchObject, $pager->pageSize, $pager->calcOffset());
		$elasticQueryResponse = $searchMgr->search($searchQuery);
		$responseArray = $searchMgr->getHitsFromElasticResponse($elasticQueryResponse);
		$totalCount = $searchMgr->getTotalCount($elasticQueryResponse);
		
		$response = new KalturaBeaconListResponse();
		$response->objects = KalturaBeaconArray::fromDbArray($responseArray);
		$response->totalCount = $totalCount;
		return $response;
	}
	
	protected function createSearchObject()
	{
		$searchObject = array();
		
		$searchObject[kESearchQueryManager::TERMS_KEY] = $this->getSearchTerms();
		$searchObject[kESearchQueryManager::RANGE_KEY] = $this->getSearchRangeTerms();
		$searchObject[kESearchQueryManager::ORDER_KEY] = $this->getOrderByObject();
		
		return $searchObject;
	}
	
	private function getSearchTerms()
	{
		$terms = array();
		
		$terms[kBeacon::FIELD_OBJECT_ID] = elasticSearchUtils::formatSearchTerm($this->objectIdIn);
		$terms[kBeacon::FIELD_EVENT_TYPE] = elasticSearchUtils::formatSearchTerm($this->eventTypeIn);
		$terms[kBeacon::FIELD_PARTNER_ID] = kCurrentContext::getCurrentPartnerId();
		
		if(isset($this->indexTypeEqual))
			$terms[kBeacon::FIELD_IS_LOG] = ($this->indexTypeEqual == KalturaBeaconIndexType::LOG) ? "true" : "false";
		
		return $terms;
	}
	
	private function getSearchRangeTerms()
	{
		$range = array();
		
		$range[kBeacon::FIELD_UPDATED_AT][kESearchQueryManager::GTE_KEY] = $this->updatedAtGreaterThanOrEqual;
		$range[kBeacon::FIELD_UPDATED_AT][kESearchQueryManager::LTE_KEY] = $this->updatedAtLessThanOrEqual;
		
		return $range;
	}
	
	private function getOrderByObject()
	{
		if (!$this->orderBy)
			return array();
		
		$orderObject = array();
		$orderByMap = $this->getOrderByMap();
		
		$order_arr = explode(",", $this->orderBy);
		foreach ($order_arr as $order) 
		{
			if (!$order || !isset($orderByMap[$order]))
				continue;
			
			$order = $orderByMap[$order];
			list ($field_name, $ascending) = baseObjectFilter::getFieldAndDirection($order);
			$orderObject[$field_name] = $ascending;
		}
		
		return $orderObject;
	}
}

{'fraction_non_alphanumeric': 0.14175506268081003, 'fraction_numerical': 0.00048216007714561236, 'mean_word_length': 5.914444444444444, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '251864', 'n_tokens_mistral': 2278, 'n_tokens_neox': 2024, 'n_words': 288}