コード例 #1
0
ファイル: QueryResult.cpp プロジェクト: Web5design/firtex2
void QueryResult::init(const FieldSelector& selector, 
                       const IndexReaderPtr& pIndexReader,
                       const QueryHits& hits)
{
    StoredFieldsReaderPtr pStoredFieldsReader = pIndexReader->createStoredFieldsReader();
    if (pStoredFieldsReader.isNull())
    {
        return;
    }

    TimeProbe probe;
    probe.start();

    m_docs.reserve(hits.size());
    setTracer(hits.getTracer());

    setTotalHits(hits.getTotalHits());

    QueryHits::Iterator it = hits.iterator();
    while (it.hasNext())
    {
        const QueryHits::HitDoc& hitDoc = it.next();
        
        ResultDocPtr pResDoc(new ResultDoc(hitDoc.getDocId(), 
                        hitDoc.getScore(), selector.size()));
        addDoc(pResDoc);

        pStoredFieldsReader->getDocument(selector, *pResDoc);
    }

    probe.stop();
    FX_QUERY_TRACE(INFO, getTracer(), "fetch field time [%d] ms",
                   (int32_t)probe.elapsed() / 1000);
}
コード例 #2
0
void IndexBuilderAppRunner::buildIndex()
{
    try
    {
        cout << _T("Begin build index, config file: ") << m_sConfFile << endl;
        TimeProbe p;
        p.start();
        ProgressObserverPtr pOb(new ConsoleProgressObserver(1000));
        FX_NS(collection)::Collection::buildIndex(m_sConfFile, pOb);
        p.stop();
        cout << _T("Build index success! finished in ") << p.elapsedSeconds() 
             << _T(" s.") << endl;
    }
    catch(const FirteXException& fe)
    {
        cout << _T("Build index FAILED: ") << fe.what() << endl;
    }
}
コード例 #3
0
void HTTPSearchService::handleQuery(const Statement& state,
                                EvHttpRequestContext* pCtx) const
{
    IndexReaderPtr pIndexReader = m_searchRes.getIndexReader();
    FIRTEX_ASSERT2(pIndexReader.isNotNull());

    try
    {
        TimeProbe probe;
        probe.start();

        QueryParser parser(pIndexReader->getAnalyzerMapper(),
                           m_searchRes.getDefaultField());

        IndexSearcher searcher(pIndexReader);
        QueryHitsPtr pHits = searcher.search(state, parser);

        QueryResult result;

        if (pHits.isNotNull())
        {
            FieldSelectClausePtr pFieldClause = state.getFieldSelectClause();
            QueryClausePtr pQueryClause = state.getQueryClause();
            if (pFieldClause.isNotNull() && pQueryClause.isNotNull())
            {
                QueryPtr pQuery = parser.parse(pQueryClause->getQueryString());
                FIRTEX_ASSERT2(pQuery.isNotNull());

                FieldSelector selector(pIndexReader->getDocSchema());
                
                for (size_t i = 0; i < pFieldClause->getFieldCount(); ++i)
                {
                    const FieldSelectClause::SnippetParam& param = pFieldClause->getField(i);
                    FieldFilterPtr pFieldFilter;
                    if (param.snippet)
                    {
                        SnippetGenerator* pSnippetGen = new SnippetGenerator();
                        pFieldFilter.reset(pSnippetGen);
                        
                        if (!pSnippetGen->init(pQuery, parser.getAnalyzerMapper(), param.field,
                                        param.preTag, param.postTag, param.separator))
                        {
                            FX_LOG(ERROR, "Init snippet generator for field: [%s] FAILED", param.field.c_str());
                            sendErrorMessage("Init snippet generator for field: " + 
                                    param.field + " FAILED", pCtx);
                            return;
                        }                        
                    }

                    if (!selector.addField(param.field, pFieldFilter))
                    {
                        FX_LOG(ERROR, "Invalid field: [%s]", param.field.c_str());
                    }
                }
                result.init(selector, pIndexReader, *pHits);
            }
            else
            {
                result.init(pIndexReader, *pHits);
            }
        }

        probe.stop();
        result.setTimeCost(probe.elapsed() / 1000);
        FX_QUERY_TRACE(INFO, result.getTracer(), "search phase time [%d]",
                       (int32_t)result.getTimeCost());

        stringstream ss;
        XMLResultFormatter formatter;
        formatter.format(result, ss);
        sendResponse(ss.str(), pCtx);
    }
    catch(const FirteXException& e)
    {
        FX_LOG(ERROR, "Handle request FAILED: [%s], reason: [%s]",
               pCtx->getQuery().c_str(), e.what().c_str());
        sendErrorMessage("Handle request failed", pCtx);
    }
}