Search in sources :

Example 1 with DocTransformer

use of org.apache.solr.response.transform.DocTransformer in project lucene-solr by apache.

the class RealTimeGetComponent method process.

@Override
public void process(ResponseBuilder rb) throws IOException {
    SolrQueryRequest req = rb.req;
    SolrQueryResponse rsp = rb.rsp;
    SolrParams params = req.getParams();
    CloudDescriptor cloudDesc = req.getCore().getCoreDescriptor().getCloudDescriptor();
    if (cloudDesc != null) {
        Replica.Type replicaType = cloudDesc.getReplicaType();
        if (replicaType != null) {
            if (replicaType == Replica.Type.PULL) {
                throw new SolrException(ErrorCode.BAD_REQUEST, String.format(Locale.ROOT, "%s can't handle realtime get requests. Replicas of type %s do not support these type of requests", cloudDesc.getCoreNodeName(), Replica.Type.PULL));
            }
        // non-leader TLOG replicas should not respond to distrib /get requests, but internal requests are OK
        }
    }
    if (!params.getBool(COMPONENT_NAME, true)) {
        return;
    }
    // This seems rather kludgey, may there is better way to indicate
    // that replica can support handling version ranges
    String val = params.get("checkCanHandleVersionRanges");
    if (val != null) {
        rb.rsp.add("canHandleVersionRanges", true);
        return;
    }
    val = params.get("getFingerprint");
    if (val != null) {
        processGetFingeprint(rb);
        return;
    }
    val = params.get("getVersions");
    if (val != null) {
        processGetVersions(rb);
        return;
    }
    val = params.get("getUpdates");
    if (val != null) {
        // solrcloud_debug
        if (log.isDebugEnabled()) {
            try {
                RefCounted<SolrIndexSearcher> searchHolder = req.getCore().getNewestSearcher(false);
                SolrIndexSearcher searcher = searchHolder.get();
                try {
                    log.debug(req.getCore().getCoreContainer().getZkController().getNodeName() + " min count to sync to (from most recent searcher view) " + searcher.search(new MatchAllDocsQuery(), 1).totalHits);
                } finally {
                    searchHolder.decref();
                }
            } catch (Exception e) {
                log.debug("Error in solrcloud_debug block", e);
            }
        }
        processGetUpdates(rb);
        return;
    }
    val = params.get("getInputDocument");
    if (val != null) {
        processGetInputDocument(rb);
        return;
    }
    final IdsRequsted reqIds = IdsRequsted.parseParams(req);
    if (reqIds.allIds.isEmpty()) {
        return;
    }
    // parse any existing filters
    try {
        String[] fqs = req.getParams().getParams(CommonParams.FQ);
        if (fqs != null && fqs.length != 0) {
            List<Query> filters = rb.getFilters();
            // if filters already exists, make a copy instead of modifying the original
            filters = filters == null ? new ArrayList<Query>(fqs.length) : new ArrayList<>(filters);
            for (String fq : fqs) {
                if (fq != null && fq.trim().length() != 0) {
                    QParser fqp = QParser.getParser(fq, req);
                    filters.add(fqp.getQuery());
                }
            }
            if (!filters.isEmpty()) {
                rb.setFilters(filters);
            }
        }
    } catch (SyntaxError e) {
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, e);
    }
    final SolrCore core = req.getCore();
    SchemaField idField = core.getLatestSchema().getUniqueKeyField();
    FieldType fieldType = idField.getType();
    SolrDocumentList docList = new SolrDocumentList();
    UpdateLog ulog = core.getUpdateHandler().getUpdateLog();
    SearcherInfo searcherInfo = new SearcherInfo(core);
    // this is initialized & set on the context *after* any searcher (re-)opening
    ResultContext resultContext = null;
    final DocTransformer transformer = rsp.getReturnFields().getTransformer();
    // true in any situation where we have to use a realtime searcher rather then returning docs
    // directly from the UpdateLog
    final boolean mustUseRealtimeSearcher = // if we have filters, we need to check those against the indexed form of the doc
    (rb.getFilters() != null) || ((null != transformer) && transformer.needsSolrIndexSearcher());
    try {
        BytesRefBuilder idBytes = new BytesRefBuilder();
        for (String idStr : reqIds.allIds) {
            fieldType.readableToIndexed(idStr, idBytes);
            if (ulog != null) {
                Object o = ulog.lookup(idBytes.get());
                if (o != null) {
                    // should currently be a List<Oper,Ver,Doc/Id>
                    List entry = (List) o;
                    assert entry.size() >= 3;
                    int oper = (Integer) entry.get(UpdateLog.FLAGS_IDX) & UpdateLog.OPERATION_MASK;
                    switch(oper) {
                        // fall through to ADD
                        case UpdateLog.UPDATE_INPLACE:
                        case UpdateLog.ADD:
                            if (mustUseRealtimeSearcher) {
                                // close handles to current searchers & result context
                                searcherInfo.clear();
                                resultContext = null;
                                // force open a new realtime searcher
                                ulog.openRealtimeSearcher();
                                // pretend we never found this record and fall through to use the searcher
                                o = null;
                                break;
                            }
                            SolrDocument doc;
                            if (oper == UpdateLog.ADD) {
                                doc = toSolrDoc((SolrInputDocument) entry.get(entry.size() - 1), core.getLatestSchema());
                            } else if (oper == UpdateLog.UPDATE_INPLACE) {
                                assert entry.size() == 5;
                                // For in-place update case, we have obtained the partial document till now. We need to
                                // resolve it to a full document to be returned to the user.
                                doc = resolveFullDocument(core, idBytes.get(), rsp.getReturnFields(), (SolrInputDocument) entry.get(entry.size() - 1), entry, null);
                                if (doc == null) {
                                    // document has been deleted as the resolve was going on
                                    break;
                                }
                            } else {
                                throw new SolrException(ErrorCode.INVALID_STATE, "Expected ADD or UPDATE_INPLACE. Got: " + oper);
                            }
                            if (transformer != null) {
                                // unknown docID
                                transformer.transform(doc, -1, 0);
                            }
                            docList.add(doc);
                            break;
                        case UpdateLog.DELETE:
                            break;
                        default:
                            throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Unknown Operation! " + oper);
                    }
                    if (o != null)
                        continue;
                }
            }
            // didn't find it in the update log, so it should be in the newest searcher opened
            searcherInfo.init();
            // don't bother with ResultContext yet, we won't need it if doc doesn't match filters
            int docid = -1;
            long segAndId = searcherInfo.getSearcher().lookupId(idBytes.get());
            if (segAndId >= 0) {
                int segid = (int) segAndId;
                LeafReaderContext ctx = searcherInfo.getSearcher().getTopReaderContext().leaves().get((int) (segAndId >> 32));
                docid = segid + ctx.docBase;
                if (rb.getFilters() != null) {
                    for (Query raw : rb.getFilters()) {
                        Query q = raw.rewrite(searcherInfo.getSearcher().getIndexReader());
                        Scorer scorer = searcherInfo.getSearcher().createWeight(q, false, 1f).scorer(ctx);
                        if (scorer == null || segid != scorer.iterator().advance(segid)) {
                            // filter doesn't match.
                            docid = -1;
                            break;
                        }
                    }
                }
            }
            if (docid < 0)
                continue;
            Document luceneDocument = searcherInfo.getSearcher().doc(docid, rsp.getReturnFields().getLuceneFieldNames());
            SolrDocument doc = toSolrDoc(luceneDocument, core.getLatestSchema());
            SolrDocumentFetcher docFetcher = searcherInfo.getSearcher().getDocFetcher();
            docFetcher.decorateDocValueFields(doc, docid, docFetcher.getNonStoredDVs(true));
            if (null != transformer) {
                if (null == resultContext) {
                    // either first pass, or we've re-opened searcher - either way now we setContext
                    resultContext = new RTGResultContext(rsp.getReturnFields(), searcherInfo.getSearcher(), req);
                    transformer.setContext(resultContext);
                }
                transformer.transform(doc, docid, 0);
            }
            docList.add(doc);
        }
    } finally {
        searcherInfo.clear();
    }
    addDocListToResponse(rb, docList);
}
Also used : ResultContext(org.apache.solr.response.ResultContext) Query(org.apache.lucene.search.Query) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) SolrCore(org.apache.solr.core.SolrCore) DocTransformer(org.apache.solr.response.transform.DocTransformer) ArrayList(java.util.ArrayList) Scorer(org.apache.lucene.search.Scorer) Document(org.apache.lucene.document.Document) SolrInputDocument(org.apache.solr.common.SolrInputDocument) SolrDocument(org.apache.solr.common.SolrDocument) SolrInputDocument(org.apache.solr.common.SolrInputDocument) SolrDocument(org.apache.solr.common.SolrDocument) SyntaxError(org.apache.solr.search.SyntaxError) UpdateLog(org.apache.solr.update.UpdateLog) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) SolrDocumentList(org.apache.solr.common.SolrDocumentList) DocList(org.apache.solr.search.DocList) List(java.util.List) ArrayList(java.util.ArrayList) NamedList(org.apache.solr.common.util.NamedList) SolrException(org.apache.solr.common.SolrException) SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) BytesRefBuilder(org.apache.lucene.util.BytesRefBuilder) SolrIndexSearcher(org.apache.solr.search.SolrIndexSearcher) SolrDocumentList(org.apache.solr.common.SolrDocumentList) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) Replica(org.apache.solr.common.cloud.Replica) CloudDescriptor(org.apache.solr.cloud.CloudDescriptor) SolrException(org.apache.solr.common.SolrException) IOException(java.io.IOException) IndexFingerprint(org.apache.solr.update.IndexFingerprint) FieldType(org.apache.solr.schema.FieldType) SchemaField(org.apache.solr.schema.SchemaField) SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) QParser(org.apache.solr.search.QParser) SolrParams(org.apache.solr.common.params.SolrParams) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) SolrDocumentFetcher(org.apache.solr.search.SolrDocumentFetcher)

