private static bool TryFlattenShould()

in src/Elastic.Clients.Elasticsearch/_Shared/Types/QueryDsl/BoolQueryOrExtensions.cs [33:56]


	private static bool TryFlattenShould(Query leftContainer, Query rightContainer, BoolQuery leftBool, BoolQuery rightBool, out Query query)
	{
		query = null;

		var leftCanMerge = leftContainer.CanMergeShould();
		var rightCanMerge = rightContainer.CanMergeShould();

		if (!leftCanMerge && !rightCanMerge)
			query = CreateShouldContainer(new List<Query> { leftContainer, rightContainer });

		// Left can merge but right's bool can not. instead of wrapping into a new bool we inject the whole bool into left
		else if (leftCanMerge && !rightCanMerge && rightBool is not null)
		{
			leftBool.Should = leftBool.Should.AddIfNotNull(rightContainer).ToArray();
			query = leftContainer;
		}
		else if (rightCanMerge && !leftCanMerge && leftBool is not null)
		{
			rightBool.Should = rightBool.Should.AddIfNotNull(leftContainer).ToArray();
			query = rightContainer;
		}

		return query != null;
	}