// // $Id$ // // // Copyright (c) 2001-2011, Andrew Aksyonoff // Copyright (c) 2008-2011, Sphinx Technologies Inc // All rights reserved // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU Library General Public License. You should // have received a copy of the LGPL license along with this program; if you // did not, you can find it at http://www.gnu.org/ // #include #include #include #if _WIN32 #include #endif #include "sphinxclient.h" static sphinx_bool g_smoke = SPH_FALSE; static int g_failed = 0; void die ( const char * template, ... ) { va_list ap; va_start ( ap, template ); printf ( "FATAL: " ); vprintf ( template, ap ); printf ( "\n" ); va_end ( ap ); exit ( 1 ); } void net_init () { #if _WIN32 // init WSA on Windows WSADATA wsa_data; int wsa_startup_err; wsa_startup_err = WSAStartup ( WINSOCK_VERSION, &wsa_data ); if ( wsa_startup_err ) die ( "failed to initialize WinSock2: error %d", wsa_startup_err ); #endif } void test_query ( sphinx_client * client, const char * query ) { sphinx_result * res; const char *index; int i, j, k, mva_len; unsigned int * mva; const char * field_names[2]; int field_weights[2]; index = "test1"; field_names[0] = "title"; field_names[1] = "content"; field_weights[0] = 100; field_weights[1] = 1; sphinx_set_field_weights ( client, 2, field_names, field_weights ); field_weights[0] = 1; field_weights[1] = 1; res = sphinx_query ( client, query, index, NULL ); if ( !res ) { g_failed += ( res==NULL ); if ( !g_smoke ) die ( "query failed: %s", sphinx_error(client) ); } if ( g_smoke ) printf ( "Query '%s' retrieved %d of %d matches.\n", query, res->total, res->total_found ); else printf ( "Query '%s' retrieved %d of %d matches in %d.%03d sec.\n", query, res->total, res->total_found, res->time_msec/1000, res->time_msec%1000 ); printf ( "Query stats:\n" ); for ( i=0; inum_words; i++ ) printf ( "\t'%s' found %d times in %d documents\n", res->words[i].word, res->words[i].hits, res->words[i].docs ); printf ( "\nMatches:\n" ); for ( i=0; inum_matches; i++ ) { printf ( "%d. doc_id=%d, weight=%d", 1+i, (int)sphinx_get_id ( res, i ), sphinx_get_weight ( res, i ) ); for ( j=0; jnum_attrs; j++ ) { printf ( ", %s=", res->attr_names[j] ); switch ( res->attr_types[j] ) { case SPH_ATTR_MULTI64: case SPH_ATTR_MULTI: mva = sphinx_get_mva ( res, i, j ); mva_len = *mva++; printf ( "(" ); for ( k=0; kattr_types[j]==SPH_ATTR_MULTI ? mva[k] : (unsigned int)sphinx_get_mva64_value ( mva, k ) ) ); printf ( ")" ); break; case SPH_ATTR_FLOAT: printf ( "%f", sphinx_get_float ( res, i, j ) ); break; case SPH_ATTR_STRING: printf ( "%s", sphinx_get_string ( res, i, j ) ); break; default: printf ( "%u", (unsigned int)sphinx_get_int ( res, i, j ) ); break; } } printf ( "\n" ); } printf ( "\n" ); } void test_excerpt ( sphinx_client * client ) { const char * docs[] = { "this is my test text to be highlighted, and for the sake of the testing we need to pump its length somewhat", "another test text to be highlighted, below limit", "test number three, without phrase match", "final test, not only without phrase match, but also above limit and with swapped phrase text test as well" }; const int ndocs = sizeof(docs)/sizeof(docs[0]); const char * words = "test text"; const char * index = "test1"; sphinx_excerpt_options opts; char ** res; int i, j; sphinx_init_excerpt_options ( &opts ); opts.limit = 60; opts.around = 3; opts.allow_empty = SPH_TRUE; for ( j=0; j<2; j++ ) { opts.exact_phrase = j; printf ( "exact_phrase=%d\n", j ); res = sphinx_build_excerpts ( client, ndocs, docs, index, words, &opts ); if ( !res ) { g_failed += ( res==NULL ); if ( !g_smoke ) die ( "query failed: %s", sphinx_error(client) ); } for ( i=0; i" "The institutional investment manager it. Is Filing this report and." "" "It is signed hereby represent. That it is all information." "are It or is" "" "" "cool It is cooler" "" "It is another place!" "" }; const int ndocs = sizeof(docs)/sizeof(docs[0]); const char * words = "it is"; const char * index = "test1"; sphinx_excerpt_options opts; char ** res; int i, j; sphinx_init_excerpt_options ( &opts ); opts.limit = 150; opts.limit_passages = 8; opts.around = 8; opts.html_strip_mode = "strip"; opts.passage_boundary = "zone"; opts.emit_zones = SPH_TRUE; for ( j=0; j<2; j++ ) { if ( j==1 ) { opts.passage_boundary = "sentence"; opts.emit_zones = SPH_FALSE; } printf ( "passage_boundary=%s\n", opts.passage_boundary ); res = sphinx_build_excerpts ( client, ndocs, docs, index, words, &opts ); if ( !res ) die ( "query failed: %s", sphinx_error(client) ); for ( i=0; i % s <-\n\n", name ); } int main ( int argc, char ** argv ) { int i, port = 0; sphinx_client * client; sphinx_uint64_t override_docid = 2; unsigned int override_value = 2000; for ( i=1; i