# How to write elastic search query for one required parameter and another optional paramater

**URL:** <https://discuss.elastic.co/t/how-to-write-elastic-search-query-for-one-required-parameter-and-another-optional-paramater/328089>\
**Category:** Elasticsearch\
**Created:** [March 20, 2023, 2:39pm UTC](https://discuss.elastic.co/t/how-to-write-elastic-search-query-for-one-required-parameter-and-another-optional-paramater/328089 "2023-03-20T14:39:33Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Phoenix1990](https://avatars.discourse-cdn.com/v4/letter/p/ac8455/32.png) [@Phoenix1990](https://discuss.elastic.co/u/Phoenix1990)\
**Post date:** [March 20, 2023, 2:39pm UTC](https://discuss.elastic.co/t/how-to-write-elastic-search-query-for-one-required-parameter-and-another-optional-paramater/328089/1 "2023-03-20T14:39:33Z")

</div>

I need to create an Elasticsearch endpoint with two paramater : Session(required field),CallType(Optional) which returns call details bewteen Teacher and student. I can search for specific session with below details. My query will also have call duration field which will calculate total duration of call between first and last CallDateTime

Search response should be like

```auto
{
  "CallDuration": 10,
  "Session": "g4cld594-7a9d-4177-919c-df62cab73a79",
  "StudentName": "Andy White",
  "TeacherName": "Annie Frank",
  "CallTypes": [
    {
      "CallType": "TeacherJoined",
      "CallDateTime": "2023-01-01T14:50:57.403083+05:00"
    },
   {
      "CallType": "StudentJoined",
      "CallDateTime": "2023-01-02T14:50:57.403083+05:00"
    },
    {
      "CallType": "TeacherJoined",
      "CallDateTime": "2023-01-01T14:55:57.403083+05:00"
    },
   
  ]
}

```

How can I modify below endpoint query to achieve desired response?

```auto
public async Task<List<CallDetailsResponse>> SearchCallDetails(string session,string callType)
        {
            
            var results = await client.SearchAsync<CallDetailsResponse>(s => s
                 .From(0)
                 .Size(10)
                 .Query(q => q
                     .Match(m => m
                         .Field(f => f.Session)
                         .Query(session)
                     )
                 ));
            return (CallDetailsResponse)results.Documents?.ToList();
        }

```

---

<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 17, 2023, 2:40pm UTC](https://discuss.elastic.co/t/how-to-write-elastic-search-query-for-one-required-parameter-and-another-optional-paramater/328089/2 "2023-04-17T14:40:02Z")

</div>

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