# Query with OR and AND clauses

**URL:** <https://discuss.elastic.co/t/query-with-or-and-and-clauses/48657>\
**Category:** Elasticsearch\
**Created:** [April 28, 2016, 9:41am UTC](https://discuss.elastic.co/t/query-with-or-and-and-clauses/48657 "2016-04-28T09:41:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![fallenstorm](https://avatars.discourse-cdn.com/v4/letter/f/53a042/32.png) [@fallenstorm](https://discuss.elastic.co/u/fallenstorm)\
**Post date:** [April 28, 2016, 9:41am UTC](https://discuss.elastic.co/t/query-with-or-and-and-clauses/48657/1 "2016-04-28T09:41:01Z")

</div>

I want to realize query like this:

(span\_near or (span\_near and span\_near) or ...).

I have tried:  
\> "span\_or": {

> ```
> clauses: [
> {
> "span_multi": {
> "match": {
> "regexp": {
> "message": "ttt"
> }
> }
> }
> }
> ,
> {
> "span_multi": {
> "match": {
> "bool": {
> "must": [
> {
> "term" : { "message" : "test" }
> },
> {
> "term" : { "message" : "rrr" }
> }
> ]
> }
> }
> }
> }
> ]
> }
> 
> ```

but it returns me a error:

> spanMultiTerm [match] must be of type multi term query

---

<div class="post-metadata">

**Author:** ![cbuescher](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cbuescher/32/60402_2.png) [@cbuescher](https://discuss.elastic.co/u/cbuescher)\
**Post date:** [April 28, 2016, 10:12am UTC](https://discuss.elastic.co/t/query-with-or-and-and-clauses/48657/2 "2016-04-28T10:12:56Z")

</div>

Hi,

as mentioned [here](https://www.elastic.co/guide/en/elasticsearch/reference/current/span-queries.html) span\_multi can only wrap `term`, `range`, `prefix`, `wildcard`, `regexp` or `fuzzy` query. Why do you need `span_multi` in the second clause? Can't you simply use `span_term` query and combine them in a `span_near`?

---

<div class="post-metadata">

**Author:** ![fallenstorm](https://avatars.discourse-cdn.com/v4/letter/f/53a042/32.png) [@fallenstorm](https://discuss.elastic.co/u/fallenstorm)\
**Post date:** [April 28, 2016, 10:30am UTC](https://discuss.elastic.co/t/query-with-or-and-and-clauses/48657/3 "2016-04-28T10:30:40Z")

</div>

I need query:

> (span\_near or (span\_near and span\_near) or ...).

Something like this:

> "span\_or": {  
> clauses: [  
> {  
> "span\_near": {... }  
> }  
> ,  
> {  
> "span\_and": {  
> "clauses": [  
> {  
> "span\_near": {...}  
> }  
> ,  
> {  
> "span\_near": {...}  
> }  
> ,  
> ...  
> ]  
> }  
> }  
> ]  
> }

---

<div class="post-metadata">

**Author:** ![cbuescher](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cbuescher/32/60402_2.png) [@cbuescher](https://discuss.elastic.co/u/cbuescher)\
**Post date:** [April 28, 2016, 1:31pm UTC](https://discuss.elastic.co/t/query-with-or-and-and-clauses/48657/4 "2016-04-28T13:31:51Z")

</div>

Hi,  
instead if you combine two `span_near` queries with a large slop inside another `span_near` query you might get what you need. Its hard to tell because your use case still isn't quiet clear to me. I wonder why you need all that and/or logic with span-queries. Can't you simply use one or more `bool` queries at the top level and then use the individual `span_near` queries inside it?

---

<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 5, 2017, 10:55pm UTC](https://discuss.elastic.co/t/query-with-or-and-and-clauses/48657/5 "2017-07-05T22:55:33Z")

</div>


