xref: /OpenGrok/suggester/src/main/java/org/opengrok/suggest/query/customized/PhrasePositions.java (revision d1e826faf91bd41ab1a6d05ec6a5e19cb9865010)
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 package org.opengrok.suggest.query.customized;
18 
19 import org.apache.lucene.index.PostingsEnum;
20 import org.apache.lucene.index.Term;
21 
22 import java.io.IOException;
23 
24 /**
25  * Copy of the Lucene's {@link org.apache.lucene.search.PhrasePositions}. It is needed by
26  * {@link CustomExactPhraseScorer} and {@link CustomSloppyPhraseScorer} and has package private access.
27  *
28  * Position of a term in a document that takes into account the term offset within the phrase.
29  */
30 final class PhrasePositions {
31     int position;         // position in doc
32     int count;            // remaining pos in this doc
33     int offset;           // position in phrase
34     final int ord;                                  // unique across all PhrasePositions instances
35     final PostingsEnum postings;            // stream of docs & positions
36     PhrasePositions next;                           // used to make lists
37     int rptGroup = -1; // >=0 indicates that this is a repeating PP
38     int rptInd; // index in the rptGroup
39     final Term[] terms; // for repetitions initialization
40 
PhrasePositions(PostingsEnum postings, int o, int ord, Term[] terms)41     PhrasePositions(PostingsEnum postings, int o, int ord, Term[] terms) {
42         this.postings = postings;
43         offset = o;
44         this.ord = ord;
45         this.terms = terms;
46     }
47 
firstPosition()48     void firstPosition() throws IOException {
49         count = postings.freq();  // read first pos
50         nextPosition();
51     }
52 
53     /**
54      * Go to next location of this term current document, and set
55      * <code>position</code> as <code>location - offset</code>, so that a
56      * matching exact phrase is easily identified when all PhrasePositions
57      * have exactly the same <code>position</code>.
58      */
nextPosition()59     boolean nextPosition() throws IOException {
60         if (count-- > 0) {  // read subsequent pos's
61             position = postings.nextPosition() - offset;
62             return true;
63         } else {
64             return false;
65         }
66     }
67 
68     /** for debug purposes */
69     @Override
toString()70     public String toString() {
71         String s = "o:"+offset+" p:"+position+" c:"+count;
72         if (rptGroup >=0 ) {
73             s += " rpt:"+rptGroup+",i"+rptInd;
74         }
75         return s;
76     }
77 }
78