# Text query

**URL:** https://discuss.elastic.co/t/text-query/4799
**Category:** Elasticsearch
**Created:** [July 7, 2011, 1:57am UTC](https://discuss.elastic.co/t/text-query/4799 "2011-07-07T01:57:00Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Owen\_Coutts](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/owen_coutts/32/3179_2.png) [@Owen\_Coutts](https://discuss.elastic.co/u/Owen_Coutts)
#### Post date: [July 7, 2011, 1:57am UTC](https://discuss.elastic.co/t/text-query/4799/1 "2011-07-07T01:57:00Z")

</div>

Hi,

I'm running the following query:

{  
"filter": {  
"range": {  
"time\_upload": {  
"to": "2011-01-19",  
"from": "2011-01-16"  
}  
}  
},  
"query": {  
"bool": {  
"must": [  
{  
"text": {  
"report\_type": "application-functional"  
}  
},  
{  
"text": {  
"application\_locale": "en-US"  
}  
}  
]  
}  
}  
}

I would expect that if there were no documents where there was a match for  
one of the must queries this would return no documents yet that is not the  
behaviour that I see. Any advice?

Thanks,  
Owen

--  
Owen Coutts  
University of Waterloo

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [July 7, 2011, 3:50am UTC](https://discuss.elastic.co/t/text-query/4799/2 "2011-07-07T03:50:57Z")

</div>

I suggest trying to make 2 "must" queries and not one "must" query within 2 queries.

Something like :

> ```
> "query": {
> "bool": {
> "must": { },
> "must": { }}}
> 
> ```

hope this helps  
David 😉

Le 7 juil. 2011 à 03:57, Owen Coutts [owen@owencoutts.com](mailto:owen@owencoutts.com) a écrit :

> Hi,
> 
> I'm running the following query:
> 
> {  
> "filter": {  
> "range": {  
> "time\_upload": {  
> "to": "2011-01-19",  
> "from": "2011-01-16"  
> }  
> }  
> },  
> "query": {  
> "bool": {  
> "must": [  
> {  
> "text": {  
> "report\_type": "application-functional"  
> }  
> },  
> {  
> "text": {  
> "application\_locale": "en-US"  
> }  
> }  
> ]  
> }  
> }  
> }
> 
> I would expect that if there were no documents where there was a match for one of the must queries this would return no documents yet that is not the behaviour that I see. Any advice?
> 
> Thanks,  
> Owen
> 
> --  
> Owen Coutts  
> University of Waterloo

---

<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: [July 7, 2011, 9:29am UTC](https://discuss.elastic.co/t/text-query/4799/3 "2011-07-07T09:29:56Z")

</div>

On Thu, 2011-07-07 at 05:50 +0200, David Pilato wrote:

> I suggest trying to make 2 "must" queries and not one "must" query within 2 queries.
> 
> Something like :
> 
> > ```
> > "query": {
> > "bool": {
> > "must": { },
> > "must": { }}}
> > 
> > ```

This syntax is incorrect. In JSON, the second "must" key would override  
the first (although this form may actually work in ES because it reads  
JSON as a stream).

Owen, a couple of comments about your query:

> > {  
> > "filter": {  
> > "range": {  
> > "time\_upload": {  
> > "to": "2011-01-19",  
> > "from": "2011-01-16"  
> > }  
> > }  
> > },

First, unless you specifically want to produce facets based on the  
unfiltered results of your query, and then filter the results with the  
above filter, you should be using a filtered query instead, ie:

{ query: {  
filtered: {  
query: { bool: {.....}},  
filter: { range: {.....}  
}  
}

Second, what mapping do 'report\_type' and 'application\_locale' have?  
Did you set them to be {index: not\_analyzed} ?

If not, they will automatically be considered to be full text, and thus  
analyzed. Then your "text" query will do a full text search on them, ie  
look for ("application" or "functional") and ("en" or "us").

If you set them to be not\_analyzed, then the "text" query will look for  
EXACTLY the term "en-US", which seems to be what you want.

Thirdly, there is no relevance in this query ie you're not looking for  
the document that best matches "interesting treatise on the query dsl"

Your documents either match, or they don't. It's boolean: true or false.

In this case, it would be better to use filters rather than a bool  
query, so your query would be better rewritten as:

{  
"query" : {  
"constant\_score" : {  
"filter" : {  
"and" : [  
{  
"term" : {  
"application\_locale" : "en\_US"  
}  
},  
{  
"term" : {  
"report\_type" : "application-functional"  
}  
},  
{  
"numeric\_range" : {  
"time\_upload" : {  
"lte" : "2011-01-16",  
"gte" : "2011-01-19"  
}  
}  
}  
]  
}  
}  
}  
}

Notes:

1. The above query won't work until you change the mapping of the  
application\_locale and report\_type fields to not\_analyzed, and reindex  
your data.

2. I'm guessing that your 'time\_upload' is a date-time field, with  
potentially very many values. In this case, it is better to use a  
numeric\_range filter instead of a range filter.

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:01am UTC](https://discuss.elastic.co/t/text-query/4799/4 "2017-07-06T04:01:29Z")

</div>


