# Is it possible to combine results for .DateRange and .QueryString using NEST client?

**URL:** <https://discuss.elastic.co/t/is-it-possible-to-combine-results-for-daterange-and-querystring-using-nest-client/244474>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [August 11, 2020, 1:32am UTC](https://discuss.elastic.co/t/is-it-possible-to-combine-results-for-daterange-and-querystring-using-nest-client/244474 "2020-08-11T01:32:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mccamike](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mccamike/32/73646_2.png) [@mccamike](https://discuss.elastic.co/u/mccamike)\
**Post date:** [August 11, 2020, 1:32am UTC](https://discuss.elastic.co/t/is-it-possible-to-combine-results-for-daterange-and-querystring-using-nest-client/244474/1 "2020-08-11T01:32:41Z")

</div>

I currently have a query where I'd like to combine the free text nature of .QueryString with the range operations of .DateRange in the same resultset.

I was looking at how to [combine queries](https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/writing-queries.html#combining-queries) and it looks like you can use the bool operator, but I can't find an example of combining .QueryString and .DateRange.

Is what I want to do possible?

Thanks!

---

<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:** [August 17, 2020, 1:16am UTC](https://discuss.elastic.co/t/is-it-possible-to-combine-results-for-daterange-and-querystring-using-nest-client/244474/2 "2020-08-17T01:16:54Z")

</div>

Hi @mccamike, [the `bool` query is typically used to combine multiple queries together](https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/bool-queries.html) in order to calculate an overall score for matching documents.

Typically with range queries, the answer to whether a document matches is a simple yes/no i.e. does the document field fall within the input range or not? These kinds of queries can be placed in a [`bool` query filter clause](https://www.elastic.co/guide/en/elasticsearch/reference/7.8/query-dsl-bool-query.html#score-bool-filter). For full-text queries such as `query_string` query where a score is calculated for a document based on how well it matches the input query, these are typically placed in the `must` clause. Putting this altogether

```auto
var client = new ElasticClient();

var response = client.Search<MyDocument>(s => s
	.Query(q => q
		.Bool(b => b
			.Must(mu => mu
				.QueryString(qs => qs
					.Fields(f => f
						.Fields("field1", "field2")
					)
					.Query("my query string input")
				)
			)
			.Filter(fi => fi
				.DateRange(d => d
					.Field("datefield")
					.GreaterThan(DateMath.Now.Subtract("7d"))
				)
			)
		)
	)
);

```

which produces the query

```json
{
  "query": {
    "bool": {
      "filter": [
        {
          "range": {
            "datefield": {
              "gt": "now-7d"
            }
          }
        }
      ],
      "must": [
        {
          "query_string": {
            "fields": [
              "field1",
              "field2"
            ],
            "query": "my query string input"
          }
        }
      ]
    }
  }
}

```

Because writing `bool` queries is so common, [NEST overloads operators on queries](https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/bool-queries.html#_operator_overloading) to make writing complex queries more succinct. The above query using operator overloading looks like

```auto
var response = client.Search<MyDocument>(s => s
	.Query(q => q
		.QueryString(qs => qs
			.Fields(f => f
				.Fields("field1", "field2")
			)
			.Query("my query string input")
		) && +q
		.DateRange(d => d
			.Field("datefield")
			.GreaterThan(DateMath.Now.Subtract("7d"))
		)
	)
);

```

---

<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:** [September 14, 2020, 1:16am UTC](https://discuss.elastic.co/t/is-it-possible-to-combine-results-for-daterange-and-querystring-using-nest-client/244474/3 "2020-09-14T01:16:58Z")

</div>

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