<commit_before><?php
/**
 * @package plugins.elasticSearch
 * @subpackage model.items
 */
abstract class ESearchNestedObjectItem extends ESearchItem
{

	const DEFAULT_INNER_HITS_SIZE = 10;

	protected static function initializeInnerHitsSize($queryAttributes)
	{
		$overrideInnerHitsSize = $queryAttributes->getOverrideInnerHitsSize();
		if($overrideInnerHitsSize)
			return $overrideInnerHitsSize;

		$innerHitsConfig = kConf::get('innerHits', 'elastic');
		$innerHitsConfigKey = static::INNER_HITS_CONFIG_KEY;
		$innerHitsSize = isset($innerHitsConfig[$innerHitsConfigKey]) ? $innerHitsConfig[$innerHitsConfigKey] : self::DEFAULT_INNER_HITS_SIZE;

		return $innerHitsSize;
	}

	protected static function createNestedQueryForItems($eSearchItemsArr, $boolOperator, &$queryAttributes)
	{
		$finalQuery = array();
		$innerHitsSize = self::initializeInnerHitsSize($queryAttributes);
		$allowedSearchTypes = static::getAllowedSearchTypesForField();
		
		//don't group to a single query if the operator is AND
		if($boolOperator == 'must')
		{
			foreach ($eSearchItemsArr as $eSearchItem)
			{
				$nestedQuery = null;
				$nestedQuery['nested']['path'] = static::NESTED_QUERY_PATH;
				$nestedQuery['nested']['inner_hits'] = array('size' => $innerHitsSize, '_source' => true);
				$queryAttributes->setScopeToInner();
				static::createSingleItemSearchQuery($eSearchItem, $boolOperator, $nestedQuery, $allowedSearchTypes, $queryAttributes);
				$highlight = kBaseSearch::getHighlightSection(static::HIGHLIGHT_CONFIG_KEY, $queryAttributes);
				if(isset($highlight))
					$nestedQuery['nested']['inner_hits']['highlight'] = $highlight;
				$finalQuery[] = $nestedQuery;
			}
		}
		else
		{
			$nestedQuery['nested']['path'] = static::NESTED_QUERY_PATH;;
			$nestedQuery['nested']['inner_hits'] = array('size' => $innerHitsSize, '_source' => true);
			$queryAttributes->setScopeToInner();
			foreach ($eSearchItemsArr as $eSearchItem)
			{
				static::createSingleItemSearchQuery($eSearchItem, $boolOperator, $nestedQuery, $allowedSearchTypes, $queryAttributes);
			}
			$highlight = kBaseSearch::getHighlightSection(static::HIGHLIGHT_CONFIG_KEY, $queryAttributes);
			if(isset($highlight))
				$nestedQuery['nested']['inner_hits']['highlight'] = $highlight;<commit_msg>PLAT-7994: avoid setting must_not twice for nested queries<commit_after><?php
/**
 * @package plugins.elasticSearch
 * @subpackage model.items
 */
abstract class ESearchNestedObjectItem extends ESearchItem
{

	const DEFAULT_INNER_HITS_SIZE = 10;

	protected static function initializeInnerHitsSize($queryAttributes)
	{
		$overrideInnerHitsSize = $queryAttributes->getOverrideInnerHitsSize();
		if($overrideInnerHitsSize)
			return $overrideInnerHitsSize;

		$innerHitsConfig = kConf::get('innerHits', 'elastic');
		$innerHitsConfigKey = static::INNER_HITS_CONFIG_KEY;
		$innerHitsSize = isset($innerHitsConfig[$innerHitsConfigKey]) ? $innerHitsConfig[$innerHitsConfigKey] : self::DEFAULT_INNER_HITS_SIZE;

		return $innerHitsSize;
	}

	protected static function createNestedQueryForItems($eSearchItemsArr, $boolOperator, &$queryAttributes)
	{
		$finalQuery = array();
		$innerHitsSize = self::initializeInnerHitsSize($queryAttributes);
		$allowedSearchTypes = static::getAllowedSearchTypesForField();

		// must_not was already set in a higher level of the query inside ESearchOperator
		if($boolOperator == 'must_not')
			$boolOperator = 'must';
		
		//don't group to a single query if the operator is AND
		if($boolOperator == 'must')
		{
			foreach ($eSearchItemsArr as $eSearchItem)
			{
				$nestedQuery = null;
				$nestedQuery['nested']['path'] = static::NESTED_QUERY_PATH;
				$nestedQuery['nested']['inner_hits'] = array('size' => $innerHitsSize, '_source' => true);
				$queryAttributes->setScopeToInner();
				static::createSingleItemSearchQuery($eSearchItem, $boolOperator, $nestedQuery, $allowedSearchTypes, $queryAttributes);
				$highlight = kBaseSearch::getHighlightSection(static::HIGHLIGHT_CONFIG_KEY, $queryAttributes);
				if(isset($highlight))
					$nestedQuery['nested']['inner_hits']['highlight'] = $highlight;
				$finalQuery[] = $nestedQuery;
			}
		}
		else
		{
			$nestedQuery['nested']['path'] = static::NESTED_QUERY_PATH;;
			$nestedQuery['nested']['inner_hits'] = array('size' => $innerHitsSize, '_source' => true);
			$queryAttributes->setScopeToInner();
			foreach ($eSearchItemsArr as $eSearchItem)
			{
				static::createSingleItemSearchQuery($eSearchItem, $boolOperator, $nestedQuery, $allowedSearchTypes, $queryAttributes);
			}
			$highlight = kBaseSearch::getHighlightSection(static::HIGHLIGHT_CONFIG_KEY, $queryAttributes);
			if(isset($highlight))
				$nestedQuery['nested']['inner_hits']['highlight'] = $highlight;
{'fraction_non_alphanumeric': 0.13604577240940877, 'fraction_numerical': 0.00169527442254715, 'mean_word_length': 6.750410509031199, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 17, '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': '6206437', 'n_tokens_mistral': 1623, 'n_tokens_neox': 1426, 'n_words': 241}