Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

facet_builder_datetime.go 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 facet
  15. import (
  16. "sort"
  17. "time"
  18. "github.com/blevesearch/bleve/numeric"
  19. "github.com/blevesearch/bleve/search"
  20. )
  21. type dateTimeRange struct {
  22. start time.Time
  23. end time.Time
  24. }
  25. type DateTimeFacetBuilder struct {
  26. size int
  27. field string
  28. termsCount map[string]int
  29. total int
  30. missing int
  31. ranges map[string]*dateTimeRange
  32. sawValue bool
  33. }
  34. func NewDateTimeFacetBuilder(field string, size int) *DateTimeFacetBuilder {
  35. return &DateTimeFacetBuilder{
  36. size: size,
  37. field: field,
  38. termsCount: make(map[string]int),
  39. ranges: make(map[string]*dateTimeRange, 0),
  40. }
  41. }
  42. func (fb *DateTimeFacetBuilder) AddRange(name string, start, end time.Time) {
  43. r := dateTimeRange{
  44. start: start,
  45. end: end,
  46. }
  47. fb.ranges[name] = &r
  48. }
  49. func (fb *DateTimeFacetBuilder) Field() string {
  50. return fb.field
  51. }
  52. func (fb *DateTimeFacetBuilder) UpdateVisitor(field string, term []byte) {
  53. if field == fb.field {
  54. fb.sawValue = true
  55. // only consider the values which are shifted 0
  56. prefixCoded := numeric.PrefixCoded(term)
  57. shift, err := prefixCoded.Shift()
  58. if err == nil && shift == 0 {
  59. i64, err := prefixCoded.Int64()
  60. if err == nil {
  61. t := time.Unix(0, i64)
  62. // look at each of the ranges for a match
  63. for rangeName, r := range fb.ranges {
  64. if (r.start.IsZero() || t.After(r.start) || t.Equal(r.start)) && (r.end.IsZero() || t.Before(r.end)) {
  65. fb.termsCount[rangeName] = fb.termsCount[rangeName] + 1
  66. fb.total++
  67. }
  68. }
  69. }
  70. }
  71. }
  72. }
  73. func (fb *DateTimeFacetBuilder) StartDoc() {
  74. fb.sawValue = false
  75. }
  76. func (fb *DateTimeFacetBuilder) EndDoc() {
  77. if !fb.sawValue {
  78. fb.missing++
  79. }
  80. }
  81. func (fb *DateTimeFacetBuilder) Result() *search.FacetResult {
  82. rv := search.FacetResult{
  83. Field: fb.field,
  84. Total: fb.total,
  85. Missing: fb.missing,
  86. }
  87. rv.DateRanges = make([]*search.DateRangeFacet, 0, len(fb.termsCount))
  88. for term, count := range fb.termsCount {
  89. dateRange := fb.ranges[term]
  90. tf := &search.DateRangeFacet{
  91. Name: term,
  92. Count: count,
  93. }
  94. if !dateRange.start.IsZero() {
  95. start := dateRange.start.Format(time.RFC3339Nano)
  96. tf.Start = &start
  97. }
  98. if !dateRange.end.IsZero() {
  99. end := dateRange.end.Format(time.RFC3339Nano)
  100. tf.End = &end
  101. }
  102. rv.DateRanges = append(rv.DateRanges, tf)
  103. }
  104. sort.Sort(rv.DateRanges)
  105. // we now have the list of the top N facets
  106. if fb.size < len(rv.DateRanges) {
  107. rv.DateRanges = rv.DateRanges[:fb.size]
  108. }
  109. notOther := 0
  110. for _, nr := range rv.DateRanges {
  111. notOther += nr.Count
  112. }
  113. rv.Other = fb.total - notOther
  114. return &rv
  115. }