Search in sources :

Example 91 with FieldType

use of org.apache.solr.schema.FieldType in project lucene-solr by apache.

the class TopGroupsResultTransformer method serializeTopGroups.

protected NamedList serializeTopGroups(TopGroups<BytesRef> data, SchemaField groupField) throws IOException {
    NamedList<Object> result = new NamedList<>();
    result.add("totalGroupedHitCount", data.totalGroupedHitCount);
    result.add("totalHitCount", data.totalHitCount);
    if (data.totalGroupCount != null) {
        result.add("totalGroupCount", data.totalGroupCount);
    }
    final IndexSchema schema = rb.req.getSearcher().getSchema();
    SchemaField uniqueField = schema.getUniqueKeyField();
    for (GroupDocs<BytesRef> searchGroup : data.groups) {
        NamedList<Object> groupResult = new NamedList<>();
        groupResult.add("totalHits", searchGroup.totalHits);
        if (!Float.isNaN(searchGroup.maxScore)) {
            groupResult.add("maxScore", searchGroup.maxScore);
        }
        List<NamedList<Object>> documents = new ArrayList<>();
        for (int i = 0; i < searchGroup.scoreDocs.length; i++) {
            NamedList<Object> document = new NamedList<>();
            documents.add(document);
            Document doc = retrieveDocument(uniqueField, searchGroup.scoreDocs[i].doc);
            document.add(ID, uniqueField.getType().toExternal(doc.getField(uniqueField.getName())));
            if (!Float.isNaN(searchGroup.scoreDocs[i].score)) {
                document.add("score", searchGroup.scoreDocs[i].score);
            }
            if (!(searchGroup.scoreDocs[i] instanceof FieldDoc)) {
                // thus don't add sortValues below
                continue;
            }
            FieldDoc fieldDoc = (FieldDoc) searchGroup.scoreDocs[i];
            Object[] convertedSortValues = new Object[fieldDoc.fields.length];
            for (int j = 0; j < fieldDoc.fields.length; j++) {
                Object sortValue = fieldDoc.fields[j];
                Sort withinGroupSort = rb.getGroupingSpec().getSortWithinGroup();
                SchemaField field = withinGroupSort.getSort()[j].getField() != null ? schema.getFieldOrNull(withinGroupSort.getSort()[j].getField()) : null;
                if (field != null) {
                    FieldType fieldType = field.getType();
                    if (sortValue != null) {
                        sortValue = fieldType.marshalSortValue(sortValue);
                    }
                }
                convertedSortValues[j] = sortValue;
            }
            document.add("sortValues", convertedSortValues);
        }
        groupResult.add("documents", documents);
        String groupValue = searchGroup.groupValue != null ? groupField.getType().indexedToReadable(searchGroup.groupValue, new CharsRefBuilder()).toString() : null;
        result.add(groupValue, groupResult);
    }
    return result;
}
Also used : FieldDoc(org.apache.lucene.search.FieldDoc) NamedList(org.apache.solr.common.util.NamedList) ArrayList(java.util.ArrayList) Document(org.apache.lucene.document.Document) FieldType(org.apache.solr.schema.FieldType) SchemaField(org.apache.solr.schema.SchemaField) Sort(org.apache.lucene.search.Sort) IndexSchema(org.apache.solr.schema.IndexSchema) CharsRefBuilder(org.apache.lucene.util.CharsRefBuilder) BytesRef(org.apache.lucene.util.BytesRef)

Example 92 with FieldType

use of org.apache.solr.schema.FieldType in project lucene-solr by apache.

the class GeoDistValueSourceParser method parseSfield.

private MultiValueSource parseSfield(FunctionQParser fp) throws SyntaxError {
    String sfield = fp.getParam(SpatialParams.FIELD);
    if (sfield == null)
        return null;
    SchemaField sf = fp.getReq().getSchema().getField(sfield);
    FieldType type = sf.getType();
    if (type instanceof AbstractSpatialFieldType) {
        AbstractSpatialFieldType asft = (AbstractSpatialFieldType) type;
        return new SpatialStrategyMultiValueSource(asft.getStrategy(sfield), asft.getDistanceUnits());
    }
    ValueSource vs = type.getValueSource(sf, fp);
    if (vs instanceof MultiValueSource) {
        return (MultiValueSource) vs;
    }
    throw new SyntaxError("Spatial field must implement MultiValueSource or extend AbstractSpatialFieldType:" + sf);
}
Also used : SchemaField(org.apache.solr.schema.SchemaField) SyntaxError(org.apache.solr.search.SyntaxError) ValueSource(org.apache.lucene.queries.function.ValueSource) DoubleConstValueSource(org.apache.lucene.queries.function.valuesource.DoubleConstValueSource) MultiValueSource(org.apache.lucene.queries.function.valuesource.MultiValueSource) VectorValueSource(org.apache.lucene.queries.function.valuesource.VectorValueSource) AbstractSpatialFieldType(org.apache.solr.schema.AbstractSpatialFieldType) MultiValueSource(org.apache.lucene.queries.function.valuesource.MultiValueSource) FieldType(org.apache.solr.schema.FieldType) AbstractSpatialFieldType(org.apache.solr.schema.AbstractSpatialFieldType)

Example 93 with FieldType

use of org.apache.solr.schema.FieldType 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

FieldType (org.apache.solr.schema.FieldType)93 SchemaField (org.apache.solr.schema.SchemaField)37 SolrException (org.apache.solr.common.SolrException)29 ArrayList (java.util.ArrayList)23 BytesRef (org.apache.lucene.util.BytesRef)23 NamedList (org.apache.solr.common.util.NamedList)23 IOException (java.io.IOException)18 SimpleOrderedMap (org.apache.solr.common.util.SimpleOrderedMap)15 IndexSchema (org.apache.solr.schema.IndexSchema)14 Query (org.apache.lucene.search.Query)13 BytesRefBuilder (org.apache.lucene.util.BytesRefBuilder)13 Analyzer (org.apache.lucene.analysis.Analyzer)12 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)10 CharsRefBuilder (org.apache.lucene.util.CharsRefBuilder)10 StrField (org.apache.solr.schema.StrField)8 HashMap (java.util.HashMap)7 List (java.util.List)7 Map (java.util.Map)7 DocIterator (org.apache.solr.search.DocIterator)7 DocList (org.apache.solr.search.DocList)7