Search in sources :

Example 66 with NamedList

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

the class CdcrRequestHandler method getStatus.

private NamedList getStatus() {
    NamedList status = new NamedList();
    status.add(CdcrParams.ProcessState.getParam(), processStateManager.getState().toLower());
    status.add(CdcrParams.BufferState.getParam(), bufferStateManager.getState().toLower());
    return status;
}
Also used : NamedList(org.apache.solr.common.util.NamedList)

Example 67 with NamedList

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

the class SuggestComponent method toNamedList.

/** Convert {@link SuggesterResult} to NamedList for constructing responses */
private void toNamedList(SuggesterResult suggesterResult, Map<String, SimpleOrderedMap<NamedList<Object>>> resultObj) {
    for (String suggesterName : suggesterResult.getSuggesterNames()) {
        SimpleOrderedMap<NamedList<Object>> results = new SimpleOrderedMap<>();
        for (String token : suggesterResult.getTokens(suggesterName)) {
            SimpleOrderedMap<Object> suggestionBody = new SimpleOrderedMap<>();
            List<LookupResult> lookupResults = suggesterResult.getLookupResult(suggesterName, token);
            suggestionBody.add(SuggesterResultLabels.SUGGESTION_NUM_FOUND, lookupResults.size());
            List<SimpleOrderedMap<Object>> suggestEntriesNamedList = new ArrayList<>();
            for (LookupResult lookupResult : lookupResults) {
                String suggestionString = lookupResult.key.toString();
                long weight = lookupResult.value;
                String payload = (lookupResult.payload != null) ? lookupResult.payload.utf8ToString() : "";
                SimpleOrderedMap<Object> suggestEntryNamedList = new SimpleOrderedMap<>();
                suggestEntryNamedList.add(SuggesterResultLabels.SUGGESTION_TERM, suggestionString);
                suggestEntryNamedList.add(SuggesterResultLabels.SUGGESTION_WEIGHT, weight);
                suggestEntryNamedList.add(SuggesterResultLabels.SUGGESTION_PAYLOAD, payload);
                suggestEntriesNamedList.add(suggestEntryNamedList);
            }
            suggestionBody.add(SuggesterResultLabels.SUGGESTIONS, suggestEntriesNamedList);
            results.add(token, suggestionBody);
        }
        resultObj.put(suggesterName, results);
    }
}
Also used : NamedList(org.apache.solr.common.util.NamedList) LookupResult(org.apache.lucene.search.suggest.Lookup.LookupResult) ArrayList(java.util.ArrayList) SimpleOrderedMap(org.apache.solr.common.util.SimpleOrderedMap)

Example 68 with NamedList

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

the class FieldOptions method mapOneVector.

