[Namazu-cvs 758] r246 - Search-Namazu/trunk/Search-Namazu

cvsd @ vaj.namazu.org cvsd @ vaj.namazu.org
2006年 3月 29日 (水) 09:38:41 JST


Author: knok
Date: 2006-03-29 09:38:41 +0900 (Wed, 29 Mar 2006)
New Revision: 246

Modified:
   Search-Namazu/trunk/Search-Namazu/ChangeLog
   Search-Namazu/trunk/Search-Namazu/Namazu.pm
   Search-Namazu/trunk/Search-Namazu/Namazu.xs
Log:
Added "fields" parameter.


Modified: Search-Namazu/trunk/Search-Namazu/ChangeLog
===================================================================
--- Search-Namazu/trunk/Search-Namazu/ChangeLog	2006-03-28 08:55:36 UTC (rev 245)
+++ Search-Namazu/trunk/Search-Namazu/ChangeLog	2006-03-29 00:38:41 UTC (rev 246)
@@ -1,3 +1,7 @@
+2006-03-29  NOKUBI Takatsugu  <knok @ daionet.gr.jp>
+
+	* Namazu.{xs,pm}: Added "fields" parameter.
+
 2006-03-28  NOKUBI Takatsugu  <knok @ daionet.gr.jp>
 
 	* Namazu.{xs,pm}: Added maxget parameter.

Modified: Search-Namazu/trunk/Search-Namazu/Namazu.pm
===================================================================
--- Search-Namazu/trunk/Search-Namazu/Namazu.pm	2006-03-28 08:55:36 UTC (rev 245)
+++ Search-Namazu/trunk/Search-Namazu/Namazu.pm	2006-03-29 00:38:41 UTC (rev 246)
@@ -39,6 +39,14 @@
       print ($hlist->score, $hlist->uri, $hlist->date, $hlist->rank);
   }
 
+  $x = Search::Namazu::Search(index => '/usr/local/namazu/index',
+				query => 'foo',
+				fields => ["uri", "score", "from"]);
+
+  foreach my $rxs (@$x) {
+      print ($rxs->get("uri"), $rxs->get("score"), $rxs->get("from"));
+  }
+
 =head1 DESCRIPTION
 
 This module is an interface for Namazu library. Namazu is an implement
@@ -140,6 +148,12 @@
 Specify return method, if the parameter is set as 'reference', it
 returns a reference of array as a result.
 
+=head3 fields
+
+Specify you want to get fields as a refrence of array. In the case,
+the result is returned as a reference of array, contains
+Search::Namazu::ResultXS objects.
+
 =head2 Search::Namazu::Result
 
 Search::Namazu::Result object is for keeping result information.
@@ -177,6 +191,15 @@
 
 It returns size.
 
+=head2 Search::Namazu::ResultXS
+
+Search::Namazu::ResultXS object is also for keeping result information.
+It has the following methods:
+
+=head3 get
+
+It returns specified value of field.
+
 =head1 COPYRIGHT
 
 Copyright 1999,2000,2001,2002 NOKUBI Takatsugu All rights reserved.
@@ -241,6 +264,7 @@
     my $maxhit = $args{'maxhit'} || 10000;
     my $returnas = $args{'returnas'};
     my $maxget = $args{'maxget'} || $maxhit;
+    my $fields = $args{'fields'};
 
 # initialize
 
@@ -298,7 +322,15 @@
 
 # create Search::Namazu::Result object
 
-    if ($returnas eq 'reference') {
+    if (ref($fields) eq "ARRAY") {
+	my $hlistref = call_search_main_fields($query, $maxget, $fields);
+	my $status = nmz_getstatus();
+	if ($status != NMZ_SUCCESS) {
+	    return $status;
+	}
+	# return objects
+	return $hlistref;
+    } elsif ($returnas eq 'reference') {
 	my $hlistref = call_search_main_ref($query, $maxget);
 	my $status = nmz_getstatus();
 	if ($status != NMZ_SUCCESS) {

Modified: Search-Namazu/trunk/Search-Namazu/Namazu.xs
===================================================================
--- Search-Namazu/trunk/Search-Namazu/Namazu.xs	2006-03-28 08:55:36 UTC (rev 245)
+++ Search-Namazu/trunk/Search-Namazu/Namazu.xs	2006-03-29 00:38:41 UTC (rev 246)
@@ -66,8 +66,8 @@
 	int i;
 	status = 0;
 	retar = newAV();
+	hlist = nmz_search(query);
 	av_extend(retar, hlist.num - 1);
-	hlist = nmz_search(query);
 	status = hlist.stat;
 	for (i = 0; i < hlist.num; i ++) {
 	    if (i < maxget) {
@@ -104,6 +104,62 @@
 	return retar;
 }
 
+AV * call_search_main_fields_c(char *query, int maxget, AV *fields)
+{
+	AV *retar;
+	HV *stash;
+	char **fstr;
+	int *fsize;
+	NmzResult hlist;
+	int i;
+	int flen;
+
+	retar = newAV();
+	flen = av_len(fields);
+	if (flen < 0 || maxget <= 0 || query == NULL)
+		return retar;
+	status = 0;
+	fstr = (char **) malloc(sizeof(char *) * (flen + 1));
+	fsize = (int *) malloc(sizeof(int) * (flen + 1));
+	for (i = 0; i <= flen; i ++) {
+		SV **x;
+		x = av_fetch(fields, i, 0);
+		fstr[i] = SvPV_nolen(*x);
+		fsize[i] = SvCUR(*x);
+	}
+	hlist = nmz_search(query);
+	av_extend(retar, hlist.num - 1);
+	status = hlist.stat;
+	stash = gv_stashpv("Search::Namazu::ResultXS", 0);
+	for (i = 0; i < hlist.num; i ++) {
+		if (i < maxget) {
+			HV *hash;
+			SV *ref;
+			int j;
+			char fcont[BUFSIZE];
+
+			hash = newHV();
+			for (j = 0; j <= flen; j ++) {
+				nmz_get_field_data(hlist.data[i].idxid,
+					hlist.data[i].docid,
+					fstr[j], fcont);
+				hv_store(hash, fstr[j], fsize[j], 
+					newSVpv(fcont, strlen(fcont)), 0);
+			}
+
+			ref = newRV_inc((SV*) hash);
+			sv_bless(ref, stash);
+			av_store(retar, i, ref);
+		} else {
+			av_store(retar, i, &PL_sv_undef);
+		}
+	}
+	nmz_free_hlist(hlist);
+	free(fstr);
+	free(fsize);
+	return retar;
+}
+
 MODULE = Search::Namazu		PACKAGE = Search::Namazu
 
 PROTOTYPES: DISABLE
@@ -154,6 +210,29 @@
 	OUTPUT:
 		RETVAL
 
+SV*
+call_search_main_fields(query, maxget, fieldref)
+	SV *query
+	int maxget
+	SV *fieldref
+
+	CODE:
+		char *qstr;
+		char cqstr[BUFSIZE * 2];
+		AV *retar;
+		AV *fields;
+		int i;
+
+		fields = (AV *) SvRV(fieldref);
+		qstr = SvPV(query, PL_na);
+		nmz_codeconv_query(qstr);
+		strcpy(cqstr, qstr);
+		retar = call_search_main_fields_c(cqstr, maxget, fields);
+		nmz_free_internal();
+		RETVAL = newRV_inc((SV*) retar);
+	OUTPUT:
+		RETVAL
+
 int
 nmz_addindex(index)
 	SV *index




Namazu-cvs メーリングリストの案内