Example usage for org.apache.lucene.index TermsEnum TermsEnum

List of usage examples for org.apache.lucene.index TermsEnum TermsEnum

Introduction

In this page you can find the example usage for org.apache.lucene.index TermsEnum TermsEnum.

Prototype

protected TermsEnum() 

Source Link

Document

Sole constructor.

Usage

From source file:com.floragunn.searchguard.configuration.EmptyReader.java

License:Apache License

@Override
public Fields fields() throws IOException {
    return new Fields() {
        @Override//from  w  ww .  ja  v a 2 s .  co m
        public Iterator<String> iterator() {
            return Collections.<String>emptyList().iterator();
        }

        @Override
        public Terms terms(final String field) throws IOException {
            return new Terms() {

                @Override
                public long size() throws IOException {
                    // TODO Auto-generated method stub
                    return 0;
                }

                @Override
                public TermsEnum iterator() throws IOException {
                    // TODO Auto-generated method stub
                    return new TermsEnum() {

                        @Override
                        public BytesRef next() throws IOException {
                            // TODO Auto-generated method stub
                            return null;
                        }

                        @Override
                        public long totalTermFreq() throws IOException {
                            // TODO Auto-generated method stub
                            return 0;
                        }

                        @Override
                        public BytesRef term() throws IOException {
                            // TODO Auto-generated method stub
                            return null;
                        }

                        @Override
                        public void seekExact(long ord) throws IOException {
                            // TODO Auto-generated method stub

                        }

                        @Override
                        public SeekStatus seekCeil(BytesRef text) throws IOException {
                            // TODO Auto-generated method stub
                            return null;
                        }

                        @Override
                        public PostingsEnum postings(PostingsEnum reuse, int flags) throws IOException {
                            // TODO Auto-generated method stub
                            return null;
                        }

                        @Override
                        public long ord() throws IOException {
                            // TODO Auto-generated method stub
                            return 0;
                        }

                        @Override
                        public int docFreq() throws IOException {
                            // TODO Auto-generated method stub
                            return 0;
                        }
                    };
                }

                @Override
                public boolean hasPositions() {
                    // TODO Auto-generated method stub
                    return false;
                }

                @Override
                public boolean hasPayloads() {
                    // TODO Auto-generated method stub
                    return false;
                }

                @Override
                public boolean hasOffsets() {
                    // TODO Auto-generated method stub
                    return false;
                }

                @Override
                public boolean hasFreqs() {
                    // TODO Auto-generated method stub
                    return false;
                }

                @Override
                public long getSumTotalTermFreq() throws IOException {
                    // TODO Auto-generated method stub
                    return 0;
                }

                @Override
                public long getSumDocFreq() throws IOException {
                    // TODO Auto-generated method stub
                    return 0;
                }

                @Override
                public int getDocCount() throws IOException {
                    // TODO Auto-generated method stub
                    return 0;
                }
            };
        }

        @Override
        public int size() {
            return 0;
        }
    };
}

From source file:org.elasticsearch.action.termvector.TermVectorFields.java

License:Apache License