Example 2 with DocTransformer

use of org.apache.solr.response.transform.DocTransformer in project lucene-solr by apache.

the class SolrReturnFields method add.

private void add(String fl, NamedList<String> rename, DocTransformers augmenters, SolrQueryRequest req) {
    if (fl == null) {
        return;
    }
    try {
        StrParser sp = new StrParser(fl);
        for (; ; ) {
            sp.opt(',');
            sp.eatws();
            if (sp.pos >= sp.end)
                break;
            int start = sp.pos;
            // short circuit test for a really simple field name
            String key = null;
            String field = getFieldName(sp);
            char ch = sp.ch();
            if (field != null) {
                if (sp.opt(':')) {
                    // this was a key, not a field name
                    key = field;
                    field = null;
                    sp.eatws();
                    start = sp.pos;
                } else {
                    if (Character.isWhitespace(ch) || ch == ',' || ch == 0) {
                        addField(field, key, augmenters, false);
                        continue;
                    }
                    // an invalid field name... reset the position pointer to retry
                    sp.pos = start;
                    field = null;
                }
            }
            if (key != null) {
                // we read "key : "
                field = sp.getId(null);
                ch = sp.ch();
                if (field != null && (Character.isWhitespace(ch) || ch == ',' || ch == 0)) {
                    rename.add(field, key);
                    addField(field, key, augmenters, false);
                    continue;
                }
                // an invalid field name... reset the position pointer to retry
                sp.pos = start;
                field = null;
            }
            if (field == null) {
                // We didn't find a simple name, so let's see if it's a globbed field name.
                // Globbing only works with field names of the recommended form (roughly like java identifiers)
                field = sp.getGlobbedId(null);
                ch = sp.ch();
                if (field != null && (Character.isWhitespace(ch) || ch == ',' || ch == 0)) {
                    // "*" looks and acts like a glob, but we give it special treatment
                    if ("*".equals(field)) {
                        _wantsAllFields = true;
                    } else {
                        globs.add(field);
                    }
                    continue;
                }
                // an invalid glob
                sp.pos = start;
            }
            String funcStr = sp.val.substring(start);
            if (funcStr.startsWith("[")) {
                ModifiableSolrParams augmenterParams = new ModifiableSolrParams();
                int end = QueryParsing.parseLocalParams(funcStr, 0, augmenterParams, req.getParams(), "[", ']');
                sp.pos += end;
                // [foo] is short for [type=foo] in localParams syntax
                String augmenterName = augmenterParams.get("type");
                augmenterParams.remove("type");
                String disp = key;
                if (disp == null) {
                    disp = '[' + augmenterName + ']';
                }
                TransformerFactory factory = req.getCore().getTransformerFactory(augmenterName);
                if (factory != null) {
                    DocTransformer t = factory.create(disp, augmenterParams, req);
                    if (t != null) {
                        if (!_wantsAllFields) {
                            String[] extra = t.getExtraRequestFields();
                            if (extra != null) {
                                for (String f : extra) {
                                    // also request this field from IndexSearcher
                                    fields.add(f);
                                }
                            }
                        }
                        augmenters.addTransformer(t);
                    }
                } else {
                //throw new SolrException(ErrorCode.BAD_REQUEST, "Unknown DocTransformer: "+augmenterName);
                }
                addField(field, disp, augmenters, true);
                continue;
            }
            // let's try it as a function instead
            QParser parser = QParser.getParser(funcStr, FunctionQParserPlugin.NAME, req);
            Query q = null;
            ValueSource vs = null;
            try {
                if (parser instanceof FunctionQParser) {
                    FunctionQParser fparser = (FunctionQParser) parser;
                    fparser.setParseMultipleSources(false);
                    fparser.setParseToEnd(false);
                    q = fparser.getQuery();
                    if (fparser.localParams != null) {
                        if (fparser.valFollowedParams) {
                            // need to find the end of the function query via the string parser
                            int leftOver = fparser.sp.end - fparser.sp.pos;
                            // reset our parser to the same amount of leftover
                            sp.pos = sp.end - leftOver;
                        } else {
                            // the value was via the "v" param in localParams, so we need to find
                            // the end of the local params themselves to pick up where we left off
                            sp.pos = start + fparser.localParamsEnd;
                        }
                    } else {
                        // need to find the end of the function query via the string parser
                        int leftOver = fparser.sp.end - fparser.sp.pos;
                        // reset our parser to the same amount of leftover
                        sp.pos = sp.end - leftOver;
                    }
                } else {
                    // A QParser that's not for function queries.
                    // It must have been specified via local params.
                    q = parser.getQuery();
                    assert parser.getLocalParams() != null;
                    sp.pos = start + parser.localParamsEnd;
                }
                funcStr = sp.val.substring(start, sp.pos);
                if (q instanceof FunctionQuery) {
                    vs = ((FunctionQuery) q).getValueSource();
                } else {
                    vs = new QueryValueSource(q, 0.0f);
                }
                if (key == null) {
                    SolrParams localParams = parser.getLocalParams();
                    if (localParams != null) {
                        key = localParams.get("key");
                    }
                }
                if (key == null) {
                    key = funcStr;
                }
                addField(funcStr, key, augmenters, true);
                augmenters.addTransformer(new ValueSourceAugmenter(key, parser, vs));
            } catch (SyntaxError e) {
                // try again, simple rules for a field name with no whitespace
                sp.pos = start;
                field = sp.getSimpleString();
                if (req.getSchema().getFieldOrNull(field) != null) {
                    // OK, it was an oddly named field
                    addField(field, key, augmenters, false);
                    if (key != null) {
                        rename.add(field, key);
                    }
                } else {
                    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Error parsing fieldname: " + e.getMessage(), e);
                }
            }
        // end try as function
        }
    // end for(;;)
    } catch (SyntaxError e) {
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Error parsing fieldname", e);
    }
}
Also used : TransformerFactory(org.apache.solr.response.transform.TransformerFactory) Query(org.apache.lucene.search.Query) FunctionQuery(org.apache.lucene.queries.function.FunctionQuery) DocTransformer(org.apache.solr.response.transform.DocTransformer) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) FunctionQuery(org.apache.lucene.queries.function.FunctionQuery) ValueSource(org.apache.lucene.queries.function.ValueSource) QueryValueSource(org.apache.lucene.queries.function.valuesource.QueryValueSource) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) SolrParams(org.apache.solr.common.params.SolrParams) QueryValueSource(org.apache.lucene.queries.function.valuesource.QueryValueSource) ValueSourceAugmenter(org.apache.solr.response.transform.ValueSourceAugmenter) SolrException(org.apache.solr.common.SolrException)

Aggregations

Query (org.apache.lucene.search.Query)2 SolrException (org.apache.solr.common.SolrException)2 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)2 SolrParams (org.apache.solr.common.params.SolrParams)2 DocTransformer (org.apache.solr.response.transform.DocTransformer)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Document (org.apache.lucene.document.Document)1 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)1 FunctionQuery (org.apache.lucene.queries.function.FunctionQuery)1 ValueSource (org.apache.lucene.queries.function.ValueSource)1 QueryValueSource (org.apache.lucene.queries.function.valuesource.QueryValueSource)1 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)1 Scorer (org.apache.lucene.search.Scorer)1 BytesRefBuilder (org.apache.lucene.util.BytesRefBuilder)1 CloudDescriptor (org.apache.solr.cloud.CloudDescriptor)1 SolrDocument (org.apache.solr.common.SolrDocument)1 SolrDocumentList (org.apache.solr.common.SolrDocumentList)1 SolrInputDocument (org.apache.solr.common.SolrInputDocument)1