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.

87 lines
2.6 KiB

  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. "fmt"
  17. "github.com/blevesearch/bleve/index"
  18. "github.com/blevesearch/bleve/mapping"
  19. "github.com/blevesearch/bleve/search"
  20. "github.com/blevesearch/bleve/search/searcher"
  21. )
  22. type NumericRangeQuery struct {
  23. Min *float64 `json:"min,omitempty"`
  24. Max *float64 `json:"max,omitempty"`
  25. InclusiveMin *bool `json:"inclusive_min,omitempty"`
  26. InclusiveMax *bool `json:"inclusive_max,omitempty"`
  27. FieldVal string `json:"field,omitempty"`
  28. BoostVal *Boost `json:"boost,omitempty"`
  29. }
  30. // NewNumericRangeQuery creates a new Query for ranges
  31. // of numeric values.
  32. // Either, but not both endpoints can be nil.
  33. // The minimum value is inclusive.
  34. // The maximum value is exclusive.
  35. func NewNumericRangeQuery(min, max *float64) *NumericRangeQuery {
  36. return NewNumericRangeInclusiveQuery(min, max, nil, nil)
  37. }
  38. // NewNumericRangeInclusiveQuery creates a new Query for ranges
  39. // of numeric values.
  40. // Either, but not both endpoints can be nil.
  41. // Control endpoint inclusion with inclusiveMin, inclusiveMax.
  42. func NewNumericRangeInclusiveQuery(min, max *float64, minInclusive, maxInclusive *bool) *NumericRangeQuery {
  43. return &NumericRangeQuery{
  44. Min: min,
  45. Max: max,
  46. InclusiveMin: minInclusive,
  47. InclusiveMax: maxInclusive,
  48. }
  49. }
  50. func (q *NumericRangeQuery) SetBoost(b float64) {
  51. boost := Boost(b)
  52. q.BoostVal = &boost
  53. }
  54. func (q *NumericRangeQuery) Boost() float64{
  55. return q.BoostVal.Value()
  56. }
  57. func (q *NumericRangeQuery) SetField(f string) {
  58. q.FieldVal = f
  59. }
  60. func (q *NumericRangeQuery) Field() string{
  61. return q.FieldVal
  62. }
  63. func (q *NumericRangeQuery) Searcher(i index.IndexReader, m mapping.IndexMapping, explain bool) (search.Searcher, error) {
  64. field := q.FieldVal
  65. if q.FieldVal == "" {
  66. field = m.DefaultSearchField()
  67. }
  68. return searcher.NewNumericRangeSearcher(i, q.Min, q.Max, q.InclusiveMin, q.InclusiveMax, field, q.BoostVal.Value(), explain)
  69. }
  70. func (q *NumericRangeQuery) Validate() error {
  71. if q.Min == nil && q.Min == q.Max {
  72. return fmt.Errorf("numeric range query must specify min or max")
  73. }
  74. return nil
  75. }