本站源代码
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.

96 lines
2.7KB

  1. // Copyright (c) 2017 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 TermRangeQuery struct {
  23. Min string `json:"min,omitempty"`
  24. Max string `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. // NewTermRangeQuery creates a new Query for ranges
  31. // of text term values.
  32. // Either, but not both endpoints can be nil.
  33. // The minimum value is inclusive.
  34. // The maximum value is exclusive.
  35. func NewTermRangeQuery(min, max string) *TermRangeQuery {
  36. return NewTermRangeInclusiveQuery(min, max, nil, nil)
  37. }
  38. // NewTermRangeInclusiveQuery 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 NewTermRangeInclusiveQuery(min, max string, minInclusive, maxInclusive *bool) *TermRangeQuery {
  43. return &TermRangeQuery{
  44. Min: min,
  45. Max: max,
  46. InclusiveMin: minInclusive,
  47. InclusiveMax: maxInclusive,
  48. }
  49. }
  50. func (q *TermRangeQuery) SetBoost(b float64) {
  51. boost := Boost(b)
  52. q.BoostVal = &boost
  53. }
  54. func (q *TermRangeQuery) Boost() float64 {
  55. return q.BoostVal.Value()
  56. }
  57. func (q *TermRangeQuery) SetField(f string) {
  58. q.FieldVal = f
  59. }
  60. func (q *TermRangeQuery) Field() string {
  61. return q.FieldVal
  62. }
  63. func (q *TermRangeQuery) Searcher(i index.IndexReader, m mapping.IndexMapping, options search.SearcherOptions) (search.Searcher, error) {
  64. field := q.FieldVal
  65. if q.FieldVal == "" {
  66. field = m.DefaultSearchField()
  67. }
  68. var minTerm []byte
  69. if q.Min != "" {
  70. minTerm = []byte(q.Min)
  71. }
  72. var maxTerm []byte
  73. if q.Max != "" {
  74. maxTerm = []byte(q.Max)
  75. }
  76. return searcher.NewTermRangeSearcher(i, minTerm, maxTerm, q.InclusiveMin, q.InclusiveMax, field, q.BoostVal.Value(), options)
  77. }
  78. func (q *TermRangeQuery) Validate() error {
  79. if q.Min == "" && q.Min == q.Max {
  80. return fmt.Errorf("term range query must specify min or max")
  81. }
  82. return nil
  83. }
上海开阖软件有限公司 沪ICP备12045867号-1