private void mapOneVector(NamedList<Object> docNL, FieldOptions fieldOptions, IndexReader reader, int docID, TermsEnum termsEnum, String field) throws IOException {
    NamedList<Object> fieldNL = new NamedList<>();
    docNL.add(field, fieldNL);
    BytesRef text;
    PostingsEnum dpEnum = null;
    while ((text = termsEnum.next()) != null) {
        String term = text.utf8ToString();
        NamedList<Object> termInfo = new NamedList<>();
        fieldNL.add(term, termInfo);
        final int freq = (int) termsEnum.totalTermFreq();
        if (fieldOptions.termFreq == true) {
            termInfo.add("tf", freq);
        }
        int dpEnumFlags = 0;
        dpEnumFlags |= fieldOptions.positions ? PostingsEnum.POSITIONS : 0;
        //payloads require offsets
        dpEnumFlags |= (fieldOptions.offsets || fieldOptions.payloads) ? PostingsEnum.OFFSETS : 0;
        dpEnumFlags |= fieldOptions.payloads ? PostingsEnum.PAYLOADS : 0;
        dpEnum = termsEnum.postings(dpEnum, dpEnumFlags);
        boolean atNextDoc = false;
        if (dpEnum != null) {
            dpEnum.nextDoc();
            atNextDoc = true;
        }
        if (atNextDoc && dpEnumFlags != 0) {
            NamedList<Integer> positionsNL = null;
            NamedList<Number> theOffsets = null;
            NamedList<String> thePayloads = null;
            for (int i = 0; i < freq; i++) {
                final int pos = dpEnum.nextPosition();
                if (fieldOptions.positions && pos >= 0) {
                    if (positionsNL == null) {
                        positionsNL = new NamedList<>();
                        termInfo.add("positions", positionsNL);
                    }
                    positionsNL.add("position", pos);
                }
                int startOffset = fieldOptions.offsets ? dpEnum.startOffset() : -1;
                if (startOffset >= 0) {
                    if (theOffsets == null) {
                        theOffsets = new NamedList<>();
                        termInfo.add("offsets", theOffsets);
                    }
                    theOffsets.add("start", dpEnum.startOffset());
                    theOffsets.add("end", dpEnum.endOffset());
                }
                BytesRef payload = fieldOptions.payloads ? dpEnum.getPayload() : null;
                if (payload != null) {
                    if (thePayloads == null) {
                        thePayloads = new NamedList<>();
                        termInfo.add("payloads", thePayloads);
                    }
                    thePayloads.add("payload", Base64.byteArrayToBase64(payload.bytes, payload.offset, payload.length));
                }
            }
        }
        int df = 0;
        if (fieldOptions.docFreq || fieldOptions.tfIdf) {
            df = reader.docFreq(new Term(field, text));
        }
        if (fieldOptions.docFreq) {
            termInfo.add("df", df);
        }
        // TODO: this is not TF/IDF by anyone's definition!
        if (fieldOptions.tfIdf) {
            double tfIdfVal = ((double) freq) / df;
            termInfo.add("tf-idf", tfIdfVal);
        }
    }
}
Also used : NamedList(org.apache.solr.common.util.NamedList) Term(org.apache.lucene.index.Term) PostingsEnum(org.apache.lucene.index.PostingsEnum) BytesRef(org.apache.lucene.util.BytesRef)

Example 69 with NamedList

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

the class SimpleFacets method getListedTermCounts.

/**
   * Computes the term-&gt;count counts for the specified term values relative to the 
   * @param field the name of the field to compute term counts against
   * @param parsed contains the docset to compute term counts relative to
   * @param terms a list of term values (in the specified field) to compute the counts for 
   */
protected NamedList<Integer> getListedTermCounts(String field, final ParsedParams parsed, List<String> terms) throws IOException {
    SchemaField sf = searcher.getSchema().getField(field);
    FieldType ft = sf.getType();
    NamedList<Integer> res = new NamedList<>();
    for (String term : terms) {
        int count = searcher.numDocs(ft.getFieldQuery(null, sf, term), parsed.docs);
        res.add(term, count);
    }
    return res;
}
Also used : SchemaField(org.apache.solr.schema.SchemaField) NamedList(org.apache.solr.common.util.NamedList) FieldType(org.apache.solr.schema.FieldType)

Example 70 with NamedList

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

the class SimpleFacets method getFacetTermEnumCounts.

/**
   * Returns a list of terms in the specified field along with the 
   * corresponding count of documents in the set that match that constraint.
   * This method uses the FilterCache to get the intersection count between <code>docs</code>
   * and the DocSet for each term in the filter.
   *
   * @see FacetParams#FACET_LIMIT
   * @see FacetParams#FACET_ZEROS
   * @see FacetParams#FACET_MISSING
   */
