6390ebb5eeb4e8cb8a29556fe6bc513a12d5c4dc
[yaz-moved-to-github.git] / src / rpn2solr.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2011 Index Data
3  * See the file LICENSE for details.
4  */
5 /**
6  * \file
7  * \brief Implements RPN to SOLR conversion
8  */
9 #if HAVE_CONFIG_H
10 #include <config.h>
11 #endif
12
13 #include <assert.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <yaz/rpn2solr.h>
17 #include <yaz/xmalloc.h>
18 #include <yaz/diagbib1.h>
19 #include <yaz/z-core.h>
20 #include <yaz/wrbuf.h>
21
22 static void wrbuf_vputs(const char *buf, void *client_data)
23 {
24     wrbuf_write((WRBUF) client_data, buf, strlen(buf));
25 }
26
27 static const char *lookup_index_from_string_attr(Z_AttributeList *attributes)
28 {
29     int j;
30     int server_choice = 1;
31     for (j = 0; j < attributes->num_attributes; j++)
32     {
33         Z_AttributeElement *ae = attributes->attributes[j];
34         if (*ae->attributeType == 1) /* use attribute */
35         {
36             if (ae->which == Z_AttributeValue_complex)
37             {
38                 Z_ComplexAttribute *ca = ae->value.complex;
39                 int i;
40                 for (i = 0; i < ca->num_list; i++)
41                 {
42                     Z_StringOrNumeric *son = ca->list[i];
43                     if (son->which == Z_StringOrNumeric_string)
44                         return son->u.string;
45                 }
46             }
47             server_choice = 0; /* not serverChoice because we have use attr */
48         }
49     }
50     if (server_choice)
51         return "solr.serverChoice";
52     return 0;
53 }
54
55 static const char *lookup_relation_index_from_attr(Z_AttributeList *attributes)
56 {
57     int j;
58     for (j = 0; j < attributes->num_attributes; j++)
59     {
60         Z_AttributeElement *ae = attributes->attributes[j];
61         if (*ae->attributeType == 2) /* relation attribute */
62         {
63             if (ae->which == Z_AttributeValue_numeric)
64             {
65                 /* Only support for numeric relation */
66                 Odr_int *relation = ae->value.numeric;
67                 /* map this numeric to representation in SOLR */
68                 switch (*relation)
69                 {
70                     /* Unsure on whether this is the relation attribute constants? */
71                 case Z_ProximityOperator_Prox_lessThan: 
72                     return 0;
73                 case Z_ProximityOperator_Prox_lessThanOrEqual: 
74                     return 0;
75                 case Z_ProximityOperator_Prox_equal: 
76                     return ":";
77                 case Z_ProximityOperator_Prox_greaterThanOrEqual: 
78                     return 0;
79                 case Z_ProximityOperator_Prox_greaterThan: 
80                     return 0;
81                 case Z_ProximityOperator_Prox_notEqual: 
82                     return 0;
83                 case 100: 
84                     /* phonetic is not implemented*/
85                     return 0; 
86                 case 101: 
87                     /* stem is not not implemented */
88                     return 0; 
89                 case 102: 
90                     /* relevance is supported in SOLR, but not implemented yet */
91                     return 0; 
92                 default:
93                     /* Invalid relation */
94                     return 0;
95                 }
96             }
97             else {
98                 /*  Can we have a complex relation value?
99                     Should we implement something?
100                 */
101             }
102         }
103     }
104     return ":";
105 }
106
107 static int rpn2solr_attr(solr_transform_t ct,
108                         Z_AttributeList *attributes, WRBUF w)
109 {
110     const char *relation = solr_lookup_reverse(ct, "relation.", attributes);
111     const char *index = solr_lookup_reverse(ct, "index.", attributes);
112     const char *structure = solr_lookup_reverse(ct, "structure.", attributes);
113
114     /* if transform (properties) do not match, we'll just use a USE string attribute (bug #2978) */
115     if (!index)
116         index = lookup_index_from_string_attr(attributes);
117
118     /* Attempt to fix bug #2978: Look for a relation attribute */
119     if (!relation) 
120         relation = lookup_relation_index_from_attr(attributes);
121
122     if (!index)
123     {
124         solr_transform_set_error(ct,
125                                 YAZ_BIB1_UNSUPP_USE_ATTRIBUTE, 0);
126         return -1;
127     }
128     /* for serverChoice we omit index+relation+structure */
129     if (strcmp(index, "solr.serverChoice"))
130     {
131         wrbuf_puts(w, index);
132         if (relation)
133         {
134             if (!strcmp(relation, "exact"))
135                 /* TODO Verify if a exact  SOLR exists */
136                 relation = ":";
137             else if (!strcmp(relation, "eq"))
138                 relation = ":";
139             else if (!strcmp(relation, "le")) {
140                 /* TODO Not support as such, but could perhaps be transformed into a range
141                 relation = ":[ * to ";
142                 close_range = "]"
143                 */
144             }
145             else if (!strcmp(relation, "ge")) {
146                 /* TODO Not support as such, but could perhaps be transformed into a range
147                 relation = "[";
148                 relation = ":[ * to ";
149                 close_range = "]"
150                 */
151             }
152             /* Missing mapping of not equal, phonetic, stem and relevance */
153             wrbuf_puts(w, relation);
154         }
155         else
156             wrbuf_puts(w, ":");
157
158         if (structure)
159         {
160             if (strcmp(structure, "*"))
161             {
162                 wrbuf_puts(w, "/");
163                 wrbuf_puts(w, structure);
164                 wrbuf_puts(w, " ");
165             }
166         }
167     }
168     return 0;
169 }
170
171 /* Bug 2878: Currently only support left and right truncation. Specific check for this */
172 static int checkForTruncation(int flag, Z_AttributeList *attributes)
173 {
174     int j;
175     for (j = 0; j < attributes->num_attributes; j++)
176     {
177         Z_AttributeElement *ae = attributes->attributes[j];
178         if (*ae->attributeType == 5) /* truncation attribute */
179         {
180             if (ae->which == Z_AttributeValue_numeric)
181             {
182                 Odr_int truncation = *(ae->value.numeric);
183                 /* This logic only works for Left, right and both. eg. 1,2,3 */
184                 if (truncation <= 3)
185                     return ((int) truncation & flag);
186             }
187             /* Complex: Shouldn't happen */
188         }
189     }
190     /* No truncation or unsupported */
191     return 0;
192 };
193
194 static int checkForLeftTruncation(Z_AttributeList *attributes) {
195         return checkForTruncation(1, attributes);
196 }
197
198 static int checkForRightTruncation(Z_AttributeList *attributes) {
199         return checkForTruncation(2, attributes);
200 };
201
202 static int rpn2solr_simple(solr_transform_t ct,
203                           void (*pr)(const char *buf, void *client_data),
204                           void *client_data,
205                           Z_Operand *q, WRBUF w)
206 {
207     int ret = 0;
208     if (q->which != Z_Operand_APT)
209     {
210         ret = -1;
211         solr_transform_set_error(ct, YAZ_BIB1_RESULT_SET_UNSUPP_AS_A_SEARCH_TERM, 0);
212     }
213     else
214     {
215         Z_AttributesPlusTerm *apt = q->u.attributesPlusTerm;
216         Z_Term *term = apt->term;
217         const char *sterm = 0;
218         size_t lterm = 0;
219
220         wrbuf_rewind(w);
221         ret = rpn2solr_attr(ct, apt->attributes, w);
222
223         switch(term->which)
224         {
225         case Z_Term_general:
226             lterm = term->u.general->len;
227             sterm = (const char *) term->u.general->buf;
228             break;
229         case Z_Term_numeric:
230             wrbuf_printf(w, ODR_INT_PRINTF, *term->u.numeric);
231             break;
232         case Z_Term_characterString:
233             sterm = term->u.characterString;
234             lterm = strlen(sterm);
235             break;
236         default:
237             ret = -1;
238             solr_transform_set_error(ct, YAZ_BIB1_TERM_TYPE_UNSUPP, 0);
239         }
240
241         if (term)
242         {
243             size_t i;
244             int must_quote = 0;
245             for (i = 0 ; i < lterm; i++)
246                 if (sterm[i] == ' ')
247                     must_quote = 1;
248             if (must_quote)
249                 wrbuf_puts(w, "\"");
250             /* Bug 2878: Check and add Truncation */
251                         if (checkForLeftTruncation(apt->attributes))
252                 wrbuf_puts(w, "*");
253             wrbuf_write(w, sterm, lterm);
254             /* Bug 2878: Check and add Truncation */
255                         if (checkForRightTruncation(apt->attributes))
256                 wrbuf_puts(w, "*");
257             if (must_quote)
258                 wrbuf_puts(w, "\"");
259         }
260         if (ret == 0)
261             pr(wrbuf_cstr(w), client_data);
262     }
263     return ret;
264 }
265
266
267 static int rpn2solr_structure(solr_transform_t ct,
268                              void (*pr)(const char *buf, void *client_data),
269                              void *client_data,
270                              Z_RPNStructure *q, int nested,
271                              WRBUF w)
272 {
273     if (q->which == Z_RPNStructure_simple)
274         return rpn2solr_simple(ct, pr, client_data, q->u.simple, w);
275     else
276     {
277         Z_Operator *op = q->u.complex->roperator;
278         int r;
279
280         if (nested)
281             pr("(", client_data);
282
283         r = rpn2solr_structure(ct, pr, client_data, q->u.complex->s1, 1, w);
284         if (r)
285             return r;
286         switch(op->which)
287         {
288         case  Z_Operator_and:
289             pr(" AND ", client_data);
290             break;
291         case  Z_Operator_or:
292             pr(" OR ", client_data);
293             break;
294         case  Z_Operator_and_not:
295             pr(" AND NOT ", client_data);
296             break;
297         case  Z_Operator_prox:
298             solr_transform_set_error(ct, YAZ_BIB1_UNSUPP_SEARCH, 0);
299             return -1;
300         }
301         r = rpn2solr_structure(ct, pr, client_data, q->u.complex->s2, 1, w);
302         if (nested)
303             pr(")", client_data);
304         return r;
305     }
306 }
307
308 int solr_transform_rpn2solr_stream(solr_transform_t ct,
309                                  void (*pr)(const char *buf, void *client_data),
310                                  void *client_data,
311                                  Z_RPNQuery *q)
312 {
313     int r;
314     WRBUF w = wrbuf_alloc();
315     solr_transform_set_error(ct, 0, 0);
316     r = rpn2solr_structure(ct, pr, client_data, q->RPNStructure, 0, w);
317     wrbuf_destroy(w);
318     return r;
319 }
320
321
322 int solr_transform_rpn2solr_wrbuf(solr_transform_t ct,
323                                 WRBUF w,
324                                 Z_RPNQuery *q)
325 {
326     return solr_transform_rpn2solr_stream(ct, wrbuf_vputs, w, q);
327 }
328
329 /*
330  * Local variables:
331  * c-basic-offset: 4
332  * c-file-style: "Stroustrup"
333  * indent-tabs-mode: nil
334  * End:
335  * vim: shiftwidth=4 tabstop=8 expandtab
336  */
337