Search in sources :

Example 1 with SortedSetFieldSource

use of org.apache.lucene.queries.function.valuesource.SortedSetFieldSource in project lucene-solr by apache.

the class TrieFloatField method getSingleValueSource.

@Override
protected ValueSource getSingleValueSource(SortedSetSelector.Type choice, SchemaField f) {
    return new SortedSetFieldSource(f.getName(), choice) {

        @Override
        public FunctionValues getValues(Map context, LeafReaderContext readerContext) throws IOException {
            // needed for nested anon class ref
            SortedSetFieldSource thisAsSortedSetFieldSource = this;
            SortedSetDocValues sortedSet = DocValues.getSortedSet(readerContext.reader(), field);
            SortedDocValues view = SortedSetSelector.wrap(sortedSet, selector);
            return new FloatDocValues(thisAsSortedSetFieldSource) {

                private int lastDocID;

                private boolean setDoc(int docID) throws IOException {
                    if (docID < lastDocID) {
                        throw new IllegalArgumentException("docs out of order: lastDocID=" + lastDocID + " docID=" + docID);
                    }
                    if (docID > view.docID()) {
                        return docID == view.advance(docID);
                    } else {
                        return docID == view.docID();
                    }
                }

                @Override
                public float floatVal(int doc) throws IOException {
                    if (setDoc(doc)) {
                        BytesRef bytes = view.binaryValue();
                        assert bytes.length > 0;
                        return NumericUtils.sortableIntToFloat(LegacyNumericUtils.prefixCodedToInt(bytes));
                    } else {
                        return 0F;
                    }
                }

                @Override
                public boolean exists(int doc) throws IOException {
                    return setDoc(doc);
                }

                @Override
                public ValueFiller getValueFiller() {
                    return new ValueFiller() {

                        private final MutableValueFloat mval = new MutableValueFloat();

                        @Override
                        public MutableValue getValue() {
                            return mval;
                        }

                        @Override
                        public void fillValue(int doc) throws IOException {
                            if (setDoc(doc)) {
                                mval.exists = true;
                                mval.value = NumericUtils.sortableIntToFloat(LegacyNumericUtils.prefixCodedToInt(view.binaryValue()));
                            } else {
                                mval.exists = false;
                                mval.value = 0F;
                            }
                        }
                    };
                }
            };
        }
    };
}
Also used : SortedSetDocValues(org.apache.lucene.index.SortedSetDocValues) MutableValueFloat(org.apache.lucene.util.mutable.MutableValueFloat) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) FloatDocValues(org.apache.lucene.queries.function.docvalues.FloatDocValues) Map(java.util.Map) SortedSetFieldSource(org.apache.lucene.queries.function.valuesource.SortedSetFieldSource) SortedDocValues(org.apache.lucene.index.SortedDocValues) BytesRef(org.apache.lucene.util.BytesRef)

Example 2 with SortedSetFieldSource

use of org.apache.lucene.queries.function.valuesource.SortedSetFieldSource in project lucene-solr by apache.

the class TestSortedSetFieldSource method testSimple.

public void testSimple() throws Exception {
    Directory dir = newDirectory();
    IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(null));
    Document doc = new Document();
    doc.add(new SortedSetDocValuesField("value", new BytesRef("baz")));
    doc.add(newStringField("id", "2", Field.Store.YES));
    writer.addDocument(doc);
    doc = new Document();
    doc.add(new SortedSetDocValuesField("value", new BytesRef("foo")));
    doc.add(new SortedSetDocValuesField("value", new BytesRef("bar")));
    doc.add(newStringField("id", "1", Field.Store.YES));
    writer.addDocument(doc);
    writer.forceMerge(1);
    writer.close();
    DirectoryReader ir = DirectoryReader.open(dir);
    IndexSearcher searcher = newSearcher(ir);
    LeafReader ar = getOnlyLeafReader(ir);
    ValueSource vs = new SortedSetFieldSource("value");
    FunctionValues values = vs.getValues(Collections.emptyMap(), ar.getContext());
    assertEquals("baz", values.strVal(0));
    assertEquals("bar", values.strVal(1));
    // test SortField optimization
    final boolean reverse = random().nextBoolean();
    SortField vssf = vs.getSortField(reverse);
    SortField sf = new SortedSetSortField("value", reverse);
    assertEquals(sf, vssf);
    vssf = vssf.rewrite(searcher);
    sf = sf.rewrite(searcher);
    assertEquals(sf, vssf);
    ir.close();
    dir.close();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) SortedSetSortField(org.apache.lucene.search.SortedSetSortField) LeafReader(org.apache.lucene.index.LeafReader) DirectoryReader(org.apache.lucene.index.DirectoryReader) SortedSetSortField(org.apache.lucene.search.SortedSetSortField) SortField(org.apache.lucene.search.SortField) Document(org.apache.lucene.document.Document) IndexWriter(org.apache.lucene.index.IndexWriter) SortedSetDocValuesField(org.apache.lucene.document.SortedSetDocValuesField) BytesRef(org.apache.lucene.util.BytesRef) SortedSetFieldSource(org.apache.lucene.queries.function.valuesource.SortedSetFieldSource) Directory(org.apache.lucene.store.Directory)

