# Boolean must/should where field is optional in some documents

**URL:** https://discuss.elastic.co/t/boolean-must-should-where-field-is-optional-in-some-documents/12145
**Category:** Elasticsearch
**Created:** [May 28, 2013, 9:31am UTC](https://discuss.elastic.co/t/boolean-must-should-where-field-is-optional-in-some-documents/12145 "2013-05-28T09:31:25Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![Nick\_Dunn](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nick_dunn/32/2315_2.png) [@Nick\_Dunn](https://discuss.elastic.co/u/Nick_Dunn)
#### Post date: [May 28, 2013, 9:31am UTC](https://discuss.elastic.co/t/boolean-must-should-where-field-is-optional-in-some-documents/12145/1 "2013-05-28T09:31:25Z")

</div>

I can't quite get my head around a specific search requirement I have, so I  
hope someone can help.

My documents have a field "published" with a string value "yes" or "no"  
(not boolean true/false, indexed data out of my control at present!). This  
defines whether the document has been published or not, obviously. I have a  
fairly large boolean query of a series of "must" sub-queries, of which one  
is on this published field, ensuring that only those "yes" are returned.

However I've just realised that this field is _optional_ in the document.  
If it doesn't have a yes/no value, then the assumption is that the document  
is published ("yes") and should be returned. So in essence I want to add a  
must clause for this field _only when it exists and has a value_. Is this  
even possible? Should I perhaps use a filter here?

Any ideas gratefully received.

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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 29, 2013, 9:11pm UTC](https://discuss.elastic.co/t/boolean-must-should-where-field-is-optional-in-some-documents/12145/2 "2013-05-29T21:11:33Z")

</div>

You can work around your problem by using the exists and/or missing filter.

If the default for the published field is "yes, then you can use an Or  
filter with one clause being a term filter for "yes" and the other a  
missing filter.

> **[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.

> **[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 Tue, May 28, 2013 at 2:31 AM, Nick Dunn [nick@nick-dunn.co.uk](mailto:nick@nick-dunn.co.uk) wrote:

> I can't quite get my head around a specific search requirement I have, so  
> I hope someone can help.
> 
> My documents have a field "published" with a string value "yes" or "no"  
> (not boolean true/false, indexed data out of my control at present!). This  
> defines whether the document has been published or not, obviously. I have a  
> fairly large boolean query of a series of "must" sub-queries, of which one  
> is on this published field, ensuring that only those "yes" are returned.
> 
> However I've just realised that this field is _optional_ in the document.  
> If it doesn't have a yes/no value, then the assumption is that the document  
> is published ("yes") and should be returned. So in essence I want to add a  
> must clause for this field _only when it exists and has a value_. Is this  
> even possible? Should I perhaps use a filter here?
> 
> Any ideas gratefully received.
> 
> --  
> 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).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

### Author: ![javierco](https://avatars.discourse-cdn.com/v4/letter/j/b77776/32.png) [@javierco](https://discuss.elastic.co/u/javierco)
#### Post date: [March 1, 2016, 3:36pm UTC](https://discuss.elastic.co/t/boolean-must-should-where-field-is-optional-in-some-documents/12145/3 "2016-03-01T15:36:59Z")

</div>

Hi, I found this old posting but it is now relevant with the new Boolean Queries in es 2.x. I have a similar situation where I need to search for documents where a specific field may be missing, but if it is present, then it needs to have an specific value. My old query using OR (deprecated) looks like this:

```
{
  "query": {
    "filtered": {
      "query": {
        "query_string": {
          "query": "(title:mykeyword body:mykeyword)"
        }
      },
      "filter": {
        "or": {
          "filters": [
            {
              "missing": {
                "field": "bookId"
              }
            },
            {
              "term": {
                "bookId": 2
              }
            }
          ]
        }
      }
    }
  }
}

```

How can I replicate this same behavior with a boolean query that does not have OR clauses?

Thanks a lot!

Javier C.

---

<div class="post-metadata">

### Author: ![javierco](https://avatars.discourse-cdn.com/v4/letter/j/b77776/32.png) [@javierco](https://discuss.elastic.co/u/javierco)
#### Post date: [March 1, 2016, 3:44pm UTC](https://discuss.elastic.co/t/boolean-must-should-where-field-is-optional-in-some-documents/12145/4 "2016-03-01T15:44:26Z")

</div>

Update: I tried the following, but it does not work ;(

```
{
  "bool" : {
    "must" : {
      "query_string" : {
        "query": "(title:mykeyword body:mykeyword)"
      }
    },
    "filter" : {
      "bool" : {
        "must" : {
          "term" : {
            "bookId" : 2
          }
        },
        "must_not" : {
          "exists" : {
            "field" : "bookId"
          }
        }
      }
    }
  }
}
```

---

<div class="post-metadata">

### Author: ![Camilo\_Sierra](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/camilo_sierra/32/14397_2.png) [@Camilo\_Sierra](https://discuss.elastic.co/u/Camilo_Sierra)
#### Post date: [March 1, 2016, 4:43pm UTC](https://discuss.elastic.co/t/boolean-must-should-where-field-is-optional-in-some-documents/12145/5 "2016-03-01T16:43:25Z")

</div>

hope it helps !

```
  {
  "query": {
    "bool": {
      "must": {
        "query_string": {
          "query": "(title:mykeyword body:mykeyword)"
        }
      },
      "filter": {
        "bool": {
          "should": [
            {
              "missing": {
                "field": "bookId"
              }
            },
            {
              "term": {
                "bookId": 2
              }
            }
          ]
        }
      }
    }
  }
}
```

---

<div class="post-metadata">

### Author: ![javierco](https://avatars.discourse-cdn.com/v4/letter/j/b77776/32.png) [@javierco](https://discuss.elastic.co/u/javierco)
#### Post date: [March 1, 2016, 5:19pm UTC](https://discuss.elastic.co/t/boolean-must-should-where-field-is-optional-in-some-documents/12145/6 "2016-03-01T17:19:48Z")

</div>

Thanks @Camilo_Sierra but the "missing" query is also deprecated in es 2.2 (Deprecated in 2.2.0. Use exists query inside a must\_not clause instead.) So, based on your example, I tried something like this:

```
{
  "query": {
    "bool": {
      "must": {
        "query_string": {
          "query": "(title:mykeyword body:mykeyword)"
        }
      },
      "filter": {
        "bool": {
          "should": [
            {
              "must_not": {
                "exists": {
                  "field": "bookId"
                }
              }
            },
            {
              "term": {
                "bookId": 2
              }
            }
          ]
        }
      }
    }
  }
}

```

and it gave me this error:

`QueryParsingException[No query registered for [must_not]]`

Thanks again!

---

<div class="post-metadata">

### Author: ![Camilo\_Sierra](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/camilo_sierra/32/14397_2.png) [@Camilo\_Sierra](https://discuss.elastic.co/u/Camilo_Sierra)
#### Post date: [March 1, 2016, 5:22pm UTC](https://discuss.elastic.co/t/boolean-must-should-where-field-is-optional-in-some-documents/12145/7 "2016-03-01T17:22:14Z")

</div>

need to use bool 😉

```
{
	"query": {
		"bool": {
			"must": {
				"query_string": {
					"query": "(title:mykeyword body:mykeyword)"
				}
			},
			"filter": {
				"bool": {
					"should": [{
						"bool": {
							"must_not": {
								"exists": {
									"field": "bookId"
								}
							}
						}
					}, {
						"term": {
							"bookId": 2
						}
					}]
				}
			}
		}
	}
}
```

---

<div class="post-metadata">

### Author: ![javierco](https://avatars.discourse-cdn.com/v4/letter/j/b77776/32.png) [@javierco](https://discuss.elastic.co/u/javierco)
#### Post date: [March 1, 2016, 5:26pm UTC](https://discuss.elastic.co/t/boolean-must-should-where-field-is-optional-in-some-documents/12145/8 "2016-03-01T17:26:05Z")

</div>

And... it worked! 🙂

Thanks a lot!!!

Javier C.

---

<div class="post-metadata">

### Author: ![Camilo\_Sierra](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/camilo_sierra/32/14397_2.png) [@Camilo\_Sierra](https://discuss.elastic.co/u/Camilo_Sierra)
#### Post date: [March 1, 2016, 5:28pm UTC](https://discuss.elastic.co/t/boolean-must-should-where-field-is-optional-in-some-documents/12145/9 "2016-03-01T17:28:16Z")

</div>

awesome!!

---

<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, 11:12pm UTC](https://discuss.elastic.co/t/boolean-must-should-where-field-is-optional-in-some-documents/12145/10 "2017-07-05T23:12:24Z")

</div>


