List of usage examples for org.apache.lucene.index PostingsEnum PostingsEnum
protected PostingsEnum()
From source file:org.apache.solr.search.FloatPayloadValueSource.java
License:Apache License
@Override public FunctionValues getValues(Map context, LeafReaderContext readerContext) throws IOException { Fields fields = readerContext.reader().fields(); final Terms terms = fields.terms(indexedField); FunctionValues defaultValues = defaultValueSource.getValues(context, readerContext); // copied the bulk of this from TFValueSource - TODO: this is a very repeated pattern - base-class this advance logic stuff? return new FloatDocValues(this) { PostingsEnum docs;//from w w w . ja v a 2 s .c om int atDoc; int lastDocRequested = -1; { reset(); } public void reset() throws IOException { // no one should call us for deleted docs? if (terms != null) { final TermsEnum termsEnum = terms.iterator(); if (termsEnum.seekExact(indexedBytes)) { docs = termsEnum.postings(null, PostingsEnum.ALL); } else { docs = null; } } else { docs = null; } if (docs == null) { // dummy PostingsEnum so floatVal() can work // when would this be called? if field/val did not match? this is called for every doc? create once and cache? docs = new PostingsEnum() { @Override public int freq() { return 0; } @Override public int nextPosition() throws IOException { return -1; } @Override public int startOffset() throws IOException { return -1; } @Override public int endOffset() throws IOException { return -1; } @Override public BytesRef getPayload() throws IOException { return null; } @Override public int docID() { return DocIdSetIterator.NO_MORE_DOCS; } @Override public int nextDoc() { return DocIdSetIterator.NO_MORE_DOCS; } @Override public int advance(int target) { return DocIdSetIterator.NO_MORE_DOCS; } @Override public long cost() { return 0; } }; } atDoc = -1; } @Override public float floatVal(int doc) { try { if (doc < lastDocRequested) { // out-of-order access.... reset reset(); } lastDocRequested = doc; if (atDoc < doc) { atDoc = docs.advance(doc); } if (atDoc > doc) { // term doesn't match this document... either because we hit the // end, or because the next doc is after this doc. return defaultValues.floatVal(doc); } // a match! int freq = docs.freq(); int numPayloadsSeen = 0; float currentScore = 0; for (int i = 0; i < freq; i++) { docs.nextPosition(); BytesRef payload = docs.getPayload(); if (payload != null) { float payloadVal = decoder.decode(atDoc, docs.startOffset(), docs.endOffset(), payload); // payloadFunction = null represents "first" if (payloadFunction == null) return payloadVal; currentScore = payloadFunction.currentScore(doc, indexedField, docs.startOffset(), docs.endOffset(), numPayloadsSeen, currentScore, payloadVal); numPayloadsSeen++; } } return (numPayloadsSeen > 0) ? payloadFunction.docScore(doc, indexedField, numPayloadsSeen, currentScore) : defaultValues.floatVal(doc); } catch (IOException e) { throw new RuntimeException("caught exception in function " + description() + " : doc=" + doc, e); } } }; }
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 w ww . j a v a 2 s. c om*/ 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/*from w w w.ja v a 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(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(); }