Search in sources :

Example 61 with FieldType

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

the class PivotFacetProcessor method getSubsetSize.

/**
   * Given a base docset, computes the size of the subset of documents corresponding to the specified pivotValue
   *
   * @param base the set of documents to evaluate relative to
   * @param field the field type used by the pivotValue
   * @param pivotValue String representation of the value, may be null (ie: "missing")
   */
private int getSubsetSize(DocSet base, SchemaField field, String pivotValue) throws IOException {
    FieldType ft = field.getType();
    if (null == pivotValue) {
        Query query = ft.getRangeQuery(null, field, null, null, false, false);
        DocSet hasVal = searcher.getDocSet(query);
        return base.andNotSize(hasVal);
    } else {
        Query query = ft.getFieldQuery(null, field, pivotValue);
        return searcher.numDocs(query, base);
    }
}
Also used : Query(org.apache.lucene.search.Query) DocSet(org.apache.solr.search.DocSet) FieldType(org.apache.solr.schema.FieldType)

Example 62 with FieldType

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

the class QueryComponent method doFieldSortValues.

protected void doFieldSortValues(ResponseBuilder rb, SolrIndexSearcher searcher) throws IOException {
    SolrQueryRequest req = rb.req;
    SolrQueryResponse rsp = rb.rsp;
    // The query cache doesn't currently store sort field values, and SolrIndexSearcher doesn't
    // currently have an option to return sort field values.  Because of this, we
    // take the documents given and re-derive the sort values.
    //
    // TODO: See SOLR-5595
    boolean fsv = req.getParams().getBool(ResponseBuilder.FIELD_SORT_VALUES, false);
    if (fsv) {
        // order is important for the sort fields
        NamedList<Object[]> sortVals = new NamedList<>();
        IndexReaderContext topReaderContext = searcher.getTopReaderContext();
        List<LeafReaderContext> leaves = topReaderContext.leaves();
        LeafReaderContext currentLeaf = null;
        if (leaves.size() == 1) {
            // if there is a single segment, use that subReader and avoid looking up each time
            currentLeaf = leaves.get(0);
            leaves = null;
        }
        DocList docList = rb.getResults().docList;
        // sort ids from lowest to highest so we can access them in order
        int nDocs = docList.size();
        final long[] sortedIds = new long[nDocs];
        // doc scores, parallel to sortedIds
        final float[] scores = new float[nDocs];
        DocList docs = rb.getResults().docList;
        DocIterator it = docs.iterator();
        for (int i = 0; i < nDocs; i++) {
            sortedIds[i] = (((long) it.nextDoc()) << 32) | i;
            scores[i] = docs.hasScores() ? it.score() : Float.NaN;
        }
        // sort ids and scores together
        new InPlaceMergeSorter() {

            @Override
            protected void swap(int i, int j) {
                long tmpId = sortedIds[i];
                float tmpScore = scores[i];
                sortedIds[i] = sortedIds[j];
                scores[i] = scores[j];
                sortedIds[j] = tmpId;
                scores[j] = tmpScore;
            }

            @Override
            protected int compare(int i, int j) {
                return Long.compare(sortedIds[i], sortedIds[j]);
            }
        }.sort(0, sortedIds.length);
        SortSpec sortSpec = rb.getSortSpec();
        Sort sort = searcher.weightSort(sortSpec.getSort());
        SortField[] sortFields = sort == null ? new SortField[] { SortField.FIELD_SCORE } : sort.getSort();
        List<SchemaField> schemaFields = sortSpec.getSchemaFields();
        for (int fld = 0; fld < schemaFields.size(); fld++) {
            SchemaField schemaField = schemaFields.get(fld);
            FieldType ft = null == schemaField ? null : schemaField.getType();
            SortField sortField = sortFields[fld];
            SortField.Type type = sortField.getType();
            // :TODO: would be simpler to always serialize every position of SortField[]
            if (type == SortField.Type.SCORE || type == SortField.Type.DOC)
                continue;
            FieldComparator<?> comparator = sortField.getComparator(1, 0);
            LeafFieldComparator leafComparator = null;
            Object[] vals = new Object[nDocs];
            int lastIdx = -1;
            int idx = 0;
            for (int i = 0; i < sortedIds.length; ++i) {
                long idAndPos = sortedIds[i];
                float score = scores[i];
                int doc = (int) (idAndPos >>> 32);
                int position = (int) idAndPos;
                if (leaves != null) {
                    idx = ReaderUtil.subIndex(doc, leaves);
                    currentLeaf = leaves.get(idx);
                    if (idx != lastIdx) {
                        // we switched segments.  invalidate leafComparator.
                        lastIdx = idx;
                        leafComparator = null;
                    }
                }
                if (leafComparator == null) {
                    leafComparator = comparator.getLeafComparator(currentLeaf);
                }
                // adjust for what segment this is in
                doc -= currentLeaf.docBase;
                leafComparator.setScorer(new FakeScorer(doc, score));
                leafComparator.copy(0, doc);
                Object val = comparator.value(0);
                if (null != ft)
                    val = ft.marshalSortValue(val);
                vals[position] = val;
            }
            sortVals.add(sortField.getField(), vals);
        }
        rsp.add("sort_values", sortVals);
    }
}
Also used : DocIterator(org.apache.solr.search.DocIterator) SortField(org.apache.lucene.search.SortField) LeafFieldComparator(org.apache.lucene.search.LeafFieldComparator) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) Sort(org.apache.lucene.search.Sort) SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) NamedList(org.apache.solr.common.util.NamedList) InPlaceMergeSorter(org.apache.lucene.util.InPlaceMergeSorter) IndexReaderContext(org.apache.lucene.index.IndexReaderContext) FieldType(org.apache.solr.schema.FieldType) SchemaField(org.apache.solr.schema.SchemaField) SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) DocList(org.apache.solr.search.DocList) SortSpec(org.apache.solr.search.SortSpec)