public NamedList<Integer> getFacetTermEnumCounts(SolrIndexSearcher searcher, DocSet docs, String field, int offset, int limit, int mincount, boolean missing, String sort, String prefix, Predicate<BytesRef> termFilter, boolean intersectsCheck) throws IOException {
    /* :TODO: potential optimization...
    * cache the Terms with the highest docFreq and try them first
    * don't enum if we get our max from them
    */
    // Minimum term docFreq in order to use the filterCache for that term.
    int minDfFilterCache = global.getFieldInt(field, FacetParams.FACET_ENUM_CACHE_MINDF, 0);
    // make sure we have a set that is fast for random access, if we will use it for that
    DocSet fastForRandomSet = docs;
    if (minDfFilterCache > 0 && docs instanceof SortedIntDocSet) {
        SortedIntDocSet sset = (SortedIntDocSet) docs;
        fastForRandomSet = new HashDocSet(sset.getDocs(), 0, sset.size());
    }
    IndexSchema schema = searcher.getSchema();
    FieldType ft = schema.getFieldType(field);
    assert !ft.isPointField() : "Point Fields don't support enum method";
    LeafReader r = searcher.getSlowAtomicReader();
    boolean sortByCount = sort.equals("count") || sort.equals("true");
    final int maxsize = limit >= 0 ? offset + limit : Integer.MAX_VALUE - 1;
    final BoundedTreeSet<CountPair<BytesRef, Integer>> queue = sortByCount ? new BoundedTreeSet<CountPair<BytesRef, Integer>>(maxsize) : null;
    final NamedList<Integer> res = new NamedList<>();
    // the smallest value in the top 'N' values    
    int min = mincount - 1;
    int off = offset;
    int lim = limit >= 0 ? limit : Integer.MAX_VALUE;
    BytesRef prefixTermBytes = null;
    if (prefix != null) {
        String indexedPrefix = ft.toInternal(prefix);
        prefixTermBytes = new BytesRef(indexedPrefix);
    }
    Fields fields = r.fields();
    Terms terms = fields == null ? null : fields.terms(field);
    TermsEnum termsEnum = null;
    SolrIndexSearcher.DocsEnumState deState = null;
    BytesRef term = null;
    if (terms != null) {
        termsEnum = terms.iterator();
        if (prefixTermBytes != null) {
            if (termsEnum.seekCeil(prefixTermBytes) == TermsEnum.SeekStatus.END) {
                termsEnum = null;
            } else {
                term = termsEnum.term();
            }
        } else {
            // position termsEnum on first term
            term = termsEnum.next();
        }
    }
    PostingsEnum postingsEnum = null;
    CharsRefBuilder charsRef = new CharsRefBuilder();
    if (docs.size() >= mincount) {
        while (term != null) {
            if (prefixTermBytes != null && !StringHelper.startsWith(term, prefixTermBytes))
                break;
            if (termFilter == null || termFilter.test(term)) {
                int df = termsEnum.docFreq();
                // make a large difference (for example, many terms with df=1).
                if (df > 0 && df > min) {
                    int c;
                    if (df >= minDfFilterCache) {
                        if (deState == null) {
                            deState = new SolrIndexSearcher.DocsEnumState();
                            deState.fieldName = field;
                            deState.liveDocs = r.getLiveDocs();
                            deState.termsEnum = termsEnum;
                            deState.postingsEnum = postingsEnum;
                        }
                        if (intersectsCheck) {
                            c = searcher.intersects(docs, deState) ? 1 : 0;
                        } else {
                            c = searcher.numDocs(docs, deState);
                        }
                        postingsEnum = deState.postingsEnum;
                    } else {
                        // iterate over TermDocs to calculate the intersection
                        // TODO: specialize when base docset is a bitset or hash set (skipDocs)?  or does it matter for this?
                        // TODO: do this per-segment for better efficiency (MultiDocsEnum just uses base class impl)
                        // TODO: would passing deleted docs lead to better efficiency over checking the fastForRandomSet?
                        postingsEnum = termsEnum.postings(postingsEnum, PostingsEnum.NONE);
                        c = 0;
                        if (postingsEnum instanceof MultiPostingsEnum) {
                            MultiPostingsEnum.EnumWithSlice[] subs = ((MultiPostingsEnum) postingsEnum).getSubs();
                            int numSubs = ((MultiPostingsEnum) postingsEnum).getNumSubs();
                            SEGMENTS_LOOP: for (int subindex = 0; subindex < numSubs; subindex++) {
                                MultiPostingsEnum.EnumWithSlice sub = subs[subindex];
                                if (sub.postingsEnum == null)
                                    continue;
                                int base = sub.slice.start;
                                int docid;
                                while ((docid = sub.postingsEnum.nextDoc()) != DocIdSetIterator.NO_MORE_DOCS) {
                                    if (fastForRandomSet.exists(docid + base)) {
                                        c++;
                                        if (intersectsCheck) {
                                            assert c == 1;
                                            break SEGMENTS_LOOP;
                                        }
                                    }
                                }
                            }
                        } else {
                            int docid;
                            while ((docid = postingsEnum.nextDoc()) != DocIdSetIterator.NO_MORE_DOCS) {
                                if (fastForRandomSet.exists(docid)) {
                                    c++;
                                    if (intersectsCheck) {
                                        assert c == 1;
                                        break;
                                    }
                                }
                            }
                        }
                    }
                    if (sortByCount) {
                        if (c > min) {
                            BytesRef termCopy = BytesRef.deepCopyOf(term);
                            queue.add(new CountPair<>(termCopy, c));
                            if (queue.size() >= maxsize)
                                min = queue.last().val;
                        }
                    } else {
                        if (c >= mincount && --off < 0) {
                            if (--lim < 0)
                                break;
                            ft.indexedToReadable(term, charsRef);
                            res.add(charsRef.toString(), c);
                        }
                    }
                }
            }
            term = termsEnum.next();
        }
    }
    if (sortByCount) {
        for (CountPair<BytesRef, Integer> p : queue) {
            if (--off >= 0)
                continue;
            if (--lim < 0)
                break;
            ft.indexedToReadable(p.key, charsRef);
            res.add(charsRef.toString(), p.val);
        }
    }
    if (missing) {
        res.add(null, getFieldMissingCount(searcher, docs, field));
    }
    return res;
}
Also used : SortedIntDocSet(org.apache.solr.search.SortedIntDocSet) HashDocSet(org.apache.solr.search.HashDocSet) TermsEnum(org.apache.lucene.index.TermsEnum) CharsRefBuilder(org.apache.lucene.util.CharsRefBuilder) MultiPostingsEnum(org.apache.lucene.index.MultiPostingsEnum) PostingsEnum(org.apache.lucene.index.PostingsEnum) BytesRef(org.apache.lucene.util.BytesRef) LeafReader(org.apache.lucene.index.LeafReader) NamedList(org.apache.solr.common.util.NamedList) Terms(org.apache.lucene.index.Terms) SolrIndexSearcher(org.apache.solr.search.SolrIndexSearcher) FieldType(org.apache.solr.schema.FieldType) Fields(org.apache.lucene.index.Fields) MultiPostingsEnum(org.apache.lucene.index.MultiPostingsEnum) IndexSchema(org.apache.solr.schema.IndexSchema) HashDocSet(org.apache.solr.search.HashDocSet) DocSet(org.apache.solr.search.DocSet) SortedIntDocSet(org.apache.solr.search.SortedIntDocSet) BitDocSet(org.apache.solr.search.BitDocSet)

Aggregations

NamedList (org.apache.solr.common.util.NamedList)440 Test (org.junit.Test)125 ArrayList (java.util.ArrayList)111 Map (java.util.Map)83 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)83 SolrException (org.apache.solr.common.SolrException)80 SimpleOrderedMap (org.apache.solr.common.util.SimpleOrderedMap)79 List (java.util.List)76 HashMap (java.util.HashMap)65 SolrQueryResponse (org.apache.solr.response.SolrQueryResponse)55 IOException (java.io.IOException)53 SolrDocumentList (org.apache.solr.common.SolrDocumentList)45 QueryRequest (org.apache.solr.client.solrj.request.QueryRequest)35 SolrQueryRequest (org.apache.solr.request.SolrQueryRequest)35 SolrParams (org.apache.solr.common.params.SolrParams)31 LocalSolrQueryRequest (org.apache.solr.request.LocalSolrQueryRequest)31 QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)30 SolrCore (org.apache.solr.core.SolrCore)30 HttpSolrClient (org.apache.solr.client.solrj.impl.HttpSolrClient)27 SolrIndexSearcher (org.apache.solr.search.SolrIndexSearcher)27