# Complex query

**URL:** <https://discuss.elastic.co/t/complex-query/17557>\
**Category:** Elasticsearch\
**Created:** [May 16, 2014, 1:22pm UTC](https://discuss.elastic.co/t/complex-query/17557 "2014-05-16T13:22:20Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ElasticRookie](https://avatars.discourse-cdn.com/v4/letter/e/6de8d8/32.png) [@ElasticRookie](https://discuss.elastic.co/u/ElasticRookie)\
**Post date:** [May 16, 2014, 1:22pm UTC](https://discuss.elastic.co/t/complex-query/17557/1 "2014-05-16T13:22:20Z")

</div>

Hello community,  
First, I apologize for my English.

I can not find a complex query.  
I have thes columns :  
_Id : String_  
_Name : String_  
_FirstName : String_  
_ZipCode : String_

If a user perfoms a query : "\*word1 \*.... _wordn_"  
I need to make a query, something like this :

Name contains word1 _OR_  
FirstName contains word1 _OR_  
ZipCode starts with word1  
_AND_  
....  
_AND_  
Name contains wordn _OR_  
FirstName contains wordn _OR_  
ZipCode starts with wordn

i make this query but it's not my real need :

_{_

- "query": {\*
- "bool": {\*
- 

```
 "must": [*

```

- 

```
   {*

```

- 

```
     "query_string": {*

```

- 

```
       "fields": [*

```

- 

```
         "name",*

```

- 

```
         "firstname",*

```

- 

```
         "zipcode"*

```

- 

```
       ],*

```

- 

```
       "query": "*word1*"*

```

- 

```
     }*

```

- 

```
   },*

```

- 

```
   {*

```

- 

```
     "query_string": {*

```

- 

```
       "fields": [*

```

- 

```
         "name",*

```

- 

```
         "firstname",*

```

- 

```
         "zipcode"*

```

- 

```
       ],*

```

- 

```
       "query": "*wordn*"*

```

- 

```
     }*

```

- 

```
   }*

```

- 

```
 ],*

```

- 

```
 "must_not": [],*

```

- 

```
 "should": []*

```

- }\*
- },\*
- "from": 0,\*
- "size": 10,\*
- "sort": [],\*
- "facets": {}\*  
\*} \*

This query is not good because I don't have my condition "zipcode start  
with" but "zipcode contains".

Can someone help me build the right query ?

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/882034da-08c4-4573-ac1a-c3d929ca1154%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/882034da-08c4-4573-ac1a-c3d929ca1154%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![ElasticRookie](https://avatars.discourse-cdn.com/v4/letter/e/6de8d8/32.png) [@ElasticRookie](https://discuss.elastic.co/u/ElasticRookie)\
**Post date:** [May 16, 2014, 2:01pm UTC](https://discuss.elastic.co/t/complex-query/17557/2 "2014-05-16T14:01:41Z")

</div>

Please, I really need help for this query.

I just created this query but it's a very heavy query

{  
"query": {  
"match\_all": {}  
},  
"filter": {  
"and": [  
{  
"or": [  
{  
"query": {  
"query\_string": {  
"query": "_Word1_",  
"default\_field": "name"  
}  
}  
},  
{  
"query": {  
"query\_string": {  
"query": "_Word1_",  
"default\_field": "firstname"  
}  
}  
},  
{  
"query": {  
"query\_string": {  
"query": "Word1\*",  
"default\_field": "zipcode"  
}  
}  
}  
]  
},  
{  
"or": [  
{  
"query": {  
"query\_string": {  
"query": "_Wordn_",  
"default\_field": "name"  
}  
}  
},  
{  
"query": {  
"query\_string": {  
"query": "_Wordn_",  
"default\_field": "firstname"  
}  
}  
},  
{  
"query": {  
"query\_string": {  
"query": "Wordn\*",  
"default\_field": "zipcode"  
}  
}  
}  
]  
}  
]  
}  
}

If i have ten word, i think the query will be very very very long to be  
execute

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/29359521-cada-41de-beac-1b6d786abbdd%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/29359521-cada-41de-beac-1b6d786abbdd%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![ElasticRookie](https://avatars.discourse-cdn.com/v4/letter/e/6de8d8/32.png) [@ElasticRookie](https://discuss.elastic.co/u/ElasticRookie)\
**Post date:** [May 19, 2014, 7:56am UTC](https://discuss.elastic.co/t/complex-query/17557/3 "2014-05-19T07:56:00Z")

</div>

Hello,

Nobody can help me to find a solution for this query ?

I really need help plz.

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/20f5fa82-0e18-45cf-babd-70a00fbb4e6d%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/20f5fa82-0e18-45cf-babd-70a00fbb4e6d%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)\
**Post date:** [May 19, 2014, 9:26pm UTC](https://discuss.elastic.co/t/complex-query/17557/4 "2014-05-19T21:26:16Z")

</div>

I haven't had time to look at your query, but have you looked into  
multi-field queries?

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

--  
Ivan

On Mon, May 19, 2014 at 12:56 AM, ElasticRookie [farhat.bakiri@gmail.com](mailto:farhat.bakiri@gmail.com)wrote:

> Hello,
> 
> Nobody can help me to find a solution for this query ?
> 
> I really need help plz.
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/20f5fa82-0e18-45cf-babd-70a00fbb4e6d%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/20f5fa82-0e18-45cf-babd-70a00fbb4e6d%40googlegroups.com)[https://groups.google.com/d/msgid/elasticsearch/20f5fa82-0e18-45cf-babd-70a00fbb4e6d%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/20f5fa82-0e18-45cf-babd-70a00fbb4e6d%40googlegroups.com?utm_medium=email&utm_source=footer)  
> .
> 
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQCHhe4M%2BTwrWc2x1VUbNweSWr\_%2BrzTNyFEG3rY3MULGkg%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQCHhe4M%2BTwrWc2x1VUbNweSWr_%2BrzTNyFEG3rY3MULGkg%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Andreas\_Jaekle](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/andreas_jaekle/32/1548_2.png) [@Andreas\_Jaekle](https://discuss.elastic.co/u/Andreas_Jaekle)\
**Post date:** [May 20, 2014, 3:54pm UTC](https://discuss.elastic.co/t/complex-query/17557/5 "2014-05-20T15:54:25Z")

</div>

maybe its simpler to make it in just one query\_string like this:

curl '[http://127.0.0.1:9200/data/\_search](http://127.0.0.1:9200/data/_search)' --data  
'{"fields":["_parent","source"],"query":{"bool":{"must": [ {  
"simple\_query\_string": { "query": "(a|b\*) c (x|y)_",  
"default\_operator": "AND" } }  
]}},"from":0,"size":10,"sort":,"facets":{},"version":true}'

query\_string allows really powerful expressions

Am 16.05.2014 15:22, schrieb ElasticRookie:

> Hello community,  
> First, I apologize for my English.
> 
> I can not find a complex query.  
> I have thes columns :  
> _Id : String_  
> _Name : String_  
> _FirstName : String_  
> _ZipCode : String_
> 
> If a user perfoms a query : "\*word1 \*.... _wordn_"  
> I need to make a query, something like this :
> 
> Name contains word1 _OR_  
> FirstName contains word1 _OR_  
> ZipCode starts with word1  
> _AND_  
> ....  
> _AND_  
> Name contains wordn _OR_  
> FirstName contains wordn _OR_  
> ZipCode starts with wordn
> 
> i make this query but it's not my real need :
> 
> /{/  
> / "query": {/  
> / "bool": {/  
> / "must": [/  
> / {/  
> / "query\_string": {/  
> / "fields": [/  
> / "name",/  
> / "firstname",/  
> / "zipcode"/  
> / ],/  
> / "query": "_word1_"/  
> / }/  
> / },/  
> / {/  
> / "query\_string": {/  
> / "fields": [/  
> / "name",/  
> / "firstname",/  
> / "zipcode"/  
> / ],/  
> / "query": "_wordn_"/  
> / }/  
> / }/  
> / ],/  
> / "must\_not": ,/  
> / "should": /  
> / }/  
> / },/  
> / "from": 0,/  
> / "size": 10,/  
> / "sort": ,/  
> / "facets": {}/  
> /} /
> 
> This query is not good because I don't have my condition "zipcode  
> start with" but "zipcode contains".
> 
> ## Can someone help me build the right query ?
> 
> You received this message because you are subscribed to the Google  
> Groups "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send  
> an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com)  
> [mailto:elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/882034da-08c4-4573-ac1a-c3d929ca1154%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/882034da-08c4-4573-ac1a-c3d929ca1154%40googlegroups.com)  
> [https://groups.google.com/d/msgid/elasticsearch/882034da-08c4-4573-ac1a-c3d929ca1154%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/882034da-08c4-4573-ac1a-c3d929ca1154%40googlegroups.com?utm_medium=email&utm_source=footer).  
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/537B7AB1.2010702%40gmail.com](https://groups.google.com/d/msgid/elasticsearch/537B7AB1.2010702%40gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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, 1:28am UTC](https://discuss.elastic.co/t/complex-query/17557/6 "2017-07-06T01:28:04Z")

</div>

Closed on July 6, 2017, 1:28am UTC


