5 // 27 February 2007 -- tds
8 #define GALAGOD_DEFAULT_QUERY_RESULTS (20)
9 #define GALAGOD_MAXIMUM_QUERY_RESULTS (1<<20)
15 static void print_xml_results( const std::vector
<indri::api::ScoredExtentResult
>& results
,
17 FCGX_Request
& request
)
19 FCGX_FPrintF( request
.out
, "[\n" );
21 for( int i
=0; i
<results
.size(); i
++ ) {
22 std::string document
= retrieval
.getDocument(results
[i
].document
);
23 bool more
= (results
.size() - i
> 1);
25 FCGX_FPrintF( request
.out
, "\t{ \"score\" : %f, \"document\" : %s }%s\n", results
[i
].score
,
30 FCGX_FPrintF( request
.out
, "]\n" );
37 static void print_json_results( const std::vector
<indri::api::ScoredExtentResult
>& results
,
39 FCGX_Request
& request
)
41 FCGX_FPrintF( request
.out
, "<results>\n" );
43 for( int i
=0; i
<results
.size(); i
++ ) {
44 std::string document
= retrieval
.getDocument(results
[i
].document
);
46 FCGX_FPrintF( request
.out
, "\t<result>\n" );
47 FCGX_FPrintF( request
.out
, "\t\t<score>%f</score>\n", results
[i
].score
);
48 FCGX_FPrintF( request
.out
, "\t\t<document>%s</document>\n", document
.c_str() );
49 FCGX_FPrintF( request
.out
, "\t</result>\n" );
52 FCGX_FPrintF( request
.out
, "</results>\n" );
59 static std::string
unescape_string( const std::string
& input
) {
60 std::stringstream output
;
62 for( int i
=0; i
<input
.size(); i
++ ) {
63 if( input
[i
] == '+' ) {
65 } else if( input
[i
] == '%' ) {
66 output
<< unescape_hex( input
, i
+1 );
80 static std::map
<std::string
, std::string
> parse_query_string( const std::string
& input
) {
82 std::map
<std::string
, std::string
> result
;
85 std::string::size_type equals
= input
.find( '=', location
);
86 std::string::size_type ampersand
= input
.find( '&', equals
);
88 if( equals
== std::string::npos
)
91 std::string key
= input
.substr( location
, equals
-location
-1 );
94 if( ampersand
!= std::string::npos
) {
95 value
= input
.substr( equals
+1 );
97 value
= input
.substr( equals
+1, ampersand
-equals
-1 );
100 value
= unescape_string( value
);
103 if( ampersand
== std::string::npos
)
114 static void fcgi_daemon( Retrieval
& retrieval
, indri::api::Parameters
& parameters
) {
115 FCGX_Request request
;
116 FCGX_InitRequest(&request
, 0, 0);
119 int rc
= FCGX_Accept_r( &request
);
124 const char* requestString
= FCGX_GetParam( "QUERY_STRING", request
.envp
);
126 // parse the query string
127 std::map
<std::string
, std::string
> parsed
= parse_query_string( requestString
);
129 // get the count parameter
130 int count
= GALAGOD_DEFAULT_QUERY_RESULTS
;
132 if( parsed
.find( "count" ) != parsed
.end() ) {
133 count
= string_to_i64( parsed
["count"] );
134 count
= std::min( count
, GALAGOD_MAXIMUM_QUERY_RESULTS
;
138 std::string queryText
;
140 if( parsed
.find( "query" ) != parsed
.end() ) {
141 queryText
= parsed
["query"];
145 std::vector
<indri::api::ScoredExtentResult
> results
;
146 results
= retrieval
.runQuery( queryText
, requested
);
149 if( parsed
.find( "json" ) != parsed
.end() ) {
150 print_json_results( results
, retrieval
, request
);
152 print_xml_results( results
, retrieval
, request
);
154 FCGX_Finish_r(&request
);
162 int main( int argc
, char** argv
) {
164 // read in parameters
165 indri::api::Parameters::instance().loadCommandLine( argc
, argv
);
166 indri::api::Parameters
& parameters
= indri::api::Parameters::instance();
170 std::string indexPath
= parameters
["index"];
171 retrieval
.openRead( indexPath
);
173 fcgi_daemon( retrieval
, parameters
);
174 } catch( lemur::api::Exception
& e
) {