# Fields in the nested filter

**URL:** <https://discuss.elastic.co/t/fields-in-the-nested-filter/13276>\
**Category:** Elasticsearch\
**Created:** [August 20, 2013, 5:29pm UTC](https://discuss.elastic.co/t/fields-in-the-nested-filter/13276 "2013-08-20T17:29:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Abhishek\_Andhavarapu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abhishek_andhavarapu/32/782_2.png) [@Abhishek\_Andhavarapu](https://discuss.elastic.co/u/Abhishek_Andhavarapu)\
**Post date:** [August 20, 2013, 5:29pm UTC](https://discuss.elastic.co/t/fields-in-the-nested-filter/13276/1 "2013-08-20T17:29:32Z")

</div>

Hi All,

My Mappings  
{  
"response": {  
"properties": {  
"rtext": {  
"type": "nested",  
"properties": {  
"textmapid": {  
"type": "integer"  
},  
"textvalue": {  
"type": "string"  
}  
}  
}  
}  
}  
}

Sample data

{  
"rtext" : [  
{  
"textmapid" : 1,  
"textvalue" : "Jennifer"  
},  
{  
"textmapid" : 2,  
"textvalue" : "Adam"  
}  
]  
},

{  
"rtext" : [  
{  
"textmapid" : 1,  
"textvalue" : "John"  
},  
{  
"textmapid" : 2,  
"textvalue" : "A"  
}  
]  
}

Query  
{  
"fields": [  
"rtext.textvalue"  
],  
"filter": {  
"bool": {  
"must": [  
{  
"nested": {  
"path": "rtext",  
"query": {  
"bool": {  
"must": [  
{  
"term": {  
"rtext.textmapid": 1  
}  
}  
]  
}  
}  
}  
}  
]  
}  
}  
}

My Query comes back hits: [

- {
  - \_index: xxx
  - \_type: response
  - \_id: 1
  - \_score: 1
  - fields: {
    - rtext.textvalue: [
      - Jennifer,
      - Adam  
]  
}  
},

- {
  - \_index: xxx
  - \_type: response
  - \_id: 1
  - \_score: 1
  - fields: {
    - rtext.textvalue: [
      - John,
      - A  
]  
}  
}

Since nested query/filter doesn't support fields how do I get just  
textvalues whoose textmapid is 1 My expected output is Jennifer, John.

Any help is appreciated,  
Abhishek

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Abhishek\_Andhavarapu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abhishek_andhavarapu/32/782_2.png) [@Abhishek\_Andhavarapu](https://discuss.elastic.co/u/Abhishek_Andhavarapu)\
**Post date:** [August 20, 2013, 9:42pm UTC](https://discuss.elastic.co/t/fields-in-the-nested-filter/13276/2 "2013-08-20T21:42:03Z")

</div>

Figured it out. Used the script\_fields to filter the source. Any other  
efficient way to do this ?

{  
"script\_fields": {  
"textdata": {  
"script": "for (int i = 0; i \< \_source.rtext.size();  
i++){if(\_source.rtext[i].textmapid == mapid){ return  
\_source.rtext[i].textvalue} }",  
"params": {  
"mapid": 1  
}  
}  
},  
"filter": {  
"bool": {  
"must": [  
{  
"nested": {  
"path": "rtext",  
"query": {  
"bool": {  
"must": [  
{  
"term": {  
"rtext.textmapid": 1  
}  
}  
]  
}  
}  
}  
}  
]  
}  
}  
}

On Tuesday, August 20, 2013 11:29:32 AM UTC-6, Abhishek Andhavarapu wrote:

> Hi All,
> 
> My Mappings  
> {  
> "response": {  
> "properties": {  
> "rtext": {  
> "type": "nested",  
> "properties": {  
> "textmapid": {  
> "type": "integer"  
> },  
> "textvalue": {  
> "type": "string"  
> }  
> }  
> }  
> }  
> }  
> }
> 
> Sample data
> 
> {  
> "rtext" : [  
> {  
> "textmapid" : 1,  
> "textvalue" : "Jennifer"  
> },  
> {  
> "textmapid" : 2,  
> "textvalue" : "Adam"  
> }  
> ]  
> },
> 
> {  
> "rtext" : [  
> {  
> "textmapid" : 1,  
> "textvalue" : "John"  
> },  
> {  
> "textmapid" : 2,  
> "textvalue" : "A"  
> }  
> ]  
> }
> 
> Query  
> {  
> "fields": [  
> "rtext.textvalue"  
> ],  
> "filter": {  
> "bool": {  
> "must": [  
> {  
> "nested": {  
> "path": "rtext",  
> "query": {  
> "bool": {  
> "must": [  
> {  
> "term": {  
> "rtext.textmapid": 1  
> }  
> }  
> ]  
> }  
> }  
> }  
> }  
> ]  
> }  
> }  
> }
> 
> My Query comes back hits: [
> 
> - {
> - \_index: xxx
> - \_type: response
> - \_id: 1
> - \_score: 1
> - fields: {
> - rtext.textvalue: [
> - Jennifer,
> - Adam  
> ]  
> }  
> },
> 
> - {
> - \_index: xxx
> - \_type: response
> - \_id: 1
> - \_score: 1
> - fields: {
> - rtext.textvalue: [
> - John,
> - A  
> ]  
> }  
> }
> 
> Since nested query/filter doesn't support fields how do I get just  
> textvalues whoose textmapid is 1 My expected output is Jennifer, John.
> 
> Any help is appreciated,  
> Abhishek

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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:** [July 6, 2017, 2:20am UTC](https://discuss.elastic.co/t/fields-in-the-nested-filter/13276/3 "2017-07-06T02:20:25Z")

</div>


