Search in sources :

Example 51 with CharsRefBuilder

use of org.apache.lucene.util.CharsRefBuilder 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 52 with CharsRefBuilder

use of org.apache.lucene.util.CharsRefBuilder in project lucene-solr by apache.

the class BlockJoinFieldFacetAccumulator method getFacetValue.

/** copy paste from {@link DocValuesFacets} */
NamedList<Integer> getFacetValue() throws IOException {
    NamedList<Integer> facetValue = new NamedList<>();
    // if there is no globs, take segment's ones
    final CharsRefBuilder charsRef = new CharsRefBuilder();
    for (int i = 1; i < (globalCounts != null ? globalCounts.length : segmentAccums.length); i++) {
        int count = globalCounts != null ? globalCounts[i] : (int) (segmentAccums[i] >> 32);
        if (count > 0) {
            BytesRef term = topSSDV.lookupOrd(-1 + i);
            fieldType.indexedToReadable(term, charsRef);
            facetValue.add(charsRef.toString(), count);
        }
    }
    return facetValue;
}
Also used : NamedList(org.apache.solr.common.util.NamedList) CharsRefBuilder(org.apache.lucene.util.CharsRefBuilder) BytesRef(org.apache.lucene.util.BytesRef)

Aggregations

CharsRefBuilder (org.apache.lucene.util.CharsRefBuilder)52 BytesRef (org.apache.lucene.util.BytesRef)30 ArrayList (java.util.ArrayList)11 IOException (java.io.IOException)10 NamedList (org.apache.solr.common.util.NamedList)10 FieldType (org.apache.solr.schema.FieldType)10 TermsEnum (org.apache.lucene.index.TermsEnum)9 SchemaField (org.apache.solr.schema.SchemaField)7 BytesRefBuilder (org.apache.lucene.util.BytesRefBuilder)6 HashSet (java.util.HashSet)5 Test (org.junit.Test)5 TokenStream (org.apache.lucene.analysis.TokenStream)4 PostingsEnum (org.apache.lucene.index.PostingsEnum)4 Terms (org.apache.lucene.index.Terms)4 SimpleOrderedMap (org.apache.solr.common.util.SimpleOrderedMap)4 LeafReader (org.apache.lucene.index.LeafReader)3 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)3 CharsRef (org.apache.lucene.util.CharsRef)3 Util (org.apache.lucene.util.fst.Util)3 SolrException (org.apache.solr.common.SolrException)3