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_docid.go 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // Copyright (c) 2015 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. "github.com/blevesearch/bleve/search/scorer"
  19. )
  20. // DocIDSearcher returns documents matching a predefined set of identifiers.
  21. type DocIDSearcher struct {
  22. reader index.DocIDReader
  23. scorer *scorer.ConstantScorer
  24. count int
  25. }
  26. func NewDocIDSearcher(indexReader index.IndexReader, ids []string, boost float64,
  27. options search.SearcherOptions) (searcher *DocIDSearcher, err error) {
  28. reader, err := indexReader.DocIDReaderOnly(ids)
  29. if err != nil {
  30. return nil, err
  31. }
  32. scorer := scorer.NewConstantScorer(1.0, boost, options)
  33. return &DocIDSearcher{
  34. scorer: scorer,
  35. reader: reader,
  36. count: len(ids),
  37. }, nil
  38. }
  39. func (s *DocIDSearcher) Count() uint64 {
  40. return uint64(s.count)
  41. }
  42. func (s *DocIDSearcher) Weight() float64 {
  43. return s.scorer.Weight()
  44. }
  45. func (s *DocIDSearcher) SetQueryNorm(qnorm float64) {
  46. s.scorer.SetQueryNorm(qnorm)
  47. }
  48. func (s *DocIDSearcher) Next(ctx *search.SearchContext) (*search.DocumentMatch, error) {
  49. docidMatch, err := s.reader.Next()
  50. if err != nil {
  51. return nil, err
  52. }
  53. if docidMatch == nil {
  54. return nil, nil
  55. }
  56. docMatch := s.scorer.Score(ctx, docidMatch)
  57. return docMatch, nil
  58. }
  59. func (s *DocIDSearcher) Advance(ctx *search.SearchContext, ID index.IndexInternalID) (*search.DocumentMatch, error) {
  60. docidMatch, err := s.reader.Advance(ID)
  61. if err != nil {
  62. return nil, err
  63. }
  64. if docidMatch == nil {
  65. return nil, nil
  66. }
  67. docMatch := s.scorer.Score(ctx, docidMatch)
  68. return docMatch, nil
  69. }
  70. func (s *DocIDSearcher) Close() error {
  71. return s.reader.Close()
  72. }
  73. func (s *DocIDSearcher) Min() int {
  74. return 0
  75. }
  76. func (s *DocIDSearcher) DocumentMatchPoolSize() int {
  77. return 1
  78. }