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.

search_filter.go 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 searcher
  15. import (
  16. "github.com/blevesearch/bleve/index"
  17. "github.com/blevesearch/bleve/search"
  18. )
  19. // FilterFunc defines a function which can filter documents
  20. // returning true means keep the document
  21. // returning false means do not keep the document
  22. type FilterFunc func(d *search.DocumentMatch) bool
  23. // FilteringSearcher wraps any other searcher, but checks any Next/Advance
  24. // call against the supplied FilterFunc
  25. type FilteringSearcher struct {
  26. child search.Searcher
  27. accept FilterFunc
  28. }
  29. func NewFilteringSearcher(s search.Searcher, filter FilterFunc) *FilteringSearcher {
  30. return &FilteringSearcher{
  31. child: s,
  32. accept: filter,
  33. }
  34. }
  35. func (f *FilteringSearcher) Next(ctx *search.SearchContext) (*search.DocumentMatch, error) {
  36. next, err := f.child.Next(ctx)
  37. for next != nil && err == nil {
  38. if f.accept(next) {
  39. return next, nil
  40. }
  41. next, err = f.child.Next(ctx)
  42. }
  43. return nil, err
  44. }
  45. func (f *FilteringSearcher) Advance(ctx *search.SearchContext, ID index.IndexInternalID) (*search.DocumentMatch, error) {
  46. adv, err := f.child.Advance(ctx, ID)
  47. if err != nil {
  48. return nil, err
  49. }
  50. if adv == nil {
  51. return nil, nil
  52. }
  53. if f.accept(adv) {
  54. return adv, nil
  55. }
  56. return f.Next(ctx)
  57. }
  58. func (f *FilteringSearcher) Close() error {
  59. return f.child.Close()
  60. }
  61. func (f *FilteringSearcher) Weight() float64 {
  62. return f.child.Weight()
  63. }
  64. func (f *FilteringSearcher) SetQueryNorm(n float64) {
  65. f.child.SetQueryNorm(n)
  66. }
  67. func (f *FilteringSearcher) Count() uint64 {
  68. return f.child.Count()
  69. }
  70. func (f *FilteringSearcher) Min() int {
  71. return f.child.Min()
  72. }
  73. func (f *FilteringSearcher) DocumentMatchPoolSize() int {
  74. return f.child.DocumentMatchPoolSize()
  75. }