# How to search for a term, a null value and loading facets

**URL:** <https://discuss.elastic.co/t/how-to-search-for-a-term-a-null-value-and-loading-facets/4303>\
**Category:** Elasticsearch\
**Created:** [April 27, 2011, 1:55pm UTC](https://discuss.elastic.co/t/how-to-search-for-a-term-a-null-value-and-loading-facets/4303 "2011-04-27T13:55:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rubish\_Gupta](https://avatars.discourse-cdn.com/v4/letter/r/45deac/32.png) [@Rubish\_Gupta](https://discuss.elastic.co/u/Rubish_Gupta)\
**Post date:** [April 27, 2011, 1:55pm UTC](https://discuss.elastic.co/t/how-to-search-for-a-term-a-null-value-and-loading-facets/4303/1 "2011-04-27T13:55:28Z")

</div>

Please excuse me if it is too basic, I am just starting with elastic  
search.

I have three fields, fieldA, fieldB, fieldC and I want to search where  
fieldA = "xxx", fieldB is null and facets for fieldC.

I was trying to construct the query step by step.

Step1: get documents where fieldA = "xxx"  
{"query" :  
{ "term" : {"fieldA" : "xxx"}}  
}

Step2: get documents where fieldB is not null to not it later(I  
couldn't find any query in docs checking for null)  
{"query" :  
{ "wildcard" : {"fieldB": "?\*"}}  
}

Step3: combine first two queries  
{"query" :  
{ "term" : {"fieldA" : "xxx"}},  
{ "wildcard" : {"fieldB": "?\*"}}  
}

But this query always fails. I have tried many different combinations,  
but nothing worked. I also wasn't able to get the not query working on  
step two.

Any suggestions on how it should be done would be greatly appreciated.

Thanks.

---

<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:** [April 27, 2011, 2:08pm UTC](https://discuss.elastic.co/t/how-to-search-for-a-term-a-null-value-and-loading-facets/4303/2 "2011-04-27T14:08:31Z")

</div>

Hi Rubish

> I have three fields, fieldA, fieldB, fieldC and I want to search where  
> fieldA = "xxx", fieldB is null and facets for fieldC.

Try this:

curl -XGET '[http://127.0.0.1:9200/\_all/\_search?pretty=1](http://127.0.0.1:9200/_all/_search?pretty=1)' -d '  
{  
"query" : {  
"filtered" : {  
"query" : {  
"term" : {  
"fieldA" : "xxx"  
}  
},  
"filter" : {  
"missing" : {  
"field" : "fieldB"  
}  
}  
}  
},  
"facets" : {  
"field\_c\_facets" : {  
"terms" : {  
"field" : "fieldC"  
}  
}  
}  
}  
'

clint

---

<div class="post-metadata">

**Author:** ![Rubish\_Gupta](https://avatars.discourse-cdn.com/v4/letter/r/45deac/32.png) [@Rubish\_Gupta](https://discuss.elastic.co/u/Rubish_Gupta)\
**Post date:** [April 27, 2011, 2:41pm UTC](https://discuss.elastic.co/t/how-to-search-for-a-term-a-null-value-and-loading-facets/4303/3 "2011-04-27T14:41:09Z")

</div>

Thanks Mate! Just got the {missing} working, now need to get on with facets.  
Cheers!

On Wed, Apr 27, 2011 at 7:38 PM, Clinton Gormley [clinton@iannounce.co.uk](mailto:clinton@iannounce.co.uk)wrote:

> Hi Rubish
> 
> > I have three fields, fieldA, fieldB, fieldC and I want to search where  
> > fieldA = "xxx", fieldB is null and facets for fieldC.
> 
> Try this:
> 
> curl -XGET '[http://127.0.0.1:9200/\_all/\_search?pretty=1](http://127.0.0.1:9200/_all/_search?pretty=1)' -d '  
> {  
> "query" : {  
> "filtered" : {  
> "query" : {  
> "term" : {  
> "fieldA" : "xxx"  
> }  
> },  
> "filter" : {  
> "missing" : {  
> "field" : "fieldB"  
> }  
> }  
> }  
> },  
> "facets" : {  
> "field\_c\_facets" : {  
> "terms" : {  
> "field" : "fieldC"  
> }  
> }  
> }  
> }  
> '
> 
> 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, 4:07am UTC](https://discuss.elastic.co/t/how-to-search-for-a-term-a-null-value-and-loading-facets/4303/4 "2017-07-06T04:07:32Z")

</div>


