Search in sources :

Example 51 with FieldInfo

use of org.apache.lucene.index.FieldInfo in project lucene-solr by apache.

the class FieldValueQuery method createWeight.

@Override
public Weight createWeight(IndexSearcher searcher, boolean needsScores, float boost) throws IOException {
    return new ConstantScoreWeight(this, boost) {

        @Override
        public Scorer scorer(LeafReaderContext context) throws IOException {
            FieldInfos fieldInfos = context.reader().getFieldInfos();
            FieldInfo fieldInfo = fieldInfos.fieldInfo(field);
            if (fieldInfo == null) {
                return null;
            }
            DocValuesType dvType = fieldInfo.getDocValuesType();
            LeafReader reader = context.reader();
            DocIdSetIterator iterator;
            switch(dvType) {
                case NONE:
                    return null;
                case NUMERIC:
                    iterator = reader.getNumericDocValues(field);
                    break;
                case BINARY:
                    iterator = reader.getBinaryDocValues(field);
                    break;
                case SORTED:
                    iterator = reader.getSortedDocValues(field);
                    break;
                case SORTED_NUMERIC:
                    iterator = reader.getSortedNumericDocValues(field);
                    break;
                case SORTED_SET:
                    iterator = reader.getSortedSetDocValues(field);
                    break;
                default:
                    throw new AssertionError();
            }
            return new ConstantScoreScorer(this, score(), iterator);
        }
    };
}
Also used : FieldInfos(org.apache.lucene.index.FieldInfos) LeafReader(org.apache.lucene.index.LeafReader) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) DocValuesType(org.apache.lucene.index.DocValuesType) FieldInfo(org.apache.lucene.index.FieldInfo)

Example 52 with FieldInfo

use of org.apache.lucene.index.FieldInfo in project lucene-solr by apache.

the class BytesRefFieldSource method getValues.

@Override
public FunctionValues getValues(Map context, LeafReaderContext readerContext) throws IOException {
    final FieldInfo fieldInfo = readerContext.reader().getFieldInfos().fieldInfo(field);
    // TODO: do it cleaner?
    if (fieldInfo != null && fieldInfo.getDocValuesType() == DocValuesType.BINARY) {
        final BinaryDocValues binaryValues = DocValues.getBinary(readerContext.reader(), field);
        return new FunctionValues() {

            int lastDocID = -1;

            private BytesRef getValueForDoc(int doc) throws IOException {
                if (doc < lastDocID) {
                    throw new IllegalArgumentException("docs were sent out-of-order: lastDocID=" + lastDocID + " vs docID=" + doc);
                }
                lastDocID = doc;
                int curDocID = binaryValues.docID();
                if (doc > curDocID) {
                    curDocID = binaryValues.advance(doc);
                }
                if (doc == curDocID) {
                    return binaryValues.binaryValue();
                } else {
                    return null;
                }
            }

            @Override
            public boolean exists(int doc) throws IOException {
                return getValueForDoc(doc) != null;
            }

            @Override
            public boolean bytesVal(int doc, BytesRefBuilder target) throws IOException {
                BytesRef value = getValueForDoc(doc);
                if (value == null || value.length == 0) {
                    return false;
                } else {
                    target.copyBytes(value);
                    return true;
                }
            }

            public String strVal(int doc) throws IOException {
                final BytesRefBuilder bytes = new BytesRefBuilder();
                return bytesVal(doc, bytes) ? bytes.get().utf8ToString() : null;
            }

            @Override
            public Object objectVal(int doc) throws IOException {
                return strVal(doc);
            }

            @Override
            public String toString(int doc) throws IOException {
                return description() + '=' + strVal(doc);
            }

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

                    private final MutableValueStr mval = new MutableValueStr();

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

                    @Override
                    public void fillValue(int doc) throws IOException {
                        BytesRef value = getValueForDoc(doc);
                        mval.exists = value != null;
                        mval.value.clear();
                        if (value != null) {
                            mval.value.copyBytes(value);
                        }
                    }
                };
            }
        };
    } else {
        return new DocTermsIndexDocValues(this, readerContext, field) {

            @Override
            protected String toTerm(String readableValue) {
                return readableValue;
            }

            @Override
            public Object objectVal(int doc) throws IOException {
                return strVal(doc);
            }

            @Override
            public String toString(int doc) throws IOException {
                return description() + '=' + strVal(doc);
            }
        };
    }
}
Also used : BytesRefBuilder(org.apache.lucene.util.BytesRefBuilder) DocTermsIndexDocValues(org.apache.lucene.queries.function.docvalues.DocTermsIndexDocValues) MutableValueStr(org.apache.lucene.util.mutable.MutableValueStr) FunctionValues(org.apache.lucene.queries.function.FunctionValues) FieldInfo(org.apache.lucene.index.FieldInfo) BinaryDocValues(org.apache.lucene.index.BinaryDocValues) BytesRef(org.apache.lucene.util.BytesRef)

