# Hiding and showing fields in search results and \_source

**URL:** https://discuss.elastic.co/t/hiding-and-showing-fields-in-search-results-and--source/9043
**Category:** Elasticsearch
**Created:** [September 17, 2012, 7:07pm UTC](https://discuss.elastic.co/t/hiding-and-showing-fields-in-search-results-and--source/9043 "2012-09-17T19:07:49Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)
#### Post date: [September 17, 2012, 7:07pm UTC](https://discuss.elastic.co/t/hiding-and-showing-fields-in-search-results-and--source/9043/1 "2012-09-17T19:07:49Z")

</div>

Hi,

I am trying to hide nested fields per default and only showing them if  
the 'fields' parameter is used in the query - which does not work as I  
thought it would.

First my setup, a standard mapping

{  
"test": {  
"properties": {  
"name": { "type": "string", "index":  
"not\_analyzed", "store" : "yes" },  
"customPrices": { "store" : "yes", "type" : "object" }  
},  
"\_source" : {  
"excludes" : ["customPrices", "name"]  
}  
}  
}

The mapping should define, that anything gets included, except name  
(for testing purposes), which is a flat field and customPrices which  
is a nested object

Now I index some object matching the above criteria

curl -X PUT localhost:9200/test/test/1 -d '{ "name" : "somename" ,  
"foo" :"foo", "bar":"bar", "customPrices" : [ { "name" : "customer1",  
"price" : 22.0}, { "name" : "customer2", "price" : 1000 } ] }'

Match all querying works

curl -X POST localhost:9200/test/test/\_search?pretty=1 -d '{ "query" :  
{ "match\_all" : {}}}'

Returns the source like this: "\_source" : {"foo":"foo","bar":"bar"}

curl -X POST localhost:9200/test/test/\_search?pretty=1 -d '{ "query" :  
{ "match\_all" : {} }, "fields" : ["name"]}'

Does not return the source, but the fields array including the name field

However if I do a similar search for the customer, I fail and do not  
get any fields, if I use one of these searches

curl -X POST localhost:9200/test/test/\_search?pretty=1 -d '{ "query" :  
{ "match\_all" : {} }, "fields" : ["customPrice"]}'  
curl -X POST localhost:9200/test/test/\_search?pretty=1 -d '{ "query" :  
{ "match\_all" : {} }, "fields" : ["customPrice.customer1"]}'  
curl -X POST localhost:9200/test/test/\_search?pretty=1 -d '{ "query" :  
{ "match\_all" : {} }, "fields" : ["\*"]}'

The last query does include the name field but not the customPrices one...

I am sure this is possible, but I am missing something or have a wrong  
mapping configuration 🙂

Any hints greatly appreciated!

--Alexander

--

---

<div class="post-metadata">

### Author: ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)
#### Post date: [September 17, 2012, 7:20pm UTC](https://discuss.elastic.co/t/hiding-and-showing-fields-in-search-results-and--source/9043/2 "2012-09-17T19:20:50Z")

</div>

Hi Alexander

> {  
> "test": {  
> "properties": {  
> "name": { "type": "string", "index":  
> "not\_analyzed", "store" : "yes" },  
> "customPrices": { "store" : "yes", "type" : "object" }

An object type can't be store'd. Only the scalar fields within the  
object.

> I am sure this is possible, but I am missing something or have a wrong  
> mapping configuration 🙂

Perhaps you want to store the whole \_source, but when you query you can  
use partial fields to just return the parts of the source that you want  
to see.

clint

--

---

<div class="post-metadata">

### Author: ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)
#### Post date: [September 17, 2012, 8:06pm UTC](https://discuss.elastic.co/t/hiding-and-showing-fields-in-search-results-and--source/9043/3 "2012-09-17T20:06:44Z")

</div>

Hey Clinton

On Mon, Sep 17, 2012 at 9:20 PM, Clinton Gormley [clint@traveljury.com](mailto:clint@traveljury.com) wrote:

> An object type can't be store'd. Only the scalar fields within the  
> object.  
> Ok. No documentation reading after a full work day anymore... it is  
> pretty obvious 🙂

> Perhaps you want to store the whole \_source, but when you query you can  
> use partial fields to just return the parts of the source that you want  
> to see.  
> Good point about storing the whole source. However using partial  
> fields still returns the whole source plus the partial fields if I  
> recall correctly from my last tests...

--Alexander

--

---

<div class="post-metadata">

### Author: ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)
#### Post date: [September 18, 2012, 9:24am UTC](https://discuss.elastic.co/t/hiding-and-showing-fields-in-search-results-and--source/9043/4 "2012-09-18T09:24:02Z")

</div>

> > Perhaps you want to store the whole \_source, but when you query you can  
> > use partial fields to just return the parts of the source that you want  
> > to see.  
> > Good point about storing the whole source. However using partial  
> > fields still returns the whole source plus the partial fields if I  
> > recall correctly from my last tests...

No. If you request any field (or partial field) then it only returns the  
\_source if you specifically request it.

clint

--

---

<div class="post-metadata">

### Author: ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)
#### Post date: [September 20, 2012, 9:36pm UTC](https://discuss.elastic.co/t/hiding-and-showing-fields-in-search-results-and--source/9043/5 "2012-09-20T21:36:47Z")

</div>

Hi Clinton

On Tue, Sep 18, 2012 at 11:24 AM, Clinton Gormley [clint@traveljury.com](mailto:clint@traveljury.com) wrote:

> > Good point about storing the whole source. However using partial  
> > fields still returns the whole source plus the partial fields if I  
> > recall correctly from my last tests...  
> > No. If you request any field (or partial field) then it only returns the  
> > \_source if you specifically request it.  
> > If I request a single field, which is not in the source (excluded via  
> > mapping config), the \_source will not be returned.  
> > If I request a single field, which excluded from the source plus a  
> > field being stored in it, I'll get back the whole source.

Perhaps this is a bug, I'm not sure. I can provide a minimal sample if needed.

--Alexander

--

---

<div class="post-metadata">

### Author: ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)
#### Post date: [September 21, 2012, 9:09am UTC](https://discuss.elastic.co/t/hiding-and-showing-fields-in-search-results-and--source/9043/6 "2012-09-21T09:09:15Z")

</div>

> Perhaps this is a bug, I'm not sure. I can provide a minimal sample if needed.

Please do

clint

--

---

<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, 3:12am UTC](https://discuss.elastic.co/t/hiding-and-showing-fields-in-search-results-and--source/9043/7 "2017-07-06T03:12:01Z")

</div>


