Show
Ignore:
Timestamp:
04/07/08 21:48:43 (8 months ago)
Author:
karpet
Message:

make output a little more swish-like

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • libswish3/trunk/src/xapian/swish_xapian.cpp

    r2111 r2112  
    7373/* global vars */ 
    7474static int debug = 0; 
    75 static Xapian::WritableDatabase     wdb; 
    76 static Xapian::Database::Database   rdb; 
    77 static Xapian::Stem stemmer("english"); // TODO make this configurable 
    78 static Xapian::TermGenerator        indexer; 
    79 static int                          twords = 0; 
    80 static int                          skip_duplicates = 0; 
    81 static int                          overwrite = 0; 
    82 static vector < bool >  updated; 
    83 static swish_3                      *s3; 
    84  
    85 extern int  SWISH_DEBUG; 
     75static 
     76    Xapian::WritableDatabase 
     77    wdb; 
     78static 
     79    Xapian::Database::Database 
     80    rdb; 
     81static 
     82    Xapian::Stem 
     83stemmer( 
     84    "english" 
     85);                              // TODO make this configurable 
     86static 
     87    Xapian::TermGenerator 
     88    indexer; 
     89static int 
     90    twords = 0; 
     91static int 
     92    skip_duplicates = 0; 
     93static int 
     94    overwrite = 0; 
     95static 
     96    vector < 
     97    bool > 
     98    updated; 
     99static swish_3 * 
     100    s3; 
     101 
     102extern int 
     103    SWISH_DEBUG; 
    86104 
    87105static struct option 
     
    183201} 
    184202 
    185 inline uint32_t 
     203inline 
     204    uint32_t 
    186205binary_string_to_int( 
    187206    const std::string & s 
     
    190209    if (s.size() != 4) 
    191210        return (uint32_t) - 1; 
    192     uint32_t 
    193         v; 
     211    uint32_t v; 
    194212    memcpy(&v, s.data(), 4); 
    195213    return ntohl(v); 
    196214} 
    197215 
    198 inline 
    199     std::string 
     216inline std::string 
    200217int_to_binary_string( 
    201218    uint32_t v 
     
    206223} 
    207224 
    208 static string 
     225static 
     226    string 
    209227get_prefix( 
    210228    xmlChar *metaname, 
     
    212230) 
    213231{ 
    214     string prefix; 
    215     swish_MetaName *meta = 
    216         (swish_MetaName *)swish_hash_fetch(config->metanames, metaname); 
     232    string 
     233        prefix; 
     234    swish_MetaName * 
     235        meta = (swish_MetaName *)swish_hash_fetch(config->metanames, metaname); 
    217236    prefix = int_to_string(meta->id); 
    218     return prefix + string((const char*)":"); 
     237    return prefix + string((const char *)":"); 
    219238} 
    220239 
    221240static void 
    222241add_prefix( 
    223     swish_MetaName      *meta, 
     242    swish_MetaName *meta, 
    224243    Xapian::QueryParser qp, 
    225     xmlChar             *name 
    226 ) 
    227 { 
    228     qp.add_prefix(string((const char*)name),  
    229                          int_to_string(meta->id) + string((const char*)":")); 
     244    xmlChar *name 
     245) 
     246{ 
     247    qp.add_prefix(string((const char *)name), 
     248                  int_to_string(meta->id) + string((const char *)":")); 
    230249} 
    231250 
     
    236255) 
    237256{ 
    238     unsigned int w; 
    239     swish_MetaName *meta = 
    240         (swish_MetaName *)swish_hash_fetch(config->metanames, metaname); 
     257    unsigned int 
     258        w; 
     259    swish_MetaName * 
     260        meta = (swish_MetaName *)swish_hash_fetch(config->metanames, metaname); 
    241261    return meta->bias > 0 ? meta->bias : 1;     // TODO need to account for negative values. 
    242262} 
     
    250270{ 
    251271    // lookup weight and prefix 
    252     string prefix = get_prefix(metaname, (swish_Config *)config); 
    253     unsigned int weight = get_weight(metaname, (swish_Config *)config); 
     272    string 
     273        prefix = get_prefix(metaname, (swish_Config *)config); 
     274    unsigned int 
     275        weight = get_weight(metaname, (swish_Config *)config); 
    254276    indexer.index_text((const char *)xmlBufferContent(buffer), weight, prefix); 
     277    // index swishdefault and swishtitle without any prefix too 
     278    if (xmlStrEqual(metaname, BAD_CAST SWISH_DEFAULT_METANAME) 
     279        || xmlStrEqual(metaname, BAD_CAST SWISH_TITLE_METANAME) 
     280        ) { 
     281        indexer.index_text((const char *)xmlBufferContent(buffer), weight); 
     282    } 
    255283} 
    256284 
     
    262290) 
    263291{ 
    264     swish_Property *prop; 
     292    swish_Property * 
     293        prop; 
    265294    prop = (swish_Property *)swish_hash_fetch(s3->config->properties, name); 
    266295    //SWISH_DEBUG_MSG("adding property %s [%d]: %s", name, prop->id, 
     
    285314    } 
    286315    if (SWISH_DEBUG & SWISH_DEBUG_NAMEDBUFFER) { 
    287         swish_debug_nb(parser_data->properties, (xmlChar *)"Property"); 
    288         swish_debug_nb(parser_data->metanames, (xmlChar *)"MetaName"); 
     316        swish_debug_nb(parser_data->properties, BAD_CAST "Property"); 
     317        swish_debug_nb(parser_data->metanames, BAD_CAST "MetaName"); 
    289318    } 
    290319 
    291320    // Put the data in the document 
    292321    Xapian::Document newdocument; 
    293     xmlChar *title = 
    294         (xmlChar *)swish_nb_get_value(parser_data->properties, 
    295                                       (xmlChar *)SWISH_PROP_TITLE); 
     322    xmlChar * 
     323        title = BAD_CAST swish_nb_get_value(parser_data->properties, 
     324                                              BAD_CAST SWISH_PROP_TITLE); 
    296325    //printf("title = %s", (char *)title); 
    297     string unique_id = SWISH_PREFIX_URL + string((const char *)parser_data->docinfo->uri); 
    298     string record = "url=" + string((const char *)parser_data->docinfo->uri); 
     326    string 
     327        unique_id = SWISH_PREFIX_URL + string((const char *)parser_data->docinfo->uri); 
     328    string 
     329        record = "url=" + string((const char *)parser_data->docinfo->uri); 
    299330    record += "\ntitle=" + string((const char *)title); 
    300331    record += "\ntype=" + string((const char *)parser_data->docinfo->mime); 
     
    310341    newdocument.add_term(unique_id); 
    311342 
    312     struct tm *tm = localtime(&(parser_data->docinfo->mtime)); 
    313     string date_term = 
    314         "D" + date_to_string(tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday); 
     343    struct tm * 
     344        tm = localtime(&(parser_data->docinfo->mtime)); 
     345    string 
     346        date_term = "D" + date_to_string(tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday); 
    315347    newdocument.add_term(date_term);    // Date (YYYYMMDD) 
    316348    date_term.resize(7); 
     
    339371    // add all metanames and properties 
    340372    xmlHashScan(parser_data->metanames->hash, (xmlHashScanner)add_metanames, s3->config); 
    341     xmlHashScan(parser_data->properties->hash, (xmlHashScanner)add_properties, 
    342                 &newdocument); 
     373    xmlHashScan(parser_data->properties->hash, (xmlHashScanner)add_properties, &newdocument); 
    343374 
    344375    if (!skip_duplicates) { 
     
    373404) 
    374405{ 
    375     int exitcode = 1; 
    376     string header; 
     406    int 
     407        exitcode = 1; 
     408    string 
     409        header; 
    377410    try { 
    378411        if (!overwrite) { 
     
    388421 
    389422        indexer.set_stemmer(stemmer); 
    390          
     423 
    391424        // read header if it exists 
    392         header = dbpath +  
    393                     string((const char*)SWISH_PATH_SEP) +  
    394                     string((const char*)SWISH_HEADER_FILE); 
    395         if (swish_file_exists((xmlChar*)header.c_str())) { 
    396             swish_merge_config_with_header((char*)header.c_str(), s3->config); 
     425        header = 
     426            dbpath + string((const char *)SWISH_PATH_SEP) + 
     427            string((const char *)SWISH_HEADER_FILE); 
     428        if (swish_file_exists(BAD_CAST header.c_str())) { 
     429            swish_merge_config_with_header((char *)header.c_str(), s3->config); 
    397430        } 
    398431 
     
    426459) 
    427460{ 
    428     int exitcode = 1; 
    429     string header; 
     461    int 
     462        exitcode = 1; 
     463    string 
     464        header; 
    430465    try { 
    431466        rdb = Xapian::Database::Database(dbpath); 
    432          
    433         header = dbpath +  
    434                     string((const char*)SWISH_PATH_SEP) +  
    435                     string((const char*)SWISH_HEADER_FILE); 
    436         if (swish_file_exists((xmlChar*)header.c_str())) { 
    437             swish_merge_config_with_header((char*)header.c_str(), s3->config); 
     467 
     468        header = 
     469            dbpath + string((const char *)SWISH_PATH_SEP) + 
     470            string((const char *)SWISH_HEADER_FILE); 
     471        if (swish_file_exists(BAD_CAST header.c_str())) { 
     472            swish_merge_config_with_header((char *)header.c_str(), s3->config); 
    438473        } 
    439474 
     
    464499) 
    465500{ 
    466     int total_matches; 
    467     Xapian::Enquire    *enquire; 
    468     Xapian::Query       query; 
     501    int 
     502        total_matches; 
     503    Xapian::Enquire * enquire; 
     504    Xapian::Query query; 
    469505    Xapian::QueryParser qparser; 
    470     Xapian::MSet        mset; 
     506    Xapian::MSet mset; 
    471507    Xapian::MSetIterator iterator; 
    472     Xapian::Document    doc; 
    473      
     508    Xapian::Document doc; 
     509 
    474510    total_matches = 0; 
    475     qparser.set_stemmer(stemmer);    // TODO make this configurable 
     511    qparser.set_stemmer(stemmer);       // TODO make this configurable 
    476512    qparser.set_database(rdb); 
    477      
     513 
    478514    // map all human metanames to internal prefix 
    479515    xmlHashScan(s3->config->metanames, (xmlHashScanner)add_prefix, &qparser); 
    480      
     516 
    481517    // TODO boolean_prefix? 
    482          
     518 
    483519    try { 
    484520        query = qparser.parse_query(string(qstr)); 
    485521    } 
    486     catch (Xapian::QueryParserError &e) { 
     522    catch(Xapian::QueryParserError & e) { 
    487523        SWISH_CROAK("query parser error: %s", e.get_msg().c_str()); 
    488524    } 
    489      
     525 
     526    // this is very simplistic. swish-e does paging etc. 
    490527    enquire = new Xapian::Enquire(rdb); 
    491528    enquire->set_query(query); 
    492529    mset = enquire->get_mset(0, 100); 
     530    printf("# %d estimated matches\n", mset.get_matches_estimated()); 
     531    cout << "# " + query.get_description() << endl; 
    493532    iterator = mset.begin(); 
    494     for ( ; iterator != mset.end(); ++iterator) { 
     533     
     534    // output format is simple, not as flexible as swish-e.  
     535    // But hey. It's an example. 
     536    for (; iterator != mset.end(); ++iterator) { 
    495537        doc = iterator.get_document(); 
    496         printf("ID %d %d%%\n[\n%s\n]\n",  
    497                 iterator.operator*(), iterator.get_percent(), doc.get_data().c_str()); 
     538        printf("%3d0 %s \"%s\" %s\n", iterator.get_percent(), 
     539               doc.get_value(SWISH_PROP_DOCPATH_ID).c_str(), 
     540               doc.get_value(SWISH_PROP_TITLE_ID).c_str(), 
     541               doc.get_value(SWISH_PROP_SIZE_ID).c_str() 
     542            ); 
    498543        total_matches++; 
    499544    } 
    500     printf("%d total matches\n", total_matches); 
     545     
     546    //printf("# %d total matches\n", total_matches); 
    501547} 
    502548 
     
    506552{ 
    507553 
    508     char *descr = "swish_xapian is an example program for using libswish3 with Xapian\n"; 
     554    char * 
     555        descr = "swish_xapian is an example program for using libswish3 with Xapian\n"; 
    509556    printf("swish_xapian [opts] [- | file(s)]\n"); 
    510557    printf("opts:\n --config conf_file.xml\n --query <query>\n --debug [lvl]\n --help\n"); 
     
    520567) 
    521568{ 
    522     int i, ch; 
    523     extern char *optarg; 
    524     extern int optind; 
    525     int option_index; 
    526     int files; 
    527     char *etime; 
    528     char *query; 
    529     char *dbpath; 
    530     string header; 
    531     double start_time; 
    532     xmlChar *config_file; 
    533      
     569    int 
     570        i, 
     571        ch; 
     572    extern char * 
     573        optarg; 
     574    extern int 
     575        optind; 
     576    int 
     577        option_index; 
     578    int 
     579        files; 
     580    char * 
     581        etime; 
     582    char * 
     583        query; 
     584    char * 
     585        dbpath; 
     586    string 
     587        header; 
     588    double 
     589        start_time; 
     590    xmlChar * 
     591        config_file; 
     592 
    534593    config_file = NULL; 
    535594    option_index = 0; 
     
    554613        case 'c': 
    555614            //printf("optarg = %s\n", optarg); 
    556             config_file = swish_xstrdup((xmlChar *)optarg); 
     615            config_file = swish_xstrdup(BAD_CAST optarg); 
    557616            break; 
    558617 
     
    571630 
    572631        case 'i': 
    573             dbpath = (char *)swish_xstrdup((xmlChar *)optarg); 
     632            dbpath = (char *)swish_xstrdup(BAD_CAST optarg); 
    574633            break; 
    575634 
     
    579638 
    580639        case 'q': 
    581             query = (char *)swish_xstrdup((xmlChar *)optarg); 
     640            query = (char *)swish_xstrdup(BAD_CAST optarg); 
    582641            break; 
    583642 
     
    611670 
    612671    if (!dbpath) { 
    613         dbpath = (char *)swish_xstrdup((xmlChar *)SWISH_INDEX_FILENAME); 
     672        dbpath = (char *)swish_xstrdup(BAD_CAST SWISH_INDEX_FILENAME); 
    614673    } 
    615674 
    616675    // indexing mode 
    617676    if (!query) { 
    618          
     677 
    619678        open_writeable_index(dbpath); 
    620      
     679 
    621680        for (; i < argc; i++) { 
    622681            if (argv[i][0] != '-') { 
     
    641700        // but also if it is not (defaults). 
    642701        // so we re-write every time we have a writeable db. 
    643         header = dbpath +  
    644                     string((const char*)SWISH_PATH_SEP) +  
    645                     string((const char*)SWISH_HEADER_FILE); 
    646         swish_write_header((char*)header.c_str(), s3->config); 
     702        header = 
     703            dbpath + string((const char *)SWISH_PATH_SEP) + 
     704            string((const char *)SWISH_HEADER_FILE); 
     705        swish_write_header((char *)header.c_str(), s3->config); 
    647706 
    648707    } 
     
    656715 
    657716    etime = swish_print_time(swish_time_elapsed() - start_time); 
    658     printf("%s total time\n\n", etime); 
     717    printf("# %s total time\n\n", etime); 
    659718    swish_xfree(etime); 
    660719    swish_xfree(dbpath); 
  • libswish3/trunk/src/xapian/test.pl

    r2111 r2112  
    1111 
    1212# searching 
    13 ok( ( grep {m/2 total matches/} run(' --query swishtitle:foobar') ), 
     13ok( ( grep {m/2 estimated matches/} run(' --query swishtitle:foobar') ), 
    1414    'search swishtitle:foobar' ); 
    1515