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