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.

50 lines
1.4 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 searcher
  15. import (
  16. "github.com/blevesearch/bleve/index"
  17. "github.com/blevesearch/bleve/search"
  18. )
  19. func NewTermPrefixSearcher(indexReader index.IndexReader, prefix string,
  20. field string, boost float64, options search.SearcherOptions) (
  21. search.Searcher, error) {
  22. // find the terms with this prefix
  23. fieldDict, err := indexReader.FieldDictPrefix(field, []byte(prefix))
  24. if err != nil {
  25. return nil, err
  26. }
  27. defer func() {
  28. if cerr := fieldDict.Close(); cerr != nil && err == nil {
  29. err = cerr
  30. }
  31. }()
  32. var terms []string
  33. tfd, err := fieldDict.Next()
  34. for err == nil && tfd != nil {
  35. terms = append(terms, tfd.Term)
  36. if tooManyClauses(len(terms)) {
  37. return nil, tooManyClausesErr(field, len(terms))
  38. }
  39. tfd, err = fieldDict.Next()
  40. }
  41. if err != nil {
  42. return nil, err
  43. }
  44. return NewMultiTermSearcher(indexReader, terms, field, boost, options, true)
  45. }