# Search C# NEST query on attachments

**URL:** <https://discuss.elastic.co/t/search-c-nest-query-on-attachments/156302>\
**Category:** Elasticsearch\
**Created:** [November 12, 2018, 2:17pm UTC](https://discuss.elastic.co/t/search-c-nest-query-on-attachments/156302 "2018-11-12T14:17:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![alina.frey](https://avatars.discourse-cdn.com/v4/letter/a/c57346/32.png) [@alina.frey](https://discuss.elastic.co/u/alina.frey)\
**Post date:** [November 12, 2018, 2:17pm UTC](https://discuss.elastic.co/t/search-c-nest-query-on-attachments/156302/1 "2018-11-12T14:17:10Z")

</div>

- My goal is to be able to perform a search on the Content of a list of documents called PersonDocuments attached to IndexablePersonModel, and return the persons that have documents that contain the query term.

- The query that I'm using is returning the respective persons ONLY IF the query term matches EXACTLY the WHOLE content of the attached document. But if I search only for one word in the document, nothing is returned. Please advise.

- Using ingest-attachment plugin.

- Elasticsearch version: 5.5.1

- NEST: 5.5

- PersonDocuments is a list of IndexablePersonDocument on the IndexablePersonModel

```auto
public IEnumerable<IndexablePersonDocument> PersonDocuments { get; set; }

```

- And one of the attributes of IndexablePersonDocument is string Content.

- Here is the query that I have so far:

```auto
protected override QueryContainer Query(QueryContainerDescriptor<IndexablePersonModel> q)
{
	var returnQuery = q
	.Match(m => m
		.Field(p => p.Name.Suffix(SearchConstants.Keyword))
		.Boost(SearchConstants.Boosts.XXLarge)
		.Query(Form.Query)
	)
	|| q.Nested(n => n
		.Boost(SearchConstants.Boosts.XXXLarge)
		.InnerHits(i => i.Explain())
		.Path(p => p.PersonDocuments)
		.Query(nq => +nq
			.Bool(b => b
				.Should(
					s => s.Term(p => p.PersonDocuments.First().Attachment.Content.Suffix(SearchConstants.Keyword), Form.Query)
				).MinimumShouldMatch(MinimumShouldMatch.Fixed(1))
			)
		)
		.IgnoreUnmapped()
	)
	|| q.FunctionScore(fs => fs
		.MaxBoost(SearchConstants.Boosts.Large)
		.Functions(ff => ff
			.FieldValueFactor(fvf => fvf
				.Field(p => p.TotalPurchasedQuantity)
				.Factor(0.0001)
			)
		)
		.Query(query => query
			.MultiMatch(m => m
				.Fields(f => f
						.Field(p => p.Name, SearchConstants.Boosts.XSmall)
						.Field(p => p.PersonDocuments.First().Attachment.Content, SearchConstants.Boosts.XXLarge)
				)
				.Operator(Operator.And)
				.Query(Form.Query)
			)
		)
	)
	#endregion
	;

	return returnQuery;
}  

```

---

<div class="post-metadata">

**Author:** ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)\
**Post date:** [November 19, 2018, 11:21pm UTC](https://discuss.elastic.co/t/search-c-nest-query-on-attachments/156302/2 "2018-11-19T23:21:31Z")

</div>

> [@alina.frey](#):
>
> public IEnumerable\<IndexablePersonDocument\> PersonDocuments { get; set; }

How is this property mapped as a field in Elasticsearch? Would you be able to show the JSON mapping in the target index? I see that the `PersonDocuments` property is used in both a `nested` query and a `multi_match` query, so one of these queries won't be working as expected, depending on how it's mapped.

---

<div class="post-metadata">

**Author:** ![alina.frey](https://avatars.discourse-cdn.com/v4/letter/a/c57346/32.png) [@alina.frey](https://discuss.elastic.co/u/alina.frey)\
**Post date:** [November 20, 2018, 2:49am UTC](https://discuss.elastic.co/t/search-c-nest-query-on-attachments/156302/3 "2018-11-20T02:49:22Z")

</div>

I figured it out. I was supposed to use a Match:

```auto
q.Nested(n => n
	.Boost(SearchConstants.Boosts.XXXLarge)
	.ScoreMode(NestedScoreMode.Max) // Give the person the _score from the best-matching nested document
	.InnerHits(i => i.Explain(true))
	.Path(p => p.PersonDocuments)
	.Query(nq => +nq
		.Match(m => m
			.Field( a => a.PersonDocuments.First().Attachment.Content)
			.Query(Form.Query)
		)
	)
	.IgnoreUnmapped()
)

```

---

<div class="post-metadata">

**Author:** ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)\
**Post date:** [November 20, 2018, 3:04am UTC](https://discuss.elastic.co/t/search-c-nest-query-on-attachments/156302/4 "2018-11-20T03:04:00Z")

</div>

There is the usage of a property on `PersonDocuments` inside the query of the `function_score` query.

> [@alina.frey](#):
>
> .Field(p =\> p.PersonDocuments.First().Attachment.Content, SearchConstants.Boosts.XXLarge)

If `PersonDocuments` is mapped as a `nested` type, this should be inside of a `nested` query here too, and combined with the `match` query`on`Name`.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [December 18, 2018, 3:04am UTC](https://discuss.elastic.co/t/search-c-nest-query-on-attachments/156302/5 "2018-12-18T03:04:19Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
