# Using NEST how can I build up a search query

**URL:** <https://discuss.elastic.co/t/using-nest-how-can-i-build-up-a-search-query/125161>\
**Category:** Elasticsearch\
**Created:** [March 22, 2018, 10:03am UTC](https://discuss.elastic.co/t/using-nest-how-can-i-build-up-a-search-query/125161 "2018-03-22T10:03:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jchannon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jchannon/32/23280_2.png) [@jchannon](https://discuss.elastic.co/u/jchannon)\
**Post date:** [March 22, 2018, 10:03am UTC](https://discuss.elastic.co/t/using-nest-how-can-i-build-up-a-search-query/125161/1 "2018-03-22T10:03:50Z")

</div>

I am trying to use the NEST APIs to execute a search. In my logic of building this up with the fluent api I need to do a conditional check and so far I have this however I believe my assignment will be getting overwritten. Is there a recommended way to do this type of thing?

```auto
   var d = new SearchDescriptor<AuditLog>();
                d = d.Query(q => q
                    .Bool(b => b
                        .Filter(bf => bf
                            .DateRange(r => r
                                .Field(f => f.UtcDateTime)
                                .GreaterThanOrEquals(message.UtcFrom)
                                .LessThanOrEquals(message.UtcTo)
                            )
                        ).Filter(fg => fg.Terms(descriptor => descriptor.Field(f => f.AccessControlid).Terms(aclIds)))
                    )
                );

                if (message.CustomerId != -1)
                {
                    d = d.Query(q => q.Bool(kk => kk.Must(must => must.Match(m => m.Field(c => c.AuditUserId).Query("2")))));
                }
                
               
                var resp = this.elasticClient.Search<AuditLog>(s =>
                {
                    s = d;
                    return s
                        .Index(ElasticIndex.AuditLog)
                        .Size(20)
                        .From(message.Offset);
                });

```

---

<div class="post-metadata">

**Author:** ![jchannon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jchannon/32/23280_2.png) [@jchannon](https://discuss.elastic.co/u/jchannon)\
**Post date:** [March 22, 2018, 1:56pm UTC](https://discuss.elastic.co/t/using-nest-how-can-i-build-up-a-search-query/125161/2 "2018-03-22T13:56:32Z")

</div>

Ended up using this which seems to work. No idea if it's the correct way though.

```auto
                Func<QueryContainerDescriptor<AuditLog>, QueryContainer> dateRangeSearch = descriptor => descriptor.DateRange(r => r
                    .Field(f => f.UtcDateTime)
                    .GreaterThanOrEquals(message.UtcFrom)
                    .LessThanOrEquals(message.UtcTo)
                );

                Func<QueryContainerDescriptor<AuditLog>, QueryContainer> aclSearch = descriptor => descriptor.Terms(r => r.Field(f => f.AccessControlid).Terms(aclIds));

                Func<QueryContainerDescriptor<AuditLog>, QueryContainer> customerSearch = descriptor => descriptor.Terms(r => r.Field(f => f.AuditUserId).Terms(userIds));

                var searchTerms = new List<Func<QueryContainerDescriptor<AuditLog>, QueryContainer>> { dateRangeSearch, aclSearch };

                if (message.CustomerId != -1)
                {
                    searchTerms.Add(customerSearch);
                }

                var resp = this.elasticClient.Search<AuditLog>(s =>
                {
                    return s
                        .Query(q => q.Bool(b => b.Filter(searchTerms)))
                        .Index(ElasticIndex.AuditLog)
                        .Size(20)
                        .From(message.Offset);
                });

```

---

<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:** [March 23, 2018, 12:38am UTC](https://discuss.elastic.co/t/using-nest-how-can-i-build-up-a-search-query/125161/3 "2018-03-23T00:38:34Z")

</div>

Your query looks fine. _Pretty much_ all NEST fluent API calls are assignative, so last assignment wins. I think may be one or two places in the API where a call is additive, but this should be considered a bug.

With [query operator overloading](https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/bool-queries.html), the code could be made more succinct with

```auto
resp = client.Search<AuditLog>(s => s
	.Query(q =>
	{
		QueryContainer c = +q.DateRange(r => r
				.Field(f => f.UtcDateTime)
				.GreaterThanOrEquals(message.UtcFrom)
				.LessThanOrEquals(message.UtcTo)
			) && 
			+q.Terms(r => r.Field(f => f.AccessControlid).Terms(aclIds));

		if (message.CustomerId != -1)
			c = c && +q.Terms(r => r.Field(f => f.AuditUserId).Terms(userIds));

		return c;
	})
	.Index("audit_log")
	.Size(20)
	.From(message.Offset)
);

```

This takes advantage of unary `+` operator overloading to specify a bool query filter clause, and combining them with binary `&&` operator.

With the following

```auto
var now = DateTime.UtcNow;
var message = new Message
{
	CustomerId = 10,
	UtcFrom = now.AddMonths(-1),
	UtcTo = now,
};

var aclIds = new []{"acl1", "acl2"};
var userIds = new []{"user1", "user2"};

```

both produce

```json
POST http://localhost:9200/audit_log/auditlog/_search
{
  "from": 0,
  "size": 20,
  "query": {
    "bool": {
      "filter": [
        {
          "range": {
            "utcDateTime": {
              "gte": "2018-02-23T00:30:10.019915Z",
              "lte": "2018-03-23T00:30:10.019915Z"
            }
          }
        },
        {
          "terms": {
            "accessControlid": [
              "acl1",
              "acl2"
            ]
          }
        },
        {
          "terms": {
            "auditUserId": [
              "user1",
              "user2"
            ]
          }
        }
      ]
    }
  }
}

```

---

<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:** [April 20, 2018, 12:38am UTC](https://discuss.elastic.co/t/using-nest-how-can-i-build-up-a-search-query/125161/4 "2018-04-20T00:38:44Z")

</div>

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