Example 3 with SortedSetFieldSource

use of org.apache.lucene.queries.function.valuesource.SortedSetFieldSource in project lucene-solr by apache.

the class TrieIntField method getSingleValueSource.

@Override
protected ValueSource getSingleValueSource(SortedSetSelector.Type choice, SchemaField f) {
    return new SortedSetFieldSource(f.getName(), choice) {

        @Override
        public FunctionValues getValues(Map context, LeafReaderContext readerContext) throws IOException {
            // needed for nested anon class ref
            SortedSetFieldSource thisAsSortedSetFieldSource = this;
            SortedSetDocValues sortedSet = DocValues.getSortedSet(readerContext.reader(), field);
            SortedDocValues view = SortedSetSelector.wrap(sortedSet, selector);
            return new IntDocValues(thisAsSortedSetFieldSource) {

                private int lastDocID;

                private boolean setDoc(int docID) throws IOException {
                    if (docID < lastDocID) {
                        throw new IllegalArgumentException("docs out of order: lastDocID=" + lastDocID + " docID=" + docID);
                    }
                    if (docID > view.docID()) {
                        lastDocID = docID;
                        return docID == view.advance(docID);
                    } else {
                        return docID == view.docID();
                    }
                }

                @Override
                public int intVal(int doc) throws IOException {
                    if (setDoc(doc)) {
                        BytesRef bytes = view.binaryValue();
                        assert bytes.length > 0;
                        return LegacyNumericUtils.prefixCodedToInt(bytes);
                    } else {
                        return 0;
                    }
                }

                @Override
                public boolean exists(int doc) throws IOException {
                    return setDoc(doc);
                }

                @Override
                public ValueFiller getValueFiller() {
                    return new ValueFiller() {

                        private final MutableValueInt mval = new MutableValueInt();

                        @Override
                        public MutableValue getValue() {
                            return mval;
                        }

                        @Override
                        public void fillValue(int doc) throws IOException {
                            if (setDoc(doc)) {
                                mval.exists = true;
                                mval.value = LegacyNumericUtils.prefixCodedToInt(view.binaryValue());
                            } else {
                                mval.exists = false;
                                mval.value = 0;
                            }
                        }
                    };
                }
            };
        }
    };
}
Also used : SortedSetDocValues(org.apache.lucene.index.SortedSetDocValues) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) MutableValueInt(org.apache.lucene.util.mutable.MutableValueInt) IntDocValues(org.apache.lucene.queries.function.docvalues.IntDocValues) Map(java.util.Map) SortedSetFieldSource(org.apache.lucene.queries.function.valuesource.SortedSetFieldSource) SortedDocValues(org.apache.lucene.index.SortedDocValues) BytesRef(org.apache.lucene.util.BytesRef)

Example 4 with SortedSetFieldSource

use of org.apache.lucene.queries.function.valuesource.SortedSetFieldSource in project lucene-solr by apache.

the class TrieLongField method getSingleValueSource.

@Override
protected ValueSource getSingleValueSource(SortedSetSelector.Type choice, SchemaField f) {
    return new SortedSetFieldSource(f.getName(), choice) {

        @Override
        public FunctionValues getValues(Map context, LeafReaderContext readerContext) throws IOException {
            // needed for nested anon class ref
            SortedSetFieldSource thisAsSortedSetFieldSource = this;
            SortedSetDocValues sortedSet = DocValues.getSortedSet(readerContext.reader(), field);
            SortedDocValues view = SortedSetSelector.wrap(sortedSet, selector);
            return new LongDocValues(thisAsSortedSetFieldSource) {

                private int lastDocID;

                private boolean setDoc(int docID) throws IOException {
                    if (docID < lastDocID) {
                        throw new IllegalArgumentException("docs out of order: lastDocID=" + lastDocID + " docID=" + docID);
                    }
                    if (docID > view.docID()) {
                        lastDocID = docID;
                        return docID == view.advance(docID);
                    } else {
                        return docID == view.docID();
                    }
                }

                @Override
                public long longVal(int doc) throws IOException {
                    if (setDoc(doc)) {
                        BytesRef bytes = view.binaryValue();
                        assert bytes.length > 0;
                        return LegacyNumericUtils.prefixCodedToLong(bytes);
                    } else {
                        return 0L;
                    }
                }

                @Override
                public boolean exists(int doc) throws IOException {
                    return setDoc(doc);
                }

                @Override
                public ValueFiller getValueFiller() {
                    return new ValueFiller() {

                        private final MutableValueLong mval = new MutableValueLong();

                        @Override
                        public MutableValue getValue() {
                            return mval;
                        }

                        @Override
                        public void fillValue(int doc) throws IOException {
                            if (setDoc(doc)) {
                                mval.exists = true;
                                mval.value = LegacyNumericUtils.prefixCodedToLong(view.binaryValue());
                            } else {
                                mval.exists = false;
                                mval.value = 0L;
                            }
                        }
                    };
                }
            };
        }
    };
}
Also used : SortedSetDocValues(org.apache.lucene.index.SortedSetDocValues) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) MutableValueLong(org.apache.lucene.util.mutable.MutableValueLong) Map(java.util.Map) SortedSetFieldSource(org.apache.lucene.queries.function.valuesource.SortedSetFieldSource) SortedDocValues(org.apache.lucene.index.SortedDocValues) BytesRef(org.apache.lucene.util.BytesRef) LongDocValues(org.apache.lucene.queries.function.docvalues.LongDocValues)