Example 53 with FieldInfo

use of org.apache.lucene.index.FieldInfo in project stanbol by apache.

the class FstConfig method buildConfig.

/**
     * Inspects the SolrCore to get defined languages for the configured
     * {@link #indexField} and {@link #storeField}. Initialises the
     * {@link #getCorpusCreationInfos()}
     * @param schema the schema of the SolrCore
     * @param indexReader the index reader of the SolrCore
     */
public void buildConfig(IndexSchema schema, AtomicReader indexReader) {
    //we need this twice
    FieldInfos fieldInfos = indexReader.getFieldInfos();
    String fieldWildcard = encodeLanguage(indexField, "*");
    for (FieldInfo fieldInfo : fieldInfos) {
        //try to match the field names against the wildcard
        if (FilenameUtils.wildcardMatch(fieldInfo.name, fieldWildcard)) {
            //for matches parse the language from the field name
            String language = parseLanguage(fieldInfo.name, indexField);
            if (language != null) {
                //generate the FST file name
                StringBuilder fstFileName = new StringBuilder(fstName);
                if (!language.isEmpty()) {
                    fstFileName.append('.').append(language);
                }
                fstFileName.append(".fst");
                File fstFile = new File(fstDirectory, fstFileName.toString());
                //get the FieldType of the field from the Solr schema
                FieldType fieldType = schema.getFieldTypeNoEx(fieldInfo.name);
                if (fieldType != null) {
                    //if the fieldType is present
                    //we need also to check if the stored field with
                    //the labels is present
                    //get the stored Field and check if it is present!
                    String storeFieldName;
                    if (storeField == null) {
                        //storeField == indexField
                        storeFieldName = fieldInfo.name;
                    } else {
                        // check that the storeField is present in the index
                        storeFieldName = encodeLanguage(storeField, language);
                        FieldInfo storedFieldInfos = fieldInfos.fieldInfo(storeFieldName);
                        if (storedFieldInfos == null) {
                            log.warn(" ... ignore language {} because Stored Field {} " + "for IndexField {} does not exist! ", new Object[] { language, storeFieldName, fieldInfo.name });
                            storeFieldName = null;
                        }
                    }
                    if (storeFieldName != null) {
                        // == valid configuration
                        CorpusCreationInfo fstInfo = new CorpusCreationInfo(language, fieldInfo.name, storeFieldName, fieldType, fstFile);
                        log.info(" ... init {} ", fstInfo);
                        addCorpus(fstInfo);
                    }
                } else {
                    log.warn(" ... ignore language {} becuase unknown fieldtype " + "for SolrFied {}", language, fieldInfo.name);
                }
            }
        //else the field matched the wildcard, but has not passed the
        //encoding test.
        }
    //Solr field does not match the field definition in the config
    }
// end iterate over all fields in the SolrIndex        
}
Also used : FieldInfos(org.apache.lucene.index.FieldInfos) File(java.io.File) FieldInfo(org.apache.lucene.index.FieldInfo) FieldType(org.apache.solr.schema.FieldType)

Aggregations

FieldInfo (org.apache.lucene.index.FieldInfo)53 BytesRef (org.apache.lucene.util.BytesRef)13 LeafReader (org.apache.lucene.index.LeafReader)12 ArrayList (java.util.ArrayList)10 Terms (org.apache.lucene.index.Terms)9 TermsEnum (org.apache.lucene.index.TermsEnum)9 IOException (java.io.IOException)8 FieldInfos (org.apache.lucene.index.FieldInfos)8 HashMap (java.util.HashMap)7 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)7 DocValuesType (org.apache.lucene.index.DocValuesType)6 PointValues (org.apache.lucene.index.PointValues)6 IndexOutput (org.apache.lucene.store.IndexOutput)6 CorruptIndexException (org.apache.lucene.index.CorruptIndexException)5 SortedSetDocValues (org.apache.lucene.index.SortedSetDocValues)5 StoredFieldVisitor (org.apache.lucene.index.StoredFieldVisitor)5 Map (java.util.Map)4 Document (org.apache.lucene.document.Document)4 EmptyDocValuesProducer (org.apache.lucene.index.EmptyDocValuesProducer)4 IndexReader (org.apache.lucene.index.IndexReader)4