本站源代码
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.

142 lines
3.6KB

  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. "reflect"
  17. "github.com/blevesearch/bleve/index"
  18. "github.com/blevesearch/bleve/search"
  19. "github.com/blevesearch/bleve/search/scorer"
  20. "github.com/blevesearch/bleve/size"
  21. )
  22. var reflectStaticSizeTermSearcher int
  23. func init() {
  24. var ts TermSearcher
  25. reflectStaticSizeTermSearcher = int(reflect.TypeOf(ts).Size())
  26. }
  27. type TermSearcher struct {
  28. indexReader index.IndexReader
  29. reader index.TermFieldReader
  30. scorer *scorer.TermQueryScorer
  31. tfd index.TermFieldDoc
  32. }
  33. func NewTermSearcher(indexReader index.IndexReader, term string, field string, boost float64, options search.SearcherOptions) (*TermSearcher, error) {
  34. return NewTermSearcherBytes(indexReader, []byte(term), field, boost, options)
  35. }
  36. func NewTermSearcherBytes(indexReader index.IndexReader, term []byte, field string, boost float64, options search.SearcherOptions) (*TermSearcher, error) {
  37. needFreqNorm := options.Score != "none"
  38. reader, err := indexReader.TermFieldReader(term, field, needFreqNorm, needFreqNorm, options.IncludeTermVectors)
  39. if err != nil {
  40. return nil, err
  41. }
  42. return newTermSearcherFromReader(indexReader, reader, term, field, boost, options)
  43. }
  44. func newTermSearcherFromReader(indexReader index.IndexReader, reader index.TermFieldReader,
  45. term []byte, field string, boost float64, options search.SearcherOptions) (*TermSearcher, error) {
  46. count, err := indexReader.DocCount()
  47. if err != nil {
  48. _ = reader.Close()
  49. return nil, err
  50. }
  51. scorer := scorer.NewTermQueryScorer(term, field, boost, count, reader.Count(), options)
  52. return &TermSearcher{
  53. indexReader: indexReader,
  54. reader: reader,
  55. scorer: scorer,
  56. }, nil
  57. }
  58. func (s *TermSearcher) Size() int {
  59. return reflectStaticSizeTermSearcher + size.SizeOfPtr +
  60. s.reader.Size() +
  61. s.tfd.Size() +
  62. s.scorer.Size()
  63. }
  64. func (s *TermSearcher) Count() uint64 {
  65. return s.reader.Count()
  66. }
  67. func (s *TermSearcher) Weight() float64 {
  68. return s.scorer.Weight()
  69. }
  70. func (s *TermSearcher) SetQueryNorm(qnorm float64) {
  71. s.scorer.SetQueryNorm(qnorm)
  72. }
  73. func (s *TermSearcher) Next(ctx *search.SearchContext) (*search.DocumentMatch, error) {
  74. termMatch, err := s.reader.Next(s.tfd.Reset())
  75. if err != nil {
  76. return nil, err
  77. }
  78. if termMatch == nil {
  79. return nil, nil
  80. }
  81. // score match
  82. docMatch := s.scorer.Score(ctx, termMatch)
  83. // return doc match
  84. return docMatch, nil
  85. }
  86. func (s *TermSearcher) Advance(ctx *search.SearchContext, ID index.IndexInternalID) (*search.DocumentMatch, error) {
  87. termMatch, err := s.reader.Advance(ID, s.tfd.Reset())
  88. if err != nil {
  89. return nil, err
  90. }
  91. if termMatch == nil {
  92. return nil, nil
  93. }
  94. // score match
  95. docMatch := s.scorer.Score(ctx, termMatch)
  96. // return doc match
  97. return docMatch, nil
  98. }
  99. func (s *TermSearcher) Close() error {
  100. return s.reader.Close()
  101. }
  102. func (s *TermSearcher) Min() int {
  103. return 0
  104. }
  105. func (s *TermSearcher) DocumentMatchPoolSize() int {
  106. return 1
  107. }
  108. func (s *TermSearcher) Optimize(kind string, octx index.OptimizableContext) (
  109. index.OptimizableContext, error) {
  110. o, ok := s.reader.(index.Optimizable)
  111. if ok {
  112. return o.Optimize(kind, octx)
  113. }
  114. return octx, nil
  115. }
上海开阖软件有限公司 沪ICP备12045867号-1