<commit_before>﻿using System.Collections.Generic;
using System.Linq;
using Nest;
using Tests.Core.ManagedElasticsearch.Clusters;
using Tests.Domain;
using Tests.Framework.Integration;
using static Nest.Infer;

namespace Tests.QueryDsl.Joining.Nested
{
	/**
	* Nested query allows to query nested objects / docs (see {ref_current}/nested.html[nested mapping]).
	* The query is executed against the nested objects / docs as if they were indexed as separate
	* docs (they are, internally) and resulting in the root parent doc (or parent nested mapping).
	*
	* See the Elasticsearch documentation on {ref_current}/query-dsl-nested-query.html[nested query] for more details.
	*/
	public class NestedUsageTests : QueryDslUsageTestsBase
	{
		public NestedUsageTests(ReadOnlyCluster i, EndpointUsage usage) : base(i, usage) { }

		protected override ConditionlessWhen ConditionlessWhen => new ConditionlessWhen<INestedQuery>(a => a.Nested)
		{
			q => q.Query = null,
			q => q.Query = ConditionlessQuery,
			q => q.Path = null,
		};

		protected override QueryContainer QueryInitializer => new NestedQuery
		{
			Name = "named_query",
			Boost = 1.1,
			InnerHits = new InnerHits { Explain = true },
			Path = Field<Project>(p => p.CuratedTags),
			Query = new TermsQuery
			{
				Field = Field<Project>(p => p.CuratedTags.First().Name),
				Terms = new[] { "lorem", "ipsum" }
			},
			IgnoreUnmapped = true
		};

		protected override object QueryJson => new
		{
			nested = new
			{
				_name = "named_query",
				boost = 1.1,
				query = new
				{
					terms = new Dictionary<string, object>
					{
						{ "curatedTags.name", new[] { "lorem", "ipsum" } }
					}
				},
				ignore_unmapped = true,
				path = "curatedTags",
				inner_hits = new
				{
					explain = true
				}
			}
		};

		protected override QueryContainer QueryFluent(QueryContainerDescriptor<Project> q) => q
			.Nested(c => c
				.Name("named_query")
				.Boost(1.1)
				.InnerHits(i => i.Explain())
				.Path(p => p.CuratedTags)
				.Query(nq => nq
					.Terms(t => t
						.Field(f => f.CuratedTags.First().Name)
						.Terms("lorem", "ipsum")
					)
				)
				.IgnoreUnmapped()
			);
	}
}<commit_msg>Use nested field mapping in nested query usage tests<commit_after>﻿using System.Collections.Generic;
using System.Linq;
using Nest;
using Tests.Core.ManagedElasticsearch.Clusters;
using Tests.Domain;
using Tests.Framework.Integration;
using static Nest.Infer;

namespace Tests.QueryDsl.Joining.Nested
{
	/**
	* Nested query allows to query nested objects / docs (see {ref_current}/nested.html[nested mapping]).
	* The query is executed against the nested objects / docs as if they were indexed as separate
	* docs (they are, internally) and resulting in the root parent doc (or parent nested mapping).
	*
	* See the Elasticsearch documentation on {ref_current}/query-dsl-nested-query.html[nested query] for more details.
	*/
	public class NestedUsageTests : QueryDslUsageTestsBase
	{
		public NestedUsageTests(ReadOnlyCluster i, EndpointUsage usage) : base(i, usage) { }

		protected override ConditionlessWhen ConditionlessWhen => new ConditionlessWhen<INestedQuery>(a => a.Nested)
		{
			q => q.Query = null,
			q => q.Query = ConditionlessQuery,
			q => q.Path = null,
		};

		protected override QueryContainer QueryInitializer => new NestedQuery
		{
			Name = "named_query",
			Boost = 1.1,
			InnerHits = new InnerHits { Explain = true },
			Path = Field<Project>(p => p.Tags),
			Query = new TermsQuery
			{
				Field = Field<Project>(p => p.Tags.First().Name),
				Terms = new[] { "lorem", "ipsum" }
			},
			IgnoreUnmapped = true
		};

		protected override object QueryJson => new
		{
			nested = new
			{
				_name = "named_query",
				boost = 1.1,
				query = new
				{
					terms = new Dictionary<string, object>
					{
						{ "tags.name", new[] { "lorem", "ipsum" } }
					}
				},
				ignore_unmapped = true,
				path = "tags",
				inner_hits = new
				{
					explain = true
				}
			}
		};

		protected override QueryContainer QueryFluent(QueryContainerDescriptor<Project> q) => q
			.Nested(c => c
				.Name("named_query")
				.Boost(1.1)
				.InnerHits(i => i.Explain())
				.Path(p => p.Tags)
				.Query(nq => nq
					.Terms(t => t
						.Field(f => f.Tags.First().Name)
						.Terms("lorem", "ipsum")
					)
				)
				.IgnoreUnmapped()
			);
	}
}
{'fraction_non_alphanumeric': 0.12554811908608354, 'fraction_numerical': 0.0027694438033694898, 'mean_word_length': 3.1236917221693625, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 45, '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': '374407', 'n_tokens_mistral': 1717, 'n_tokens_neox': 1415, 'n_words': 364}