@Override
public Terms terms(String field) throws IOException {
    // first, find where in the termVectors bytes the actual term vector for
    // this field is stored
    if (!fieldMap.containsKey(field)) {
        return null; // we don't have it.
    }/*from  www. j  a  v  a  2s.c  o  m*/
    long offset = fieldMap.lget();
    final BytesStreamInput perFieldTermVectorInput = new BytesStreamInput(this.termVectors);
    perFieldTermVectorInput.reset();
    perFieldTermVectorInput.skip(offset);

    // read how many terms....
    final long numTerms = perFieldTermVectorInput.readVLong();
    // ...if positions etc. were stored....
    final boolean hasPositions = perFieldTermVectorInput.readBoolean();
    final boolean hasOffsets = perFieldTermVectorInput.readBoolean();
    final boolean hasPayloads = perFieldTermVectorInput.readBoolean();
    // read the field statistics
    final long sumTotalTermFreq = hasFieldStatistic ? readPotentiallyNegativeVLong(perFieldTermVectorInput)
            : -1;
    final long sumDocFreq = hasFieldStatistic ? readPotentiallyNegativeVLong(perFieldTermVectorInput) : -1;
    final int docCount = hasFieldStatistic ? readPotentiallyNegativeVInt(perFieldTermVectorInput) : -1;

    return new Terms() {

        @Override
        public TermsEnum iterator(TermsEnum reuse) throws IOException {
            // convert bytes ref for the terms to actual data
            return new TermsEnum() {
                int currentTerm = 0;
                int freq = 0;
                int docFreq = -1;
                long totalTermFrequency = -1;
                int[] positions = new int[1];
                int[] startOffsets = new int[1];
                int[] endOffsets = new int[1];
                BytesRef[] payloads = new BytesRef[1];
                final BytesRef spare = new BytesRef();

                @Override
                public BytesRef next() throws IOException {
                    if (currentTerm++ < numTerms) {
                        // term string. first the size...
                        int termVectorSize = perFieldTermVectorInput.readVInt();
                        spare.grow(termVectorSize);
                        // ...then the value.
                        perFieldTermVectorInput.readBytes(spare.bytes, 0, termVectorSize);
                        spare.length = termVectorSize;
                        if (hasTermStatistic) {
                            docFreq = readPotentiallyNegativeVInt(perFieldTermVectorInput);
                            totalTermFrequency = readPotentiallyNegativeVLong(perFieldTermVectorInput);

                        }

                        freq = readPotentiallyNegativeVInt(perFieldTermVectorInput);
                        // grow the arrays to read the values. this is just
                        // for performance reasons. Re-use memory instead of
                        // realloc.
                        growBuffers();
                        // finally, read the values into the arrays
                        // curentPosition etc. so that we can just iterate
                        // later
                        writeInfos(perFieldTermVectorInput);
                        return spare;

                    } else {
                        return null;
                    }

                }

                private void writeInfos(final BytesStreamInput input) throws IOException {
                    for (int i = 0; i < freq; i++) {
                        if (hasPositions) {
                            positions[i] = input.readVInt();
                        }
                        if (hasOffsets) {
                            startOffsets[i] = input.readVInt();
                            endOffsets[i] = input.readVInt();
                        }
                        if (hasPayloads) {
                            int payloadLength = input.readVInt();
                            if (payloads[i] == null) {
                                payloads[i] = new BytesRef(payloadLength);
                            } else {
                                payloads[i].grow(payloadLength);
                            }
                            input.readBytes(payloads[i].bytes, 0, payloadLength);
                            payloads[i].length = payloadLength;
                            payloads[i].offset = 0;
                        }
                    }
                }

                private void growBuffers() {

                    if (hasPositions) {
                        positions = grow(positions, freq);
                    }
                    if (hasOffsets) {
                        startOffsets = grow(startOffsets, freq);
                        endOffsets = grow(endOffsets, freq);
                    }
                    if (hasPayloads) {
                        if (payloads.length < freq) {
                            final BytesRef[] newArray = new BytesRef[ArrayUtil.oversize(freq,
                                    RamUsageEstimator.NUM_BYTES_OBJECT_REF)];
                            System.arraycopy(payloads, 0, newArray, 0, payloads.length);
                            payloads = newArray;
                        }
                    }
                }

                @Override
                public Comparator<BytesRef> getComparator() {
                    return BytesRef.getUTF8SortedAsUnicodeComparator();
                }

                @Override
                public SeekStatus seekCeil(BytesRef text) throws IOException {
                    throw new UnsupportedOperationException();
                }

                @Override
                public void seekExact(long ord) throws IOException {
                    throw new UnsupportedOperationException("Seek is not supported");
                }

                @Override
                public BytesRef term() throws IOException {
                    return spare;
                }

                @Override
                public long ord() throws IOException {
                    throw new UnsupportedOperationException("ordinals are not supported");
                }

                @Override
                public int docFreq() throws IOException {
                    return docFreq;
                }

                @Override
                public long totalTermFreq() throws IOException {
                    return totalTermFrequency;
                }

                @Override
                public DocsEnum docs(Bits liveDocs, DocsEnum reuse, int flags) throws IOException {
                    return docsAndPositions(liveDocs,
                            reuse instanceof DocsAndPositionsEnum ? (DocsAndPositionsEnum) reuse : null, 0);
                }

                @Override
                public DocsAndPositionsEnum docsAndPositions(Bits liveDocs, DocsAndPositionsEnum reuse,
                        int flags) throws IOException {
                    final TermVectorsDocsAndPosEnum retVal = (reuse instanceof TermVectorsDocsAndPosEnum
                            ? (TermVectorsDocsAndPosEnum) reuse
                            : new TermVectorsDocsAndPosEnum());
                    return retVal.reset(hasPositions ? positions : null, hasOffsets ? startOffsets : null,
                            hasOffsets ? endOffsets : null, hasPayloads ? payloads : null, freq);
                }

            };
        }

        @Override
        public Comparator<BytesRef> getComparator() {
            return BytesRef.getUTF8SortedAsUnicodeComparator();
        }

        @Override
        public long size() throws IOException {
            return numTerms;
        }

        @Override
        public long getSumTotalTermFreq() throws IOException {
            return sumTotalTermFreq;
        }

        @Override
        public long getSumDocFreq() throws IOException {
            return sumDocFreq;
        }

        @Override
        public int getDocCount() throws IOException {
            return docCount;
        }

        @Override
        public boolean hasFreqs() {
            return true;
        }

        @Override
        public boolean hasOffsets() {
            return hasOffsets;
        }

        @Override
        public boolean hasPositions() {
            return hasPositions;
        }

        @Override
        public boolean hasPayloads() {
            return hasPayloads;
        }

    };
}

From source file:org.elasticsearch.search.suggest.completion.CompletionPostingsFormatTests.java

License:Apache License

private void writeData(Directory dir, Completion090PostingsFormat.CompletionLookupProvider provider)
        throws IOException {
    IndexOutput output = dir.createOutput("foo.txt", IOContext.DEFAULT);
    FieldsConsumer consumer = provider.consumer(output);
    final List<TermPosAndPayload> terms = new ArrayList<>();
    terms.add(new TermPosAndPayload("foofightersgenerator", 256 - 2,
            provider.buildPayload(new BytesRef("Generator - Foo Fighters"), 9, new BytesRef("id:10"))));
    terms.add(new TermPosAndPayload("generator", 256 - 1,
            provider.buildPayload(new BytesRef("Generator - Foo Fighters"), 9, new BytesRef("id:10"))));
    Fields fields = new Fields() {
        @Override/*from  ww w. ja va  2  s  . c  o m*/
        public Iterator<String> iterator() {
            return Arrays.asList("foo").iterator();
        }

        @Override
        public Terms terms(String field) throws IOException {
            if (field.equals("foo")) {
                return new Terms() {
                    @Override
                    public TermsEnum iterator() throws IOException {
                        final Iterator<TermPosAndPayload> iterator = terms.iterator();
                        return new TermsEnum() {
                            private TermPosAndPayload current = null;

                            @Override
                            public SeekStatus seekCeil(BytesRef text) throws IOException {
                                throw new UnsupportedOperationException();
                            }

                            @Override
                            public void seekExact(long ord) throws IOException {
                                throw new UnsupportedOperationException();
                            }

                            @Override
                            public BytesRef term() throws IOException {
                                return current == null ? null : current.term;
                            }

                            @Override
                            public long ord() throws IOException {
                                throw new UnsupportedOperationException();
                            }

                            @Override
                            public int docFreq() throws IOException {
                                return current == null ? 0 : 1;
                            }

                            @Override
                            public long totalTermFreq() throws IOException {
                                throw new UnsupportedOperationException();
                            }

                            @Override
                            public PostingsEnum postings(PostingsEnum reuse, int flags) throws IOException {
                                final TermPosAndPayload data = current;
                                return new PostingsEnum() {
                                    boolean done = false;

                                    @Override
                                    public int nextPosition() throws IOException {
                                        return data.pos;
                                    }

                                    @Override
                                    public int startOffset() throws IOException {
                                        return 0;
                                    }

                                    @Override
                                    public int endOffset() throws IOException {
                                        return 0;
                                    }

                                    @Override
                                    public BytesRef getPayload() throws IOException {
                                        return data.payload;
                                    }

                                    @Override
                                    public int freq() throws IOException {
                                        return 1;
                                    }

                                    @Override
                                    public int docID() {
                                        if (done) {
                                            return NO_MORE_DOCS;
                                        }
                                        return 0;
                                    }

                                    @Override
                                    public int nextDoc() throws IOException {
                                        if (done) {
                                            return NO_MORE_DOCS;
                                        }
                                        done = true;
                                        return 0;
                                    }

                                    @Override
                                    public int advance(int target) throws IOException {
                                        if (done) {
                                            return NO_MORE_DOCS;
                                        }
                                        done = true;
                                        return 0;
                                    }

                                    @Override
                                    public long cost() {
                                        return 0;
                                    }
                                };
                            }

                            @Override
                            public BytesRef next() throws IOException {
                                if (iterator.hasNext()) {
                                    current = iterator.next();
                                    return current.term;
                                }
                                current = null;
                                return null;
                            }
                        };
                    }

                    @Override
                    public long size() throws IOException {
                        throw new UnsupportedOperationException();
                    }

                    @Override
                    public long getSumTotalTermFreq() throws IOException {
                        throw new UnsupportedOperationException();
                    }

                    @Override
                    public long getSumDocFreq() throws IOException {
                        throw new UnsupportedOperationException();
                    }

                    @Override
                    public int getDocCount() throws IOException {
                        throw new UnsupportedOperationException();
                    }

                    @Override
                    public boolean hasFreqs() {
                        throw new UnsupportedOperationException();
                    }

                    @Override
                    public boolean hasOffsets() {
                        throw new UnsupportedOperationException();
                    }

                    @Override
                    public boolean hasPositions() {
                        throw new UnsupportedOperationException();
                    }

                    @Override
                    public boolean hasPayloads() {
                        throw new UnsupportedOperationException();
                    }
                };
            }
            return null;
        }

        @Override
        public int size() {
            return 0;
        }
    };
    consumer.write(fields);
    consumer.close();
    output.close();

}

From source file:org.elasticsearch.search.suggest.completion.old.CompletionPostingsFormatTest.java

License:Apache License

private void writeData(Directory dir, Completion090PostingsFormat.CompletionLookupProvider provider)
        throws IOException {
    IndexOutput output = dir.createOutput("foo.txt", IOContext.DEFAULT);
    FieldsConsumer consumer = provider.consumer(output);
    final List<TermPosAndPayload> terms = new ArrayList<>();
    terms.add(new TermPosAndPayload("foofightersgenerator", 256 - 2,
            provider.buildPayload(new BytesRef("Generator - Foo Fighters"), 9, new BytesRef("id:10"))));
    terms.add(new TermPosAndPayload("generator", 256 - 1,
            provider.buildPayload(new BytesRef("Generator - Foo Fighters"), 9, new BytesRef("id:10"))));
    Fields fields = new Fields() {
        @Override//  w w w  . j av a2s .  c  o m
        public Iterator<String> iterator() {
            return Arrays.asList("foo").iterator();
        }

        @Override
        public Terms terms(String field) throws IOException {
            if (field.equals("foo")) {
                return new Terms() {
                    @Override
                    public TermsEnum iterator() throws IOException {
                        final Iterator<TermPosAndPayload> iterator = terms.iterator();
                        return new TermsEnum() {
                            private TermPosAndPayload current = null;

                            @Override
                            public SeekStatus seekCeil(BytesRef text) throws IOException {
                                throw new UnsupportedOperationException();
                            }

                            @Override
                            public void seekExact(long ord) throws IOException {
                                throw new UnsupportedOperationException();
                            }

                            @Override
                            public BytesRef term() throws IOException {
                                return current == null ? null : current.term;
                            }

                            @Override
                            public long ord() throws IOException {
                                throw new UnsupportedOperationException();
                            }

                            @Override
                            public int docFreq() throws IOException {
                                return current == null ? 0 : 1;
                            }

                            @Override
                            public long totalTermFreq() throws IOException {
                                throw new UnsupportedOperationException();
                            }

                            @Override
                            public PostingsEnum postings(Bits liveDocs, PostingsEnum reuse, int flags)
                                    throws IOException {
                                final TermPosAndPayload data = current;
                                return new PostingsEnum() {
                                    boolean done = false;

                                    @Override
                                    public int nextPosition() throws IOException {
                                        return current.pos;
                                    }

                                    @Override
                                    public int startOffset() throws IOException {
                                        return 0;
                                    }

                                    @Override
                                    public int endOffset() throws IOException {
                                        return 0;
                                    }

                                    @Override
                                    public BytesRef getPayload() throws IOException {
                                        return current.payload;
                                    }

                                    @Override
                                    public int freq() throws IOException {
                                        return 1;
                                    }

                                    @Override
                                    public int docID() {
                                        if (done) {
                                            return NO_MORE_DOCS;
                                        }
                                        return 0;
                                    }

                                    @Override
                                    public int nextDoc() throws IOException {
                                        if (done) {
                                            return NO_MORE_DOCS;
                                        }
                                        done = true;
                                        return 0;
                                    }

                                    @Override
                                    public int advance(int target) throws IOException {
                                        if (done) {
                                            return NO_MORE_DOCS;
                                        }
                                        done = true;
                                        return 0;
                                    }

                                    @Override
                                    public long cost() {
                                        return 0;
                                    }
                                };
                            }

                            @Override
                            public BytesRef next() throws IOException {
                                if (iterator.hasNext()) {
                                    current = iterator.next();
                                    return current.term;
                                }
                                current = null;
                                return null;
                            }
                        };
                    }

                    @Override
                    public long size() throws IOException {
                        throw new UnsupportedOperationException();
                    }

                    @Override
                    public long getSumTotalTermFreq() throws IOException {
                        throw new UnsupportedOperationException();
                    }

                    @Override
                    public long getSumDocFreq() throws IOException {
                        throw new UnsupportedOperationException();
                    }

                    @Override
                    public int getDocCount() throws IOException {
                        throw new UnsupportedOperationException();
                    }

                    @Override
                    public boolean hasFreqs() {
                        throw new UnsupportedOperationException();
                    }

                    @Override
                    public boolean hasOffsets() {
                        throw new UnsupportedOperationException();
                    }

                    @Override
                    public boolean hasPositions() {
                        throw new UnsupportedOperationException();
                    }

                    @Override
                    public boolean hasPayloads() {
                        throw new UnsupportedOperationException();
                    }
                };
            }
            return null;
        }

        @Override
        public int size() {
            return 0;
        }
    };
    consumer.write(fields);
    consumer.close();
    output.close();

}