2014-07-11 20:49:59 +02:00
|
|
|
// Copyright (c) 2014 Couchbase, Inc.
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
|
|
|
|
// except in compliance with the License. You may obtain a copy of the License at
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
// Unless required by applicable law or agreed to in writing, software distributed under the
|
|
|
|
// License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
|
|
|
// either express or implied. See the License for the specific language governing permissions
|
|
|
|
// and limitations under the License.
|
2014-08-29 20:18:36 +02:00
|
|
|
|
2016-09-20 03:12:36 +02:00
|
|
|
package query
|
2014-07-11 20:49:59 +02:00
|
|
|
|
|
|
|
import (
|
2014-09-12 23:21:35 +02:00
|
|
|
"github.com/blevesearch/bleve/index"
|
2016-09-18 15:33:18 +02:00
|
|
|
"github.com/blevesearch/bleve/mapping"
|
2014-08-28 21:38:57 +02:00
|
|
|
"github.com/blevesearch/bleve/search"
|
2014-07-11 20:49:59 +02:00
|
|
|
)
|
|
|
|
|
2016-09-20 03:12:36 +02:00
|
|
|
type QueryStringQuery struct {
|
2016-09-23 01:49:31 +02:00
|
|
|
Query string `json:"query"`
|
|
|
|
Boost *Boost `json:"boost,omitempty"`
|
2014-07-30 18:30:38 +02:00
|
|
|
}
|
|
|
|
|
2014-08-31 16:55:22 +02:00
|
|
|
// NewQueryStringQuery creates a new Query used for
|
|
|
|
// finding documents that satisfy a query string. The
|
|
|
|
// query string is a small query language for humans.
|
2016-09-20 03:12:36 +02:00
|
|
|
func NewQueryStringQuery(query string) *QueryStringQuery {
|
|
|
|
return &QueryStringQuery{
|
2016-09-23 01:49:31 +02:00
|
|
|
Query: query,
|
2014-07-30 18:30:38 +02:00
|
|
|
}
|
2014-07-11 20:49:59 +02:00
|
|
|
}
|
|
|
|
|
2016-09-23 01:49:31 +02:00
|
|
|
func (q *QueryStringQuery) SetBoost(b float64) {
|
|
|
|
boost := Boost(b)
|
|
|
|
q.Boost = &boost
|
2014-07-30 18:30:38 +02:00
|
|
|
}
|
|
|
|
|
2016-09-20 03:12:36 +02:00
|
|
|
func (q *QueryStringQuery) Searcher(i index.IndexReader, m mapping.IndexMapping, explain bool) (search.Searcher, error) {
|
2016-04-22 23:12:10 +02:00
|
|
|
newQuery, err := parseQuerySyntax(q.Query)
|
2014-07-11 20:49:59 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-09-12 23:21:35 +02:00
|
|
|
return newQuery.Searcher(i, m, explain)
|
2014-07-11 20:49:59 +02:00
|
|
|
}
|
|
|
|
|
2016-09-20 03:12:36 +02:00
|
|
|
func (q *QueryStringQuery) Validate() error {
|
2016-04-22 23:12:10 +02:00
|
|
|
newQuery, err := parseQuerySyntax(q.Query)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-09-23 01:49:31 +02:00
|
|
|
if newQuery, ok := newQuery.(ValidatableQuery); ok {
|
|
|
|
return newQuery.Validate()
|
|
|
|
}
|
|
|
|
return nil
|
2014-08-30 00:14:12 +02:00
|
|
|
}
|