示例#1
0
void DateTimeIndexTestCase::testQuery()
{
    const size_t NUM_DOCS = 12;
    stringstream ss;
    for (size_t i = 0; i < NUM_DOCS; ++i)
    {
        ss << 2009 << "-" << i % 12 + 1 << "-" << i % 27 + 1 << " " 
           << i % 24 << ":" << i % 60 << ":" << i % 60 << ";";
        if (i % 10 == 0)
        {
            ss << 2009 << "-" << i % 12 + 1 << "-" << i % 27 + 1 << " " 
               << i % 24 << ":" << i % 60 << ":" << i % 60 << ";";
        }
    }

    buildDateTimeIndex(ss.str());

    Index index;
    index.open(getIndexPath(), Index::READ, NULL);
    IndexReaderPtr pReader = index.acquireReader();
    IndexSearcher se(pReader);

    QueryParser queryParser(pReader->getAnalyzerMapper(), 
                    "DateTime1", QueryParser::OP_AND);
    QueryHitsPtr pHits = se.search("query=\'2009-2-2 1:1:1\'", queryParser);
    CPPUNIT_ASSERT(pHits);
    uint64_t uTotalHits =  pHits->getTotalHits();
    CPPUNIT_ASSERT_EQUAL((uint64_t)1, uTotalHits);

    pHits = se.search("query=\'2009-11-11 10:10:10\'", queryParser);
    CPPUNIT_ASSERT(pHits);
    uTotalHits =  pHits->getTotalHits();
    CPPUNIT_ASSERT_EQUAL((uint64_t)2, uTotalHits);
}
示例#2
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);
    }
}