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.

term.go 1.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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 TermQuery struct {
  22. Term string `json:"term"`
  23. FieldVal string `json:"field,omitempty"`
  24. BoostVal *Boost `json:"boost,omitempty"`
  25. }
  26. // NewTermQuery creates a new Query for finding an
  27. // exact term match in the index.
  28. func NewTermQuery(term string) *TermQuery {
  29. return &TermQuery{
  30. Term: term,
  31. }
  32. }
  33. func (q *TermQuery) SetBoost(b float64) {
  34. boost := Boost(b)
  35. q.BoostVal = &boost
  36. }
  37. func (q *TermQuery) Boost() float64 {
  38. return q.BoostVal.Value()
  39. }
  40. func (q *TermQuery) SetField(f string) {
  41. q.FieldVal = f
  42. }
  43. func (q *TermQuery) Field() string {
  44. return q.FieldVal
  45. }
  46. func (q *TermQuery) Searcher(i index.IndexReader, m mapping.IndexMapping, options search.SearcherOptions) (search.Searcher, error) {
  47. field := q.FieldVal
  48. if q.FieldVal == "" {
  49. field = m.DefaultSearchField()
  50. }
  51. return searcher.NewTermSearcher(i, q.Term, field, q.BoostVal.Value(), options)
  52. }