util.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 search
  15. func MergeLocations(locations []FieldTermLocationMap) FieldTermLocationMap {
  16. rv := locations[0]
  17. for i := 1; i < len(locations); i++ {
  18. nextLocations := locations[i]
  19. for field, termLocationMap := range nextLocations {
  20. rvTermLocationMap, rvHasField := rv[field]
  21. if rvHasField {
  22. rv[field] = MergeTermLocationMaps(rvTermLocationMap, termLocationMap)
  23. } else {
  24. rv[field] = termLocationMap
  25. }
  26. }
  27. }
  28. return rv
  29. }
  30. func MergeTermLocationMaps(rv, other TermLocationMap) TermLocationMap {
  31. for term, locationMap := range other {
  32. // for a given term/document there cannot be different locations
  33. // if they came back from different clauses, overwrite is ok
  34. rv[term] = locationMap
  35. }
  36. return rv
  37. }
  38. func MergeFieldTermLocations(dest []FieldTermLocation, matches []*DocumentMatch) []FieldTermLocation {
  39. n := len(dest)
  40. for _, dm := range matches {
  41. n += len(dm.FieldTermLocations)
  42. }
  43. if cap(dest) < n {
  44. dest = append(make([]FieldTermLocation, 0, n), dest...)
  45. }
  46. for _, dm := range matches {
  47. for _, ftl := range dm.FieldTermLocations {
  48. dest = append(dest, FieldTermLocation{
  49. Field: ftl.Field,
  50. Term: ftl.Term,
  51. Location: Location{
  52. Pos: ftl.Location.Pos,
  53. Start: ftl.Location.Start,
  54. End: ftl.Location.End,
  55. ArrayPositions: append(ArrayPositions(nil), ftl.Location.ArrayPositions...),
  56. },
  57. })
  58. }
  59. }
  60. return dest
  61. }