1//  Copyright (c) 2014 Couchbase, Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// 		http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package query
16
17import (
18	"github.com/blevesearch/bleve/index"
19	"github.com/blevesearch/bleve/mapping"
20	"github.com/blevesearch/bleve/search"
21	"github.com/blevesearch/bleve/search/searcher"
22)
23
24type BoolFieldQuery struct {
25	Bool     bool   `json:"bool"`
26	FieldVal string `json:"field,omitempty"`
27	BoostVal *Boost `json:"boost,omitempty"`
28}
29
30// NewBoolFieldQuery creates a new Query for boolean fields
31func NewBoolFieldQuery(val bool) *BoolFieldQuery {
32	return &BoolFieldQuery{
33		Bool: val,
34	}
35}
36
37func (q *BoolFieldQuery) SetBoost(b float64) {
38	boost := Boost(b)
39	q.BoostVal = &boost
40}
41
42func (q *BoolFieldQuery) Boost() float64 {
43	return q.BoostVal.Value()
44}
45
46func (q *BoolFieldQuery) SetField(f string) {
47	q.FieldVal = f
48}
49
50func (q *BoolFieldQuery) Field() string {
51	return q.FieldVal
52}
53
54func (q *BoolFieldQuery) Searcher(i index.IndexReader, m mapping.IndexMapping, options search.SearcherOptions) (search.Searcher, error) {
55	field := q.FieldVal
56	if q.FieldVal == "" {
57		field = m.DefaultSearchField()
58	}
59	term := "F"
60	if q.Bool {
61		term = "T"
62	}
63	return searcher.NewTermSearcher(i, term, field, q.BoostVal.Value(), options)
64}
65