HTTPSearchService::HTTPSearchService(const SearchResource& searchRes) : m_searchRes(searchRes) { IndexReaderPtr pIndexReader = m_searchRes.getIndexReader(); FIRTEX_ASSERT2(pIndexReader.isNotNull()); m_sEncoding = pIndexReader->getEncoding(); }
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); } }