void ResultCollectorImpl::collect(const QueryResult& queryResults)
{
    if (queryResults.hasError())
    {
        m_result.setError(m_result.getErrorMsg() + queryResults.getErrorMsg());
    }
    else
    {
        m_result.setTotalHits(m_result.getTotalHits() + queryResults.getTotalHits());
        if (queryResults.getTimeCost() > m_result.getTimeCost())
        {
            m_result.setTimeCost(queryResults.getTimeCost());
        }

        shardid_t shardId = -1;
        QueryResult::Iterator it = queryResults.iterator();
        while (it.hasNext())
        {
            const ResultDocPtr& pDoc = it.next();
            m_pResultQueue->insert(pDoc);
            shardId = pDoc->getShardId();
        }
        
        const QueryTracerPtr& pTracer = queryResults.getTracer();
        if (pTracer.isNotNull())
        {
            if (m_result.getTracer().isNull())
            {
                m_result.setTracer(new QueryTracer("proxy", pTracer->getLevel()));
            }
            QueryTracerPtr& pResTracer = m_result.getTracer();
            string str;
            NumberFormatter::append(str, shardId);
            pResTracer->addChildTracer(pTracer)->prependPath(str);
        }
    }
}
Beispiel #2
0
void XMLResultFormatter::format(const QueryResult& result,
                                std::stringstream& ss)
{
    m_xmlDoc.clear();

    m_xmlDoc.addDeclarationNode(result.getEncoding());
    XMLNodeWrapperPtr pRootNode = m_xmlDoc.appendNode(
            XMLDocumentWrapper::NODE_ELEMENT, "result");
    if (result.hasError())
    {
        formatError(pRootNode, result.getErrorMsg());
    }
    else 
    {
        XMLNodeWrapperPtr resultNode = pRootNode->appendNode(
                XMLDocumentWrapper::NODE_ELEMENT, "hits");
    
        string str;
        NumberFormatter::append(str, (int32_t)result.size());
        resultNode->appendAttribute("number_hits", str);

        str.clear();
        NumberFormatter::append(str, result.getTotalHits());
        resultNode->appendAttribute("total_hits", str);

        str.clear();
        NumberFormatter::append(str, result.getTimeCost());
        resultNode->appendAttribute("cost", str);

        QueryResult::Iterator it = result.iterator();
        while (it.hasNext())
        {
            XMLNodeWrapperPtr pHitNode = resultNode->appendNode(
                    XMLDocumentWrapper::NODE_ELEMENT, "hit");

            const ResultDocPtr& pResDoc = it.next();

            if (result.hasShardId())
            {
                str.clear();
                NumberFormatter::append(str, pResDoc->getShardId());
                pHitNode->appendNode(XMLDocumentWrapper::NODE_ELEMENT,
                        "shardid", str);
            }

            if (result.hasDocId())
            {
                str.clear();
                NumberFormatter::append(str, pResDoc->getDocId());
                pHitNode->appendNode(XMLDocumentWrapper::NODE_ELEMENT,
                        "docid", str);
            }

            if (result.hasScore())
            {
                str.clear();
                NumberFormatter::append(str, pResDoc->getScore(), 2);
                pHitNode->appendNode(XMLDocumentWrapper::NODE_ELEMENT,
                        "score", str);
            }

            if (result.hasFields())
            {
                XMLNodeWrapperPtr pFieldsNode = pHitNode->appendNode(
                        XMLDocumentWrapper::NODE_ELEMENT, "fields");

                ResultDoc::Iterator fieldIt = pResDoc->iterator();
                while (fieldIt.hasNext())
                {
                    const ResultDoc::Field& field = fieldIt.next();

                    XMLNodeWrapperPtr pCDataNode = pFieldsNode->appendNode(
                            XMLDocumentWrapper::NODE_ELEMENT, field.first);
                    pCDataNode->appendNode(XMLDocumentWrapper::NODE_CDATA,
                            "", field.second);
                }
            }
        } // end while 

        const QueryTracerPtr& pTracer = result.getTracer();
        if (pTracer)
        {
            formatTracer(pRootNode, pTracer);
        }
    }
    m_xmlDoc.print(ss);
}