title: ELASTICSEARCH- Return filtered fields based on values
body: <p>I am developing a query in which I would like to identify those fields within &quot;CES&quot;, those that show in the field &quot;ces.des&quot; or &quot;ces.output&quot; the value &quot;jack&quot;</p>
  <pre><code> {&quot;_source&quot;: [&quot;ces.desc&quot;,&quot;ces.output&quot;] ,
   &quot;query&quot;: {
     &quot;nested&quot;: {
       &quot;path&quot;: &quot;ces&quot;,
       &quot;query&quot;: {
         &quot;bool&quot;: {
            &quot;should&quot;: [
              {&quot;term&quot;: {&quot;ces.desc&quot;: &quot;jack&quot;}},
              {&quot;term&quot;: {&quot;ces.output&quot;: &quot;jack&quot;}}
            ]
          }
        }
      }
    },
    &quot;aggs&quot;: {
      &quot;nestedData&quot;: {
        &quot;nested&quot;: {
          &quot;path&quot;: &quot;ces&quot;
        },
        &quot;aggs&quot;: {
          &quot;data_desc&quot;: {
            &quot;filter&quot;: {
              &quot;term&quot;: {
                &quot;ces.desc&quot;: &quot;jack&quot;
                  
              }
            }
          }
        }
      }
    }
  }
  </code></pre>
  <p>And the output is :</p>
  <pre><code>            {
                &quot;ces&quot; : [
                  {
                    &quot;output&quot; : &quot;Laura&quot;, &lt;-------------- WRONG 
                    &quot;desc&quot; : &quot;fernando&quot; &lt;-------------- WRONG
                  },
                  {
                    &quot;output&quot; : &quot;&quot;,
                    &quot;desc&quot; : &quot;jack&quot; &lt;-------------- RIGHT
                  }
  
                 &quot;output&quot; : &quot;jack&quot;,&lt;-------------- RIGHT
                    &quot;desc&quot; : &quot;Fer&quot;
                  },
               }
  </code></pre>
  <p>mapping:</p>
  <pre><code>{
    &quot;names_1&quot; : {
      &quot;aliases&quot; : { },
      &quot;mappings&quot; : {
        &quot;properties&quot; : {
          &quot;created_at&quot; : {
            &quot;type&quot; : &quot;text&quot;,
            &quot;fields&quot; : {
              &quot;keyword&quot; : {
                &quot;type&quot; : &quot;keyword&quot;,
                &quot;ignore_above&quot; : 256
              }
            }
          },
          &quot;data&quot; : {
            &quot;properties&quot; : {
              &quot;addresses&quot; : {
                &quot;properties&quot; : {
                  &quot;asn&quot; : {
                    &quot;type&quot; : &quot;long&quot;
                  },
                  &quot;ces&quot; : {
                    &quot;type&quot; : &quot;nested&quot;,
                    &quot;properties&quot; : {
                      &quot;banner&quot; : {
                        &quot;type&quot; : &quot;text&quot;,
                        &quot;fields&quot; : {
                          &quot;keyword&quot; : {
                            &quot;type&quot; : &quot;keyword&quot;,
                            &quot;ignore_above&quot; : 256
                          }
                        }
                      },
                      &quot;desc&quot; : {
                        &quot;type&quot; : &quot;text&quot;,
                        &quot;fields&quot; : {
                          &quot;keyword&quot; : {
                            &quot;type&quot; : &quot;keyword&quot;,
                            &quot;ignore_above&quot; : 256
                          }
                        }
                      },
                      &quot;output&quot; : {
                        &quot;type&quot; : &quot;text&quot;,
                        &quot;fields&quot; : {
                          &quot;keyword&quot; : {
                            &quot;type&quot; : &quot;keyword&quot;,
                            &quot;ignore_above&quot; : 256
                          }
                        }
                      }
                    }
                  }
                }
              },
              &quot;name&quot; : {
                &quot;type&quot; : &quot;text&quot;,
                &quot;fields&quot; : {
                  &quot;keyword&quot; : {
                    &quot;type&quot; : &quot;keyword&quot;,
                    &quot;ignore_above&quot; : 256
                  }
                }
              },
              &quot;source&quot; : {
                &quot;type&quot; : &quot;text&quot;,
                &quot;fields&quot; : {
                  &quot;keyword&quot; : {
                    &quot;type&quot; : &quot;keyword&quot;,
                    &quot;ignore_above&quot; : 256
                  }
                }
              },
              &quot;tag&quot; : {
                &quot;type&quot; : &quot;text&quot;,
                &quot;fields&quot; : {
                  &quot;keyword&quot; : {
                    &quot;type&quot; : &quot;keyword&quot;,
                    &quot;ignore_above&quot; : 256
                  }
                }
              }
            }
          },
          &quot;error&quot; : {
            &quot;type&quot; : &quot;long&quot;
          },
          &quot;finished_at&quot; : {
            &quot;type&quot; : &quot;text&quot;,
            &quot;fields&quot; : {
              &quot;keyword&quot; : {
                &quot;type&quot; : &quot;keyword&quot;,
                &quot;ignore_above&quot; : 256
              }
            }
          },
          &quot;total&quot; : {
            &quot;type&quot; : &quot;long&quot;
          }
        }
      } 
    }
  }
  </code></pre>
  <p>I would like to filter only those that comply that present the values ​​according to the &quot;bool&quot; condition.
  if (ces.desc == &quot;jack&quot;) or (ces.output == &quot;jack)&quot;
  return
  ces.desc,ces.output key and value</p>
  <p>Even if I add&quot;agg&quot;, make a &quot;JACK&quot; count
  doc_value = 2</p>
  <p>What part of the query am I making the error?</p>
  <p>query mapping:</p>
  <pre><code>{  
    &quot;mappings&quot;: {
     &quot;properties&quot;: {
        &quot;data.addresses&quot;:{
          &quot;type&quot;:&quot;nested&quot;,
          &quot;properties&quot;: {
            &quot;data.addresses.ces&quot;: {
              &quot;type&quot;: &quot;nested&quot;
            }
          }
        }
      }
    }
  }
  </code></pre>