Example 63 with FieldType

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

the class RangeFacetRequest method createCalculator.

/**
   * Creates the right instance of {@link org.apache.solr.handler.component.RangeFacetRequest.RangeEndpointCalculator}
   * depending on the field type of the schema field
   */
private RangeEndpointCalculator<? extends Comparable<?>> createCalculator() {
    RangeEndpointCalculator<?> calc;
    FieldType ft = schemaField.getType();
    if (ft instanceof TrieField) {
        switch(ft.getNumberType()) {
            case FLOAT:
                calc = new FloatRangeEndpointCalculator(this);
                break;
            case DOUBLE:
                calc = new DoubleRangeEndpointCalculator(this);
                break;
            case INTEGER:
                calc = new IntegerRangeEndpointCalculator(this);
                break;
            case LONG:
                calc = new LongRangeEndpointCalculator(this);
                break;
            case DATE:
                calc = new DateRangeEndpointCalculator(this, null);
                break;
            default:
                throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Unable to range facet on Trie field of unexpected type:" + this.facetOn);
        }
    } else if (ft instanceof DateRangeField) {
        calc = new DateRangeEndpointCalculator(this, null);
    } else if (ft.isPointField()) {
        switch(ft.getNumberType()) {
            case FLOAT:
                calc = new FloatRangeEndpointCalculator(this);
                break;
            case DOUBLE:
                calc = new DoubleRangeEndpointCalculator(this);
                break;
            case INTEGER:
                calc = new IntegerRangeEndpointCalculator(this);
                break;
            case LONG:
                calc = new LongRangeEndpointCalculator(this);
                break;
            case DATE:
                calc = new DateRangeEndpointCalculator(this, null);
                break;
            default:
                throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Unable to range facet on Point field of unexpected type:" + this.facetOn);
        }
    } else {
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Unable to range facet on field:" + schemaField);
    }
    return calc;
}
Also used : TrieField(org.apache.solr.schema.TrieField) SolrException(org.apache.solr.common.SolrException) DateRangeField(org.apache.solr.schema.DateRangeField) FieldType(org.apache.solr.schema.FieldType)

Example 64 with FieldType

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

the class ExpandComponent method getPointGroupQuery.

private Query getPointGroupQuery(SchemaField sf, int size, LongHashSet groupSet) {
    Iterator<LongCursor> it = groupSet.iterator();
    List<String> values = new ArrayList<>(size);
    FieldType ft = sf.getType();
    while (it.hasNext()) {
        LongCursor cursor = it.next();
        values.add(numericToString(ft, cursor.value));
    }
    return new SolrConstantScoreQuery(new QueryWrapperFilter(sf.getType().getSetQuery(null, sf, values)));
}
Also used : LongCursor(com.carrotsearch.hppc.cursors.LongCursor) ArrayList(java.util.ArrayList) SolrConstantScoreQuery(org.apache.solr.search.SolrConstantScoreQuery) QueryWrapperFilter(org.apache.solr.search.QueryWrapperFilter) FieldType(org.apache.solr.schema.FieldType)

Example 65 with FieldType

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

the class FileBasedSpellChecker method loadExternalFileDictionary.

private void loadExternalFileDictionary(SolrCore core, SolrIndexSearcher searcher) {
    try {
        IndexSchema schema = null == searcher ? core.getLatestSchema() : searcher.getSchema();
        // Get the field's analyzer
        if (fieldTypeName != null && schema.getFieldTypeNoEx(fieldTypeName) != null) {
            FieldType fieldType = schema.getFieldTypes().get(fieldTypeName);
            // Do index-time analysis using the given fieldType's analyzer
            RAMDirectory ramDir = new RAMDirectory();
            LogMergePolicy mp = new LogByteSizeMergePolicy();
            mp.setMergeFactor(300);
            IndexWriter writer = new IndexWriter(ramDir, new IndexWriterConfig(fieldType.getIndexAnalyzer()).setMaxBufferedDocs(150).setMergePolicy(mp).setOpenMode(IndexWriterConfig.OpenMode.CREATE));
            List<String> lines = core.getResourceLoader().getLines(sourceLocation, characterEncoding);
            for (String s : lines) {
                Document d = new Document();
                d.add(new TextField(WORD_FIELD_NAME, s, Field.Store.NO));
                writer.addDocument(d);
            }
            writer.forceMerge(1);
            writer.close();
            dictionary = new HighFrequencyDictionary(DirectoryReader.open(ramDir), WORD_FIELD_NAME, 0.0f);
        } else {
            // check if character encoding is defined
            if (characterEncoding == null) {
                dictionary = new PlainTextDictionary(core.getResourceLoader().openResource(sourceLocation));
            } else {
                dictionary = new PlainTextDictionary(new InputStreamReader(core.getResourceLoader().openResource(sourceLocation), characterEncoding));
            }
        }
    } catch (IOException e) {
        log.error("Unable to load spellings", e);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) Document(org.apache.lucene.document.Document) RAMDirectory(org.apache.lucene.store.RAMDirectory) FieldType(org.apache.solr.schema.FieldType) HighFrequencyDictionary(org.apache.lucene.search.spell.HighFrequencyDictionary) LogByteSizeMergePolicy(org.apache.lucene.index.LogByteSizeMergePolicy) IndexWriter(org.apache.lucene.index.IndexWriter) PlainTextDictionary(org.apache.lucene.search.spell.PlainTextDictionary) LogMergePolicy(org.apache.lucene.index.LogMergePolicy) TextField(org.apache.lucene.document.TextField) IndexSchema(org.apache.solr.schema.IndexSchema) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig)

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