IDZEBRA  2.2.7
rankstatic.c
Go to the documentation of this file.
1 /* This file is part of the Zebra server.
2  Copyright (C) Index Data
3 
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
7 version.
8 
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
12 for more details.
13 
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
17 
18 */
19 
20 #if HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23 #include <stdio.h>
24 #include <assert.h>
25 #include <limits.h>
26 #ifdef WIN32
27 #include <io.h>
28 #endif
29 #if HAVE_UNISTD_H
30 #include <unistd.h>
31 #endif
32 
33 #include "index.h"
34 #include "rank.h"
35 
36 static int log_level = 0;
37 static int log_initialized = 0;
38 
39 struct rank_set_info {
40  int no_rank_entries;
41 };
42 
43 /*
44  * create: Creates/Initialises this rank handler. This routine is
45  * called exactly once. The routine returns the class_handle.
46  */
47 static void *create (ZebraHandle zh)
48 {
49  if (!log_initialized)
50  {
51  log_level = yaz_log_module_level("rankstatic");
52  log_initialized = 1;
53  }
54  yaz_log(log_level, "rank-static create");
55  return 0;
56 }
57 
58 /*
59  * destroy: Destroys this rank handler. This routine is called
60  * when the handler is no longer needed - i.e. when the server
61  * dies. The class_handle was previously returned by create.
62  */
63 static void destroy (struct zebra_register *reg, void *class_handle)
64 {
65  yaz_log(log_level, "rank-static destroy");
66 }
67 
68 
74 static void *begin (struct zebra_register *reg,
75  void *class_handle, RSET rset, NMEM nmem,
76  TERMID *terms, int numterms)
77 {
78  struct rank_set_info *si =
79  (struct rank_set_info *) nmem_malloc (nmem, sizeof(*si));
80  int i;
81 
82  yaz_log(log_level, "rank-static begin");
83  /* count how many terms are ranked (2=102 or similar) */
84  si->no_rank_entries = 0;
85  for (i = 0; i < numterms; i++)
86  {
87  struct ord_list *ol = terms[i]->ol;
88 
89  yaz_log(log_level, "i=%d flags=%s '%s'", i,
90  terms[i]->flags, terms[i]->name );
91 
92  for (; ol; ol = ol->next)
93  {
94  const char *index_type = 0;
95  const char *db = 0;
96  const char *string_index = 0;
97  int set = -1;
98  int use = -1;
99 
101  ol->ord, &index_type, &db, &string_index);
102 
103  if (string_index)
104  yaz_log(log_level, " ord=%d index_type=%s db=%s str-index=%s",
105  ol->ord, index_type, db, string_index);
106  else
107  yaz_log(log_level, " ord=%d index_type=%s db=%s set=%d use=%d",
108  ol->ord, index_type, db, set, use);
109  }
110  if (!strncmp (terms[i]->flags, "rank,", 5))
111  (si->no_rank_entries)++;
112  }
113  return si;
114 }
115 
116 /*
117  * end: Terminates ranking process. Called after a result set
118  * has been ranked.
119  */
120 static void end (struct zebra_register *reg, void *set_handle)
121 {
122  yaz_log(log_level, "rank-static end");
123 }
124 
125 
131 static void add (void *set_handle, int seqno, TERMID term)
132 {
133 }
134 
135 /*
136  * calc: Called for each document in a result. This handler should
137  * produce a score based on previous call(s) to the add handler. The
138  * score should be between 0 and 1000. If score cannot be obtained
139  * -1 should be returned.
140  */
141 static int calc (void *set_handle, zint sysno, zint staticrank,
142  int *stop_flag)
143 {
144  struct rank_set_info *si = (struct rank_set_info *) set_handle;
145 
146  if (!si->no_rank_entries)
147  return -1; /* ranking not enabled for any terms */
148 
149  /* if we set *stop_flag = 1, we stop processing (of result set list) */
150  /* staticrank = 0 is highest, MAXINT lowest */
151  if (staticrank >= INT_MAX)
152  return 0;
153  /* but score is reverse (logical) */
154  return INT_MAX - CAST_ZINT_TO_INT(staticrank);
155 }
156 
157 /*
158  * Pseudo-meta code with sequence of calls as they occur in a
159  * server. Handlers are prefixed by --:
160  *
161  * server init
162  * -- create
163  * foreach search
164  * rank result set
165  * -- begin
166  * foreach record
167  * foreach word
168  * -- add
169  * -- calc
170  * -- end
171  * -- destroy
172  * server close
173  */
174 
175 static struct rank_control rank_control = {
176  "rank-static",
177  create,
178  destroy,
179  begin,
180  end,
181  calc,
182  add,
183 };
184 
186 /*
187  * Local variables:
188  * c-basic-offset: 4
189  * c-file-style: "Stroustrup"
190  * indent-tabs-mode: nil
191  * End:
192  * vim: shiftwidth=4 tabstop=8 expandtab
193  */
194 
static void * begin(struct zebra_register *reg, void *class_handle, RSET rset, NMEM nmem, TERMID *terms, int numterms)
Definition: rankstatic.c:74
static struct rank_control rank_control
Definition: rankstatic.c:175
static void end(struct zebra_register *reg, void *set_handle)
Definition: rankstatic.c:120
struct rank_control * rank_static_class
Definition: rankstatic.c:185
static void * create(ZebraHandle zh)
Definition: rankstatic.c:47
static int log_initialized
Definition: rankstatic.c:37
static void destroy(struct zebra_register *reg, void *class_handle)
Definition: rankstatic.c:63
static int log_level
Definition: rankstatic.c:36
static int calc(void *set_handle, zint sysno, zint staticrank, int *stop_flag)
Definition: rankstatic.c:141
static void add(void *set_handle, int seqno, TERMID term)
Definition: rankstatic.c:131
Definition: rset.h:35
int ord
Definition: rset.h:36
struct ord_list * next
Definition: rset.h:37
int no_rank_entries
Definition: rank1.c:55
NMEM nmem
Definition: rank1.c:57
Definition: rset.h:50
struct ord_list * ol
Definition: rset.h:64
Definition: rset.h:151
ZebraExplainInfo zei
Definition: index.h:139
long zint
Zebra integer.
Definition: util.h:66
#define CAST_ZINT_TO_INT(x)
Definition: util.h:96
int zebraExplain_lookup_ord(ZebraExplainInfo zei, int ord, const char **index_type, const char **db, const char **string_index)
Definition: zinfo.c:1478