rangequery.java

来自「jsr170接口的java实现。是个apache的开源项目。」· Java 代码 · 共 495 行 · 第 1/2 页

JAVA
495 行
字号
        public Explanation explain(IndexReader reader, int doc) throws IOException {            return new Explanation();        }    }    //------------------------< RangeQueryScorer >------------------------------    /**     * Implements a <code>Scorer</code> for this <code>RangeQuery</code>.     */    private final class RangeQueryScorer extends Scorer {        /**         * The index reader to use for calculating the matching documents.         */        private final IndexReader reader;        /**         * The documents ids that match this range query.         */        private final BitSet hits;        /**         * Set to <code>true</code> when the hits have been calculated.         */        private boolean hitsCalculated = false;        /**         * The next document id to return         */        private int nextDoc = -1;        /**         * The cache key to use to store the results.         */        private final String cacheKey;        /**         * The map to store the results.         */        private final Map resultMap;        /**         * Creates a new RangeQueryScorer.         * @param similarity the similarity implementation.         * @param reader the index reader to use.         */        RangeQueryScorer(Similarity similarity, IndexReader reader) {            super(similarity);            this.reader = reader;            StringBuffer key = new StringBuffer();            key.append(lowerTerm != null ? lowerTerm.field() : upperTerm.field());            key.append('\uFFFF');            key.append(lowerTerm != null ? lowerTerm.text() : "");            key.append('\uFFFF');            key.append(upperTerm != null ? upperTerm.text() : "");            key.append('\uFFFF');            key.append(inclusive);            key.append('\uFFFF');            key.append(transform);            this.cacheKey = key.toString();            // check cache            PerQueryCache cache = PerQueryCache.getInstance();            Map m = (Map) cache.get(RangeQueryScorer.class, reader);            if (m == null) {                m = new HashMap();                cache.put(RangeQueryScorer.class, reader, m);            }            resultMap = m;            BitSet result = (BitSet) resultMap.get(cacheKey);            if (result == null) {                result = new BitSet(reader.maxDoc());            } else {                hitsCalculated = true;            }            hits = result;        }        /**         * {@inheritDoc}         */        public boolean next() throws IOException {            calculateHits();            nextDoc = hits.nextSetBit(nextDoc + 1);            return nextDoc > -1;        }        /**         * {@inheritDoc}         */        public int doc() {            return nextDoc;        }        /**         * {@inheritDoc}         */        public float score() {            return 1.0f;        }        /**         * {@inheritDoc}         */        public boolean skipTo(int target) throws IOException {            calculateHits();            nextDoc = hits.nextSetBit(target);            return nextDoc > -1;        }        /**         * Returns an empty Explanation object.         * @return an empty Explanation object.         */        public Explanation explain(int doc) {            return new Explanation();        }        /**         * Calculates the ids of the documents matching this range query.         * @throws IOException if an error occurs while reading from the index.         */        private void calculateHits() throws IOException {            if (hitsCalculated) {                return;            }            String testField = getField();            boolean checkLower = false;            if (!inclusive || transform != TRANSFORM_NONE) {                // make adjustments to set to exclusive                checkLower = true;            }            int propNameLength = FieldNames.getNameLength(lowerTerm.text());            String namePrefix = "";            if (propNameLength > 0) {                namePrefix = lowerTerm.text().substring(0, propNameLength);            }            List startTerms = new ArrayList(2);            if (transform == TRANSFORM_NONE || lowerTerm.text().length() <= propNameLength) {                // use lowerTerm as is                startTerms.add(lowerTerm);            } else {                // first enumerate terms using lower case start character                StringBuffer termText = new StringBuffer(propNameLength + 1);                termText.append(lowerTerm.text().subSequence(0, propNameLength));                char startCharacter = lowerTerm.text().charAt(propNameLength);                termText.append(Character.toLowerCase(startCharacter));                startTerms.add(new Term(lowerTerm.field(), termText.toString()));                // second enumerate terms using upper case start character                termText.setCharAt(termText.length() - 1, Character.toUpperCase(startCharacter));                startTerms.add(new Term(lowerTerm.field(), termText.toString()));            }            for (Iterator it = startTerms.iterator(); it.hasNext(); ) {                Term startTerm = (Term) it.next();                TermEnum terms = reader.terms(startTerm);                try {                    TermDocs docs = reader.termDocs();                    try {                        do {                            Term term = terms.term();                            if (term != null                                    && term.field() == testField                                    && term.text().startsWith(namePrefix)) {                                if (checkLower) {                                    int compare = termCompare(term.text(), lowerTerm.text(), propNameLength);                                    if (compare > 0 || compare == 0 && inclusive) {                                        // do not check lower term anymore if no                                        // transformation is done on the term enum                                        checkLower = transform == TRANSFORM_NONE ? false : true;                                    } else {                                        // continue with next term                                        continue;                                    }                                }                                if (upperTerm != null) {                                    int compare = termCompare(term.text(), upperTerm.text(), propNameLength);                                    // if beyond the upper term, or is exclusive and                                    // this is equal to the upper term                                    if ((compare > 0) || (!inclusive && compare == 0)) {                                        // only break out if no transformation                                        // was done on the term from the enum                                        if (transform == TRANSFORM_NONE) {                                            break;                                        } else {                                            // because of the transformation                                            // it is possible that the next                                            // term will be included again if                                            // we still enumerate on the same                                            // property name                                            if (term.text().startsWith(namePrefix)) {                                                continue;                                            } else {                                                break;                                            }                                        }                                    }                                }                                docs.seek(terms);                                while (docs.next()) {                                    hits.set(docs.doc());                                }                            } else {                                break;                            }                        } while(terms.next());                    } finally {                        docs.close();                    }                } finally {                    terms.close();                }            }            hitsCalculated = true;            // put to cache            resultMap.put(cacheKey, hits);        }        /**         * Compares the <code>text</code> with the <code>other</code> String. This         * implementation behaves like {@link String#compareTo(Object)} but also         * respects the {@link RangeQuery#transform} property.         *         * @param text   the text to compare to <code>other</code>. The         *               transformation function is applied to this parameter before         *               it is compared to <code>other</code>.         * @param other  the other String.         * @param offset start comparing the two strings at <code>offset</code>.         * @return see {@link String#compareTo(Object)}. But also respects {@link         *         #transform}.         */        private int termCompare(String text, String other, int offset) {            OffsetCharSequence seq1 = new OffsetCharSequence(offset, text, transform);            OffsetCharSequence seq2 = new OffsetCharSequence(offset, other);            return seq1.compareTo(seq2);        }    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?