Example 5 with SortedSetFieldSource

use of org.apache.lucene.queries.function.valuesource.SortedSetFieldSource in project lucene-solr by apache.

the class TrieDoubleField method getSingleValueSource.

@Override
protected ValueSource getSingleValueSource(SortedSetSelector.Type choice, SchemaField f) {
    return new SortedSetFieldSource(f.getName(), choice) {

        @Override
        public FunctionValues getValues(Map context, LeafReaderContext readerContext) throws IOException {
            // needed for nested anon class ref
            SortedSetFieldSource thisAsSortedSetFieldSource = this;
            SortedSetDocValues sortedSet = DocValues.getSortedSet(readerContext.reader(), field);
            SortedDocValues view = SortedSetSelector.wrap(sortedSet, selector);
            return new DoubleDocValues(thisAsSortedSetFieldSource) {

                private int lastDocID;

                private boolean setDoc(int docID) throws IOException {
                    if (docID < lastDocID) {
                        throw new IllegalArgumentException("docs out of order: lastDocID=" + lastDocID + " docID=" + docID);
                    }
                    if (docID > view.docID()) {
                        lastDocID = docID;
                        return docID == view.advance(docID);
                    } else {
                        return docID == view.docID();
                    }
                }

                @Override
                public double doubleVal(int doc) throws IOException {
                    if (setDoc(doc)) {
                        BytesRef bytes = view.binaryValue();
                        assert bytes.length > 0;
                        return NumericUtils.sortableLongToDouble(LegacyNumericUtils.prefixCodedToLong(bytes));
                    } else {
                        return 0D;
                    }
                }

                @Override
                public boolean exists(int doc) throws IOException {
                    return setDoc(doc);
                }

                @Override
                public ValueFiller getValueFiller() {
                    return new ValueFiller() {

                        private final MutableValueDouble mval = new MutableValueDouble();

                        @Override
                        public MutableValue getValue() {
                            return mval;
                        }

                        @Override
                        public void fillValue(int doc) throws IOException {
                            if (setDoc(doc)) {
                                mval.exists = true;
                                mval.value = NumericUtils.sortableLongToDouble(LegacyNumericUtils.prefixCodedToLong(view.binaryValue()));
                            } else {
                                mval.exists = false;
                                mval.value = 0D;
                            }
                        }
                    };
                }
            };
        }
    };
}
Also used : SortedSetDocValues(org.apache.lucene.index.SortedSetDocValues) DoubleDocValues(org.apache.lucene.queries.function.docvalues.DoubleDocValues) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) MutableValueDouble(org.apache.lucene.util.mutable.MutableValueDouble) Map(java.util.Map) SortedSetFieldSource(org.apache.lucene.queries.function.valuesource.SortedSetFieldSource) SortedDocValues(org.apache.lucene.index.SortedDocValues) BytesRef(org.apache.lucene.util.BytesRef)

Aggregations

SortedSetFieldSource (org.apache.lucene.queries.function.valuesource.SortedSetFieldSource)5 BytesRef (org.apache.lucene.util.BytesRef)5 Map (java.util.Map)4 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)4 SortedDocValues (org.apache.lucene.index.SortedDocValues)4 SortedSetDocValues (org.apache.lucene.index.SortedSetDocValues)4 Document (org.apache.lucene.document.Document)1 SortedSetDocValuesField (org.apache.lucene.document.SortedSetDocValuesField)1 DirectoryReader (org.apache.lucene.index.DirectoryReader)1 IndexWriter (org.apache.lucene.index.IndexWriter)1 LeafReader (org.apache.lucene.index.LeafReader)1 DoubleDocValues (org.apache.lucene.queries.function.docvalues.DoubleDocValues)1 FloatDocValues (org.apache.lucene.queries.function.docvalues.FloatDocValues)1 IntDocValues (org.apache.lucene.queries.function.docvalues.IntDocValues)1 LongDocValues (org.apache.lucene.queries.function.docvalues.LongDocValues)1 IndexSearcher (org.apache.lucene.search.IndexSearcher)1 SortField (org.apache.lucene.search.SortField)1 SortedSetSortField (org.apache.lucene.search.SortedSetSortField)1 Directory (org.apache.lucene.store.Directory)1 MutableValueDouble (org.apache.lucene.util.mutable.MutableValueDouble)1