# Query on responded output

**URL:** <https://discuss.elastic.co/t/query-on-responded-output/298907>\
**Category:** Elasticsearch\
**Created:** [March 6, 2022, 9:27am UTC](https://discuss.elastic.co/t/query-on-responded-output/298907 "2022-03-06T09:27:23Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tanvir\_Ahmmed](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tanvir_ahmmed/32/99289_2.png) [@Tanvir\_Ahmmed](https://discuss.elastic.co/u/Tanvir_Ahmmed)\
**Post date:** [March 6, 2022, 9:27am UTC](https://discuss.elastic.co/t/query-on-responded-output/298907/1 "2022-03-06T09:27:23Z")

</div>

How to write a conditional query on multiple fields?  
Explain: I have an index let say " **case**" and the fields are " **title**", "**secondTitle(contain nested obj)", "source(contain nested obj)**". Now I want to search on title and want number of document inside of **secondTitle,** which also contains in **source** field of another document.

```auto
PUT /case_indx_tmp_tmp
{
  "mappings": {
    "properties": {
      "title":{
        "type": "text",
        "fields": {
          "title":{
            "type":"keyword"
          }
        }
      },
      "secondTitle":{
        "type": "nested",
        "properties": {
          "second_title":{
            "type":"text",
            "fields": {
              "secondtitle":{
                "type":"keyword"
              }
        }
          }
        }
      },
      "source":{
        "type": "nested",
        "properties": {
          "source_title":{
            "type":"text",
            "fields": {
              "sourcetitle":{
                "type":"keyword"
              }
        }
          }
        }
      }
    }
  }
}

PUT /case_indx_tmp_tmp/_doc/1
{
    "title" : "Case 1",
    "secondTitle" : [
        {
            "case_title" : "Case 2"
        }
    ],
    "source":[
      {
        "source_title":"Case 3"
      },
      {
        "source_title":"Case 4"
      }
    ]
}
PUT /case_indx_tmp_tmp/_doc/2
{
    "title" : "Case 2",
    "secondTitle" : [
        {
            "case_title" : "Case 3"
        },
        {
            "case_title" : "Case 4"
        },
        {
            "case_title" : "Case 1"
        }
    ],
    "source":[
      {
        "source_title":"Case 1"
      }
    ]
}
PUT /case_indx_tmp_tmp/_doc/3
{
    "title" : "Case 3",
    "secondTitle" : [
        {
            "case_title" : "Case 5"
        },
        {
            "case_title" : "Case 4"
        },
        {
            "case_title" : "Case 1"
        }
    ],
    "source":[
      {
        "source_title":"Case 2"
      },
      {
        "source_title":"Case 5"
      }
    ]
}
PUT /case_indx_tmp_tmp/_doc/4
{
    "title" : "Case 5",
    "source":[
      {
        "source_title":"Case 3"
      },
      {
        "source_title":"Case 4"
      },
      {
        "source_title":"Case 2"
      }
    ]
}

```

If search term is "Case" then it will return

> title: Case 1  
> secondTitle\_count: 1
> 
> title: Case 2  
> secondTitle\_count: 3
> 
> title: Case 3  
> secondTitle\_count: 2
> 
> title: Case 5  
> secondTitle\_count: 0

My solution was, to iterate all documents which return after search query and make new bool query object for the "source" field where values are from secondTitle.

Thank You.

---

<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 3, 2022, 9:27am UTC](https://discuss.elastic.co/t/query-on-responded-output/298907/2 "2022-04-03T09:27:55Z")

</div>

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