# Lucene query strings

**URL:** <https://discuss.elastic.co/t/lucene-query-strings/5113>\
**Category:** Elasticsearch\
**Created:** [August 10, 2011, 2:01pm UTC](https://discuss.elastic.co/t/lucene-query-strings/5113 "2011-08-10T14:01:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mattcollins84](https://avatars.discourse-cdn.com/v4/letter/m/a9adbd/32.png) [@mattcollins84](https://discuss.elastic.co/u/mattcollins84)\
**Post date:** [August 10, 2011, 2:01pm UTC](https://discuss.elastic.co/t/lucene-query-strings/5113/1 "2011-08-10T14:01:48Z")

</div>

Hi all,

I want to user a fairly complex lucene query string with ElasticSearch:

(((isCustomer:1 AND (tagid:1959) AND (locid:14648) AND (portalid:1 OR portalid:2))^50000) OR ((isCustomer:1 AND NOT tagid:1959 AND (catid:L929) AND (locid:14648) AND (portalid:1 OR portalid:2))^25000) OR ((NOT isCustomer:1 AND (catid:L929 OR companyName:hotel) AND (locid:14648) AND (portalid:1 OR portalid:2))^5000) OR ((NOT isCustomer:1 AND (catid:L929 OR companyName:hotel OR hotel) AND (locid:14648) AND (portalid:1 OR portalid:2)))) AND NOT adultContent:true

But this doesn't seem to work (from my bash shell at least!). The error I get is:

-bash: syntax error near unexpected token `('

Can anyone shed any light on how I could go about this? I have tried URL encoding the query, but to no avail (same error).

Thanks,  
Matt

---

<div class="post-metadata">

**Author:** ![vineeth\_mohan](https://avatars.discourse-cdn.com/v4/letter/v/bc79bd/32.png) [@vineeth\_mohan](https://discuss.elastic.co/u/vineeth_mohan)\
**Post date:** [August 10, 2011, 5:30pm UTC](https://discuss.elastic.co/t/lucene-query-strings/5113/2 "2011-08-10T17:30:57Z")

</div>

Can you paste the entire command you executed.

Again this is not a ES issue , its strictly something to do with bash/shell  
scripting.  
I don't think this is the group you need to raise this query.

Thanks  
Vineeth

On Wed, Aug 10, 2011 at 7:31 PM, mattcollins84  
[mattstrikes@googlemail.com](mailto:mattstrikes@googlemail.com)wrote:

> Hi all,
> 
> I want to user a fairly complex lucene query string with Elasticsearch:
> 
> (((isCustomer:1 AND (tagid:1959) AND (locid:14648) AND (portalid:1 OR  
> portalid:2))^50000) OR ((isCustomer:1 AND NOT tagid:1959 AND (catid:L929)  
> AND (locid:14648) AND (portalid:1 OR portalid:2))^25000) OR ((NOT  
> isCustomer:1 AND (catid:L929 OR companyName:hotel) AND (locid:14648) AND  
> (portalid:1 OR portalid:2))^5000) OR ((NOT isCustomer:1 AND (catid:L929 OR  
> companyName:hotel OR hotel) AND (locid:14648) AND (portalid:1 OR  
> portalid:2)))) AND NOT adultContent:true
> 
> But this doesn't seem to work (from my bash shell at least!). The error I  
> get is:
> 
> -bash: syntax error near unexpected token `('
> 
> Can anyone shed any light on how I could go about this? I have tried URL  
> encoding the query, but to no avail (same error).
> 
> Thanks,  
> Matt
> 
> --  
> View this message in context:  
> [http://elasticsearch-users.115913.n3.nabble.com/Lucene-query-strings-tp3242645p3242645.html](http://elasticsearch-users.115913.n3.nabble.com/Lucene-query-strings-tp3242645p3242645.html)  
> Sent from the Elasticsearch Users mailing list archive at [Nabble.com](http://Nabble.com).

---

<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:** [August 11, 2011, 8:51am UTC](https://discuss.elastic.co/t/lucene-query-strings/5113/3 "2011-08-11T08:51:24Z")

</div>

Hi Matt

> I want to user a fairly complex lucene query string with Elasticsearch:
> 
> (((isCustomer:1 AND (tagid:1959) AND (locid:14648) AND (portalid:1 OR  
> portalid:2))^50000) OR ((isCustomer:1 AND NOT tagid:1959 AND (catid:L929)  
> AND (locid:14648) AND (portalid:1 OR portalid:2))^25000) OR ((NOT  
> isCustomer:1 AND (catid:L929 OR companyName:hotel) AND (locid:14648) AND  
> (portalid:1 OR portalid:2))^5000) OR ((NOT isCustomer:1 AND (catid:L929 OR  
> companyName:hotel OR hotel) AND (locid:14648) AND (portalid:1 OR  
> portalid:2)))) AND NOT adultContent:true

Besides the error you're getting in bash, I really recommend that you  
don't try to use the lucene query syntax for queries this complex. They  
become impossible to read and debug.

Much better to use the query DSL for this.

I've tried to analyze the logic of your query above:

## isCustomer tagid catid/companyName/text locid portalid adultContent boost

1 1959 -- 14648 1 or 2 false 50000  
1 !1959 -- 14648 1 or 2 false 25000  
!1 -- L929/hotel/-- 14648 1 or 2 false 5000  
!1 -- L929/hotel/text 14648 1 or 2 false --

First thing to note is that the last line doesn't make sense. It's the  
same as the line before, but MORE specific.

Second, locid, portalid and adultContent always have the same value, and  
don't affect the scoring.

I've mocked up how you could run this query using the  
custom\_filters\_score query. This may not be exactly right, as I don't  
have all the details of your data, but should get you started:

> <https://gist.github.com/clintongormley/1139183>

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:57am UTC](https://discuss.elastic.co/t/lucene-query-strings/5113/4 "2017-07-06T03:57:30Z")

</div>


