Boolean must/should only when a field exists

My documents:

{
"id": 1,
"title": "Document One",
"type": "article",
"author": "Nick"
},
{
"id": 2,
"title": "Document Two",
"type": "article",
"author": "David"
},
{
"id": 3,
"title": "Document Three",
"type": "case_study",
// no author
}

I'd like to search the following the equivalent as pseudo-SQL:

(type is "article" OR "case_study") AND (title contains "document") AND (author
is "Nick" if author IS NOT NULL)

My predicament is that the author field is not present in all documents.
For documents when it is not present, I want to ignore this condition; but
honour the condition when it is present. So in this example documents 1 and
3 would be returned.

I started by writing this as a boolean MUST:

{
"bool": {
"must": [
"term": {
"type": ["article", "case_study"]
},
"term": {
"title": "document"
},
"term": {
"author": "Nick"
}
]
}
}

However this would return document 1 only.

Adding "author" as a should instead:

{
"bool": {
"must": [
"term": {
"type": ["article", "case_study"]
},
"term": {
"title": "document"
}
],
"should": [
"term": {
"author": "Nick"
}
]
}
}

However this will return documents 1, 2 and 3.

is there a way to search an optional field in this way, so that the "must"
only occurs if the field has a value?

--
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.
For more options, visit https://groups.google.com/groups/opt_out.

Hi Nick,

You can
use http://www.elasticsearch.org/guide/reference/query-dsl/missing-filter/
as part of you boolean query with the existing flag (you'd need to wrap in
in a constant score query). If you in general also want to be able to
search for articles without an author you can use the null_value settings
in your mapping
( http://www.elasticsearch.org/guide/reference/mapping/core-types/ ). This
will basically assign this value to all article which are indexed with no
author. That means you can search on it just as you would any other term.

Cheers,
Boaz

On Tuesday, May 28, 2013 1:07:24 PM UTC+2, Nick Dunn wrote:

My documents:

{
"id": 1,
"title": "Document One",
"type": "article",
"author": "Nick"
},
{
"id": 2,
"title": "Document Two",
"type": "article",
"author": "David"
},
{
"id": 3,
"title": "Document Three",
"type": "case_study",
// no author
}

I'd like to search the following the equivalent as pseudo-SQL:

(type is "article" OR "case_study") AND (title contains "document") AND (author
is "Nick" if author IS NOT NULL)

My predicament is that the author field is not present in all documents.
For documents when it is not present, I want to ignore this condition; but
honour the condition when it is present. So in this example documents 1 and
3 would be returned.

I started by writing this as a boolean MUST:

{
"bool": {
"must": [
"term": {
"type": ["article", "case_study"]
},
"term": {
"title": "document"
},
"term": {
"author": "Nick"
}
]
}
}

However this would return document 1 only.

Adding "author" as a should instead:

{
"bool": {
"must": [
"term": {
"type": ["article", "case_study"]
},
"term": {
"title": "document"
}
],
"should": [
"term": {
"author": "Nick"
}
]
}
}

However this will return documents 1, 2 and 3.

is there a way to search an optional field in this way, so that the "must"
only occurs if the field has a value?

--
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.
For more options, visit https://groups.google.com/groups/opt_out.