You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

prefix.go 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. package query
  15. import (
  16. "github.com/blevesearch/bleve/index"
  17. "github.com/blevesearch/bleve/mapping"
  18. "github.com/blevesearch/bleve/search"
  19. "github.com/blevesearch/bleve/search/searcher"
  20. )
  21. type PrefixQuery struct {
  22. Prefix string `json:"prefix"`
  23. FieldVal string `json:"field,omitempty"`
  24. BoostVal *Boost `json:"boost,omitempty"`
  25. }
  26. // NewPrefixQuery creates a new Query which finds
  27. // documents containing terms that start with the
  28. // specified prefix.
  29. func NewPrefixQuery(prefix string) *PrefixQuery {
  30. return &PrefixQuery{
  31. Prefix: prefix,
  32. }
  33. }
  34. func (q *PrefixQuery) SetBoost(b float64) {
  35. boost := Boost(b)
  36. q.BoostVal = &boost
  37. }
  38. func (q *PrefixQuery) Boost() float64 {
  39. return q.BoostVal.Value()
  40. }
  41. func (q *PrefixQuery) SetField(f string) {
  42. q.FieldVal = f
  43. }
  44. func (q *PrefixQuery) Field() string {
  45. return q.FieldVal
  46. }
  47. func (q *PrefixQuery) Searcher(i index.IndexReader, m mapping.IndexMapping, options search.SearcherOptions) (search.Searcher, error) {
  48. field := q.FieldVal
  49. if q.FieldVal == "" {
  50. field = m.DefaultSearchField()
  51. }
  52. return searcher.NewTermPrefixSearcher(i, q.Prefix, field, q.BoostVal.Value(), options)
  53. }