# Help with Query String Query

**URL:** <https://discuss.elastic.co/t/help-with-query-string-query/9587>\
**Category:** Elasticsearch\
**Created:** [November 6, 2012, 9:59pm UTC](https://discuss.elastic.co/t/help-with-query-string-query/9587 "2012-11-06T21:59:10Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![gchen](https://avatars.discourse-cdn.com/v4/letter/g/e47774/32.png) [@gchen](https://discuss.elastic.co/u/gchen)\
**Post date:** [November 6, 2012, 9:59pm UTC](https://discuss.elastic.co/t/help-with-query-string-query/9587/1 "2012-11-06T21:59:10Z")

</div>

Hi,

I'm trying to do text search with boolean using QueryString query but it's  
not hehaving as expected.

Let's say i have a field call \*headline \*in my database and i have the  
following two headlines...

headline 1: "some random headline"  
headline 2: "some other headline"

{"query\_string":{  
"default\_field":"headline",  
"query":"some AND random OR other"}  
}

The above query should return both docs right? If not, what is wrong with  
my query?

If i can change the query to "some OR random OR other", then it does return  
both docs, but why didn't the AND work?

Thanks in advance for the help.

--

---

<div class="post-metadata">

**Author:** ![Raffaele\_Sena](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/raffaele_sena/32/1759_2.png) [@Raffaele\_Sena](https://discuss.elastic.co/u/Raffaele_Sena)\
**Post date:** [November 6, 2012, 10:06pm UTC](https://discuss.elastic.co/t/help-with-query-string-query/9587/2 "2012-11-06T22:06:32Z")

</div>

I am not sure AND has a higher priority than OR in Lucene. I would try  
"grouping":

"some AND (random OR other)"

see  
[http://lucene.apache.org/core/old\_versioned\_docs/versions/3\_0\_0/queryparsersyntax.html#Booleanoperators](http://lucene.apache.org/core/old_versioned_docs/versions/3_0_0/queryparsersyntax.html#Booleanoperators)

-- Raffaele

On Tue, Nov 6, 2012 at 1:59 PM, gchen [kevinch84@gmail.com](mailto:kevinch84@gmail.com) wrote:

> Hi,
> 
> I'm trying to do text search with boolean using QueryString query but it's  
> not hehaving as expected.
> 
> Let's say i have a field call \*headline \*in my database and i have the  
> following two headlines...
> 
> headline 1: "some random headline"  
> headline 2: "some other headline"
> 
> {"query\_string":{  
> "default\_field":"headline",  
> "query":"some AND random OR other"}  
> }
> 
> The above query should return both docs right? If not, what is wrong with  
> my query?
> 
> If i can change the query to "some OR random OR other", then it does  
> return both docs, but why didn't the AND work?
> 
> Thanks in advance for the help.
> 
> --

--

---

<div class="post-metadata">

**Author:** ![gchen](https://avatars.discourse-cdn.com/v4/letter/g/e47774/32.png) [@gchen](https://discuss.elastic.co/u/gchen)\
**Post date:** [November 6, 2012, 11:44pm UTC](https://discuss.elastic.co/t/help-with-query-string-query/9587/3 "2012-11-06T23:44:10Z")

</div>

Thanks Raffaele, grouping works. I agree with you, from what I've learn,  
AND has higher precedence, so that means the query _should_ also work  
without grouping right? Unless i'm missing something fundamental?

On Tuesday, November 6, 2012 5:06:36 PM UTC-5, Raffaele Sena wrote:

> I am not sure AND has a higher priority than OR in Lucene. I would try  
> "grouping":
> 
> "some AND (random OR other)"
> 
> see  
> [Apache Lucene - Query Parser Syntax](http://lucene.apache.org/core/old_versioned_docs/versions/3_0_0/queryparsersyntax.html#Booleanoperators)
> 
> -- Raffaele
> 
> On Tue, Nov 6, 2012 at 1:59 PM, gchen \<[kevi...@gmail.com](mailto:kevi...@gmail.com) \<javascript:\>\>wrote:
> 
> > Hi,
> > 
> > I'm trying to do text search with boolean using QueryString query but  
> > it's not hehaving as expected.
> > 
> > Let's say i have a field call \*headline \*in my database and i have the  
> > following two headlines...
> > 
> > headline 1: "some random headline"  
> > headline 2: "some other headline"
> > 
> > {"query\_string":{  
> > "default\_field":"headline",  
> > "query":"some AND random OR other"}  
> > }
> > 
> > The above query should return both docs right? If not, what is wrong  
> > with my query?
> > 
> > If i can change the query to "some OR random OR other", then it does  
> > return both docs, but why didn't the AND work?
> > 
> > Thanks in advance for the help.
> > 
> > --

--

---

<div class="post-metadata">

**Author:** ![radu\_gheorghe](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/radu_gheorghe/32/556_2.png) [@radu\_gheorghe](https://discuss.elastic.co/u/radu_gheorghe)\
**Post date:** [November 7, 2012, 8:39am UTC](https://discuss.elastic.co/t/help-with-query-string-query/9587/4 "2012-11-07T08:39:23Z")

</div>

Hello,

From what I understand by reading these two:

> **[Re: help with boolean expression](http://www.mail-archive.com/java-user@lucene.apache.org/msg00008.html)**

> **[\[LUCENE-1823\] QueryParser with new features for Lucene 3 - ASF Jira](https://issues.apache.org/jira/browse/LUCENE-1823)**

This seems to be an issue with the way Lucene parses queries, which is  
not resolved yet. So I think the best approach here is to use explicit  
grouping through those parenthesis.

## Best regards, Radu

[http://sematext.com/](http://sematext.com/) -- Elasticsearch -- Solr -- Lucene

On Wed, Nov 7, 2012 at 1:44 AM, gchen [kevinch84@gmail.com](mailto:kevinch84@gmail.com) wrote:

> Thanks Raffaele, grouping works. I agree with you, from what I've learn, AND  
> has higher precedence, so that means the query should also work without  
> grouping right? Unless i'm missing something fundamental?
> 
> On Tuesday, November 6, 2012 5:06:36 PM UTC-5, Raffaele Sena wrote:
> 
> > I am not sure AND has a higher priority than OR in Lucene. I would try  
> > "grouping":
> > 
> > "some AND (random OR other)"
> > 
> > see  
> > [Apache Lucene - Query Parser Syntax](http://lucene.apache.org/core/old_versioned_docs/versions/3_0_0/queryparsersyntax.html#Boolean)  
> > operators
> > 
> > -- Raffaele
> > 
> > On Tue, Nov 6, 2012 at 1:59 PM, gchen [kevi...@gmail.com](mailto:kevi...@gmail.com) wrote:
> > 
> > > Hi,
> > > 
> > > I'm trying to do text search with boolean using QueryString query but  
> > > it's not hehaving as expected.
> > > 
> > > Let's say i have a field call headline in my database and i have the  
> > > following two headlines...
> > > 
> > > headline 1: "some random headline"  
> > > headline 2: "some other headline"
> > > 
> > > {"query\_string":{  
> > > "default\_field":"headline",  
> > > "query":"some AND random OR other"}  
> > > }
> > > 
> > > The above query should return both docs right? If not, what is wrong  
> > > with my query?
> > > 
> > > If i can change the query to "some OR random OR other", then it does  
> > > return both docs, but why didn't the AND work?
> > > 
> > > Thanks in advance for the help.
> > > 
> > > --
> 
> --

--

---

<div class="post-metadata">

**Author:** ![Chris\_Male](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/chris_male/32/2607_2.png) [@Chris\_Male](https://discuss.elastic.co/u/Chris_Male)\
**Post date:** [November 7, 2012, 10:31am UTC](https://discuss.elastic.co/t/help-with-query-string-query/9587/5 "2012-11-07T10:31:31Z")

</div>

I agree with Radu here. The operator precedence in the QueryParsers is  
very messy and best handled through brackets.

On Wednesday, November 7, 2012 7:39:26 PM UTC+11, Radu Gheorghe wrote:

> Hello,
> 
> From what I understand by reading these two:  
> [Re: help with boolean expression](http://www.mail-archive.com/java-user@lucene.apache.org/msg00008.html)  
> [[LUCENE-1823] QueryParser with new features for Lucene 3 - ASF JIRA](https://issues.apache.org/jira/browse/LUCENE-1823)
> 
> This seems to be an issue with the way Lucene parses queries, which is  
> not resolved yet. So I think the best approach here is to use explicit  
> grouping through those parenthesis.
> 
> ## Best regards, Radu
> 
> [http://sematext.com/](http://sematext.com/) -- Elasticsearch -- Solr -- Lucene
> 
> On Wed, Nov 7, 2012 at 1:44 AM, gchen \<[kevi...@gmail.com](mailto:kevi...@gmail.com) \<javascript:\>\>  
> wrote:
> 
> > Thanks Raffaele, grouping works. I agree with you, from what I've learn,  
> > AND  
> > has higher precedence, so that means the query should also work without  
> > grouping right? Unless i'm missing something fundamental?
> > 
> > On Tuesday, November 6, 2012 5:06:36 PM UTC-5, Raffaele Sena wrote:
> > 
> > > I am not sure AND has a higher priority than OR in Lucene. I would try  
> > > "grouping":
> > > 
> > > "some AND (random OR other)"
> > > 
> > > see
> 
> [Apache Lucene - Query Parser Syntax](http://lucene.apache.org/core/old_versioned_docs/versions/3_0_0/queryparsersyntax.html#Boolean)
> 
> > > operators
> > > 
> > > -- Raffaele
> > > 
> > > On Tue, Nov 6, 2012 at 1:59 PM, gchen [kevi...@gmail.com](mailto:kevi...@gmail.com) wrote:
> > > 
> > > > Hi,
> > > > 
> > > > I'm trying to do text search with boolean using QueryString query but  
> > > > it's not hehaving as expected.
> > > > 
> > > > Let's say i have a field call headline in my database and i have the  
> > > > following two headlines...
> > > > 
> > > > headline 1: "some random headline"  
> > > > headline 2: "some other headline"
> > > > 
> > > > {"query\_string":{  
> > > > "default\_field":"headline",  
> > > > "query":"some AND random OR other"}  
> > > > }
> > > > 
> > > > The above query should return both docs right? If not, what is wrong  
> > > > with my query?
> > > > 
> > > > If i can change the query to "some OR random OR other", then it does  
> > > > return both docs, but why didn't the AND work?
> > > > 
> > > > Thanks in advance for the help.
> > > > 
> > > > --
> > 
> > --

--

---

<div class="post-metadata">

**Author:** ![gchen](https://avatars.discourse-cdn.com/v4/letter/g/e47774/32.png) [@gchen](https://discuss.elastic.co/u/gchen)\
**Post date:** [November 7, 2012, 9:31pm UTC](https://discuss.elastic.co/t/help-with-query-string-query/9587/6 "2012-11-07T21:31:57Z")

</div>

Thanks for the help everyone!

--

---

<div class="post-metadata">

**Author:** ![simonw\_2](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/simonw_2/32/1130_2.png) [@simonw\_2](https://discuss.elastic.co/u/simonw_2)\
**Post date:** [November 8, 2012, 11:40am UTC](https://discuss.elastic.co/t/help-with-query-string-query/9587/7 "2012-11-08T11:40:34Z")

</div>

Hey folks,

I usually recommend to use prefix operators: "+some +random other"  
this way its pretty clear what is meant and you won't run into weird  
brackets problems.

here [http://java.dzone.com/articles/use-prefix-operators-instead](http://java.dzone.com/articles/use-prefix-operators-instead) is a  
good writeup related to this too

simon

On Wednesday, November 7, 2012 11:31:31 AM UTC+1, Chris Male wrote:

> I agree with Radu here. The operator precedence in the QueryParsers is  
> very messy and best handled through brackets.
> 
> On Wednesday, November 7, 2012 7:39:26 PM UTC+11, Radu Gheorghe wrote:
> 
> > Hello,
> > 
> > From what I understand by reading these two:  
> > [Re: help with boolean expression](http://www.mail-archive.com/java-user@lucene.apache.org/msg00008.html)  
> > [[LUCENE-1823] QueryParser with new features for Lucene 3 - ASF JIRA](https://issues.apache.org/jira/browse/LUCENE-1823)
> > 
> > This seems to be an issue with the way Lucene parses queries, which is  
> > not resolved yet. So I think the best approach here is to use explicit  
> > grouping through those parenthesis.
> > 
> > ## Best regards, Radu
> > 
> > [http://sematext.com/](http://sematext.com/) -- Elasticsearch -- Solr -- Lucene
> > 
> > On Wed, Nov 7, 2012 at 1:44 AM, gchen [kevi...@gmail.com](mailto:kevi...@gmail.com) wrote:
> > 
> > > Thanks Raffaele, grouping works. I agree with you, from what I've  
> > > learn, AND  
> > > has higher precedence, so that means the query should also work without  
> > > grouping right? Unless i'm missing something fundamental?
> > > 
> > > On Tuesday, November 6, 2012 5:06:36 PM UTC-5, Raffaele Sena wrote:
> > > 
> > > > I am not sure AND has a higher priority than OR in Lucene. I would try  
> > > > "grouping":
> > > > 
> > > > "some AND (random OR other)"
> > > > 
> > > > see
> > 
> > [Apache Lucene - Query Parser Syntax](http://lucene.apache.org/core/old_versioned_docs/versions/3_0_0/queryparsersyntax.html#Boolean)
> > 
> > > > operators
> > > > 
> > > > -- Raffaele
> > > > 
> > > > On Tue, Nov 6, 2012 at 1:59 PM, gchen [kevi...@gmail.com](mailto:kevi...@gmail.com) wrote:
> > > > 
> > > > > Hi,
> > > > > 
> > > > > I'm trying to do text search with boolean using QueryString query but  
> > > > > it's not hehaving as expected.
> > > > > 
> > > > > Let's say i have a field call headline in my database and i have the  
> > > > > following two headlines...
> > > > > 
> > > > > headline 1: "some random headline"  
> > > > > headline 2: "some other headline"
> > > > > 
> > > > > {"query\_string":{  
> > > > > "default\_field":"headline",  
> > > > > "query":"some AND random OR other"}  
> > > > > }
> > > > > 
> > > > > The above query should return both docs right? If not, what is wrong  
> > > > > with my query?
> > > > > 
> > > > > If i can change the query to "some OR random OR other", then it does  
> > > > > return both docs, but why didn't the AND work?
> > > > > 
> > > > > Thanks in advance for the help.
> > > > > 
> > > > > --
> > > 
> > > --

--

---

<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:05am UTC](https://discuss.elastic.co/t/help-with-query-string-query/9587/8 "2017-07-06T03:05:24Z")

</div>


