diff --git a/error.go b/error.go index 52d845e3..421fc47e 100644 --- a/error.go +++ b/error.go @@ -16,7 +16,7 @@ const ( ErrorIndexMetaMissing ErrorIndexMetaCorrupt ErrorDisjunctionFewerThanMinClauses - ErrorBooleanQueryNeedsMustOrShould + ErrorBooleanQueryNeedsMustOrShouldOrNotMust ErrorNumericQueryNoBounds ErrorPhraseQueryNoTerms ErrorUnknownQueryType @@ -35,17 +35,17 @@ func (e Error) Error() string { } var errorMessages = map[int]string{ - int(ErrorIndexPathExists): "cannot create new index, path already exists", - int(ErrorIndexPathDoesNotExist): "cannot open index, path does not exist", - int(ErrorIndexMetaMissing): "cannot open index, metadata missing", - int(ErrorIndexMetaCorrupt): "cannot open index, metadata corrupt", - int(ErrorDisjunctionFewerThanMinClauses): "disjunction query has fewer than the minimum number of clauses to satisfy", - int(ErrorBooleanQueryNeedsMustOrShould): "boolean query must contain at least one must or should clause", - int(ErrorNumericQueryNoBounds): "numeric range query must specify min or max", - int(ErrorPhraseQueryNoTerms): "phrase query must contain at least one term", - int(ErrorUnknownQueryType): "unknown query type", - int(ErrorUnknownStorageType): "unknown storage type", - int(ErrorIndexClosed): "index is closed", - int(ErrorAliasMulti): "cannot perform single index operation on multiple index alias", - int(ErrorAliasEmpty): "cannot perform operation on empty alias", + int(ErrorIndexPathExists): "cannot create new index, path already exists", + int(ErrorIndexPathDoesNotExist): "cannot open index, path does not exist", + int(ErrorIndexMetaMissing): "cannot open index, metadata missing", + int(ErrorIndexMetaCorrupt): "cannot open index, metadata corrupt", + int(ErrorDisjunctionFewerThanMinClauses): "disjunction query has fewer than the minimum number of clauses to satisfy", + int(ErrorBooleanQueryNeedsMustOrShouldOrNotMust): "boolean query must contain at least one must or should or not must clause", + int(ErrorNumericQueryNoBounds): "numeric range query must specify min or max", + int(ErrorPhraseQueryNoTerms): "phrase query must contain at least one term", + int(ErrorUnknownQueryType): "unknown query type", + int(ErrorUnknownStorageType): "unknown storage type", + int(ErrorIndexClosed): "index is closed", + int(ErrorAliasMulti): "cannot perform single index operation on multiple index alias", + int(ErrorAliasEmpty): "cannot perform operation on empty alias", } diff --git a/query_boolean.go b/query_boolean.go index bf540148..46dc4db4 100644 --- a/query_boolean.go +++ b/query_boolean.go @@ -96,6 +96,16 @@ func (q *booleanQuery) SetBoost(b float64) Query { func (q *booleanQuery) Searcher(i index.IndexReader, m *IndexMapping, explain bool) (search.Searcher, error) { var err error + var mustNotSearcher search.Searcher + if q.MustNot != nil { + mustNotSearcher, err = q.MustNot.Searcher(i, m, explain) + if err != nil { + return nil, err + } + if q.Must == nil && q.Should == nil { + q.Must = NewMatchAllQuery() + } + } var mustSearcher search.Searcher if q.Must != nil { @@ -112,15 +122,6 @@ func (q *booleanQuery) Searcher(i index.IndexReader, m *IndexMapping, explain bo return nil, err } } - - var mustNotSearcher search.Searcher - if q.MustNot != nil { - mustNotSearcher, err = q.MustNot.Searcher(i, m, explain) - if err != nil { - return nil, err - } - } - return searchers.NewBooleanSearcher(i, mustSearcher, shouldSearcher, mustNotSearcher, explain) } @@ -143,8 +144,8 @@ func (q *booleanQuery) Validate() error { return err } } - if q.Must == nil && q.Should == nil { - return ErrorBooleanQueryNeedsMustOrShould + if q.Must == nil && q.Should == nil && q.MustNot == nil { + return ErrorBooleanQueryNeedsMustOrShouldOrNotMust } return nil } diff --git a/query_test.go b/query_test.go index df0f5a4d..ef51c603 100644 --- a/query_test.go +++ b/query_test.go @@ -192,14 +192,28 @@ func TestQueryValidate(t *testing.T) { nil, nil, []Query{NewMatchQuery("devon").SetField("desc")}), - err: ErrorBooleanQueryNeedsMustOrShould, + err: nil, }, { query: NewBooleanQuery( []Query{}, []Query{}, []Query{NewMatchQuery("devon").SetField("desc")}), - err: ErrorBooleanQueryNeedsMustOrShould, + err: nil, + }, + { + query: NewBooleanQuery( + nil, + nil, + nil), + err: ErrorBooleanQueryNeedsMustOrShouldOrNotMust, + }, + { + query: NewBooleanQuery( + []Query{}, + []Query{}, + []Query{}), + err: ErrorBooleanQueryNeedsMustOrShouldOrNotMust, }, { query: NewBooleanQueryMinShould(