--- trunk/pgest.c 2005/05/20 18:45:01 4 +++ trunk/pgest.c 2005/05/20 19:44:09 5 @@ -34,6 +34,8 @@ #define GET_STR(textp) DatumGetCString(DirectFunctionCall1(textout, PointerGetDatum(textp))) #define GET_TEXT(cstrp) DatumGetTextP(DirectFunctionCall1(textin, CStringGetDatum(cstrp))) +/* prototype */ +char *attr2text(ESTDOC *doc, char *attr); ESTDB *db; ESTCOND *cond; @@ -55,6 +57,7 @@ AttInMetadata *attinmeta; char *index_path; char *query; + char *attr; /* stuff done only on the first call of the function */ if (SRF_IS_FIRSTCALL()) { @@ -64,8 +67,9 @@ //index_path = _textout(PG_GETARG_TEXT_P(0)); index_path = _textout(PG_GETARG_TEXT_P(0)); query = _textout(PG_GETARG_TEXT_P(1)); - limit = PG_GETARG_INT32(2); - offset = PG_GETARG_INT32(3); + attr = _textout(PG_GETARG_TEXT_P(2)); + limit = PG_GETARG_INT32(3); + offset = PG_GETARG_INT32(4); /* create a function context for cross-call persistence */ funcctx = SRF_FIRSTCALL_INIT(); @@ -81,7 +85,7 @@ SRF_RETURN_DONE(funcctx); } - elog(INFO, "pgest: query=%s limit %d offset %d", query, limit, offset); + elog(INFO, "pgest: query[%s] attr[%s] limit %d offset %d", query, attr, limit, offset); /* create a search condition object */ if (!(cond = est_cond_new())) { @@ -92,17 +96,27 @@ /* set the search phrase to the search condition object */ est_cond_set_phrase(cond, query); + /* minimum valid attribute length is 10: @a STREQ a */ + if (attr != NULL && strlen(attr) >= 10) { + elog(INFO,"est_cond_add_attr(%s)", attr); + est_cond_add_attr(cond, attr); + } + /* get the result of search */ est_result = est_db_search(db, cond, &resnum, NULL); /* total number of tuples to be returned */ - funcctx->max_calls = resnum; + if (limit && limit < resnum) { + funcctx->max_calls = limit - offset; + } else { + funcctx->max_calls = resnum - offset; + } /* check if results exists */ if ( 0 == funcctx->max_calls ) elog(INFO, "pgest: no results for: %s", query ); - elog(DEBUG1, "pgest: found %d hits for %s", funcctx->max_calls, query); + elog(DEBUG1, "pgest: found %d hits for %s", resnum, query); /* Build a tuple description for a __pgest tuple */ tupdesc = RelationNameGetTupleDesc("__pgest"); @@ -167,10 +181,10 @@ // values[0] = (char *) palloc(strlen(_estval) * sizeof(char)); - values[0] = attr2text(doc,"@id"); - values[1] = attr2text(doc,"@uri"); - values[2] = attr2text(doc,"@title"); - values[3] = attr2text(doc,"@type"); + values[0] = (char *) attr2text(doc,"@id"); + values[1] = (char *) attr2text(doc,"@uri"); + values[2] = (char *) attr2text(doc,"@title"); + values[3] = (char *) attr2text(doc,"@type"); /* destloy the document object */ elog(DEBUG2, "est_doc_delete");