1 /* This file is part of the Zebra server.
2 Copyright (C) 1994-2009 Index Data
4 Zebra is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
9 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
33 static int log_level = 0;
34 static int log_initialized = 0;
36 struct rank_set_info {
41 * create: Creates/Initialises this rank handler. This routine is
42 * called exactly once. The routine returns the class_handle.
44 static void *create (ZebraHandle zh)
48 log_level = yaz_log_module_level("rankstatic");
51 yaz_log(log_level, "rank-static create");
56 * destroy: Destroys this rank handler. This routine is called
57 * when the handler is no longer needed - i.e. when the server
58 * dies. The class_handle was previously returned by create.
60 static void destroy (struct zebra_register *reg, void *class_handle)
62 yaz_log(log_level, "rank-static destroy");
67 * begin: Prepares beginning of "real" ranking. Called once for
68 * each result set. The returned handle is a "set handle" and
69 * will be used in each of the handlers below.
71 static void *begin (struct zebra_register *reg,
72 void *class_handle, RSET rset, NMEM nmem,
73 TERMID *terms, int numterms)
75 struct rank_set_info *si =
76 (struct rank_set_info *) nmem_malloc (nmem, sizeof(*si));
79 yaz_log(log_level, "rank-static begin");
80 /* count how many terms are ranked (2=102 or similar) */
81 si->no_rank_entries = 0;
82 for (i = 0; i < numterms; i++)
84 struct ord_list *ol = terms[i]->ol;
86 yaz_log(log_level, "i=%d flags=%s '%s'", i,
87 terms[i]->flags, terms[i]->name );
89 for (; ol; ol = ol->next)
91 const char *index_type = 0;
93 const char *string_index = 0;
97 zebraExplain_lookup_ord(reg->zei,
98 ol->ord, &index_type, &db, &string_index);
101 yaz_log(log_level, " ord=%d index_type=%s db=%s str-index=%s",
102 ol->ord, index_type, db, string_index);
104 yaz_log(log_level, " ord=%d index_type=%s db=%s set=%d use=%d",
105 ol->ord, index_type, db, set, use);
107 if (!strncmp (terms[i]->flags, "rank,", 5))
108 (si->no_rank_entries)++;
114 * end: Terminates ranking process. Called after a result set
117 static void end (struct zebra_register *reg, void *set_handle)
119 yaz_log(log_level, "rank-static end");
124 * add: Called for each word occurence in a result set. This routine
125 * should be as fast as possible. This routine should "incrementally"
128 static void add (void *set_handle, int seqno, TERMID term)
133 * calc: Called for each document in a result. This handler should
134 * produce a score based on previous call(s) to the add handler. The
135 * score should be between 0 and 1000. If score cannot be obtained
136 * -1 should be returned.
138 static int calc (void *set_handle, zint sysno, zint staticrank,
141 struct rank_set_info *si = (struct rank_set_info *) set_handle;
143 if (!si->no_rank_entries)
144 return -1; /* ranking not enabled for any terms */
146 /* if we set *stop_flag = 1, we stop processing (of result set list) */
147 /* staticrank = 0 is highest, MAXINT lowest */
148 if (staticrank >= INT_MAX)
150 /* but score is reverse (logical) */
151 return INT_MAX - CAST_ZINT_TO_INT(staticrank);
155 * Pseudo-meta code with sequence of calls as they occur in a
156 * server. Handlers are prefixed by --:
172 static struct rank_control rank_control = {
182 struct rank_control *rank_static_class = &rank_control;
186 * c-file-style: "Stroustrup"
187 * indent-tabs-mode: nil
189 * vim: shiftwidth=4 tabstop=8 expandtab