1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2010 Index Data
3 * See the file LICENSE for details.
8 * \brief Implements RPN to SOLR conversion
15 #include <yaz/rpn2solr.h>
16 #include <yaz/xmalloc.h>
17 #include <yaz/diagbib1.h>
18 #include <yaz/z-core.h>
19 #include <yaz/wrbuf.h>
21 static void wrbuf_vputs(const char *buf, void *client_data)
23 wrbuf_write((WRBUF) client_data, buf, strlen(buf));
26 static const char *lookup_index_from_string_attr(Z_AttributeList *attributes)
29 int server_choice = 1;
30 for (j = 0; j < attributes->num_attributes; j++)
32 Z_AttributeElement *ae = attributes->attributes[j];
33 if (*ae->attributeType == 1) /* use attribute */
35 if (ae->which == Z_AttributeValue_complex)
37 Z_ComplexAttribute *ca = ae->value.complex;
39 for (i = 0; i < ca->num_list; i++)
41 Z_StringOrNumeric *son = ca->list[i];
42 if (son->which == Z_StringOrNumeric_string)
46 server_choice = 0; /* not serverChoice because we have use attr */
50 return "solr.serverChoice";
54 static const char *lookup_relation_index_from_attr(Z_AttributeList *attributes)
57 for (j = 0; j < attributes->num_attributes; j++)
59 Z_AttributeElement *ae = attributes->attributes[j];
60 if (*ae->attributeType == 2) /* relation attribute */
62 if (ae->which == Z_AttributeValue_numeric)
64 /* Only support for numeric relation */
65 Odr_int *relation = ae->value.numeric;
66 /* map this numeric to representation in SOLR */
69 /* Unsure on whether this is the relation attribute constants? */
70 case Z_ProximityOperator_Prox_lessThan:
72 case Z_ProximityOperator_Prox_lessThanOrEqual:
74 case Z_ProximityOperator_Prox_equal:
76 case Z_ProximityOperator_Prox_greaterThanOrEqual:
78 case Z_ProximityOperator_Prox_greaterThan:
80 case Z_ProximityOperator_Prox_notEqual:
83 /* phonetic is not implemented*/
86 /* stem is not not implemented */
89 /* relevance is supported in SOLR, but not implemented yet */
92 /* Invalid relation */
97 /* Can we have a complex relation value?
98 Should we implement something?
106 static int rpn2solr_attr(solr_transform_t ct,
107 Z_AttributeList *attributes, WRBUF w)
109 const char *relation = solr_lookup_reverse(ct, "relation.", attributes);
110 const char *index = solr_lookup_reverse(ct, "index.", attributes);
111 const char *structure = solr_lookup_reverse(ct, "structure.", attributes);
113 /* if transform (properties) do not match, we'll just use a USE string attribute (bug #2978) */
115 index = lookup_index_from_string_attr(attributes);
117 /* Attempt to fix bug #2978: Look for a relation attribute */
119 relation = lookup_relation_index_from_attr(attributes);
123 solr_transform_set_error(ct,
124 YAZ_BIB1_UNSUPP_USE_ATTRIBUTE, 0);
127 /* for serverChoice we omit index+relation+structure */
128 if (strcmp(index, "solr.serverChoice"))
130 wrbuf_puts(w, index);
133 if (!strcmp(relation, "exact"))
134 /* TODO Verify if a exact SOLR exists */
136 else if (!strcmp(relation, "eq"))
138 else if (!strcmp(relation, "le")) {
139 /* TODO Not support as such, but could perhaps be transformed into a range
140 relation = ":[ * to ";
144 else if (!strcmp(relation, "ge")) {
145 /* TODO Not support as such, but could perhaps be transformed into a range
147 relation = ":[ * to ";
151 /* Missing mapping of not equal, phonetic, stem and relevance */
152 wrbuf_puts(w, relation);
159 if (strcmp(structure, "*"))
162 wrbuf_puts(w, structure);
170 /* Bug 2878: Currently only support left and right truncation. Specific check for this */
171 static int checkForTruncation(int flag, Z_AttributeList *attributes)
174 for (j = 0; j < attributes->num_attributes; j++)
176 Z_AttributeElement *ae = attributes->attributes[j];
177 if (*ae->attributeType == 5) /* truncation attribute */
179 if (ae->which == Z_AttributeValue_numeric)
181 Odr_int truncation = *(ae->value.numeric);
182 /* This logic only works for Left, right and both. eg. 1,2,3 */
184 return ((int) truncation & flag);
186 /* Complex: Shouldn't happen */
189 /* No truncation or unsupported */
193 static int checkForLeftTruncation(Z_AttributeList *attributes) {
194 return checkForTruncation(1, attributes);
197 static int checkForRightTruncation(Z_AttributeList *attributes) {
198 return checkForTruncation(2, attributes);
201 static int rpn2solr_simple(solr_transform_t ct,
202 void (*pr)(const char *buf, void *client_data),
204 Z_Operand *q, WRBUF w)
207 if (q->which != Z_Operand_APT)
210 solr_transform_set_error(ct, YAZ_BIB1_RESULT_SET_UNSUPP_AS_A_SEARCH_TERM, 0);
214 Z_AttributesPlusTerm *apt = q->u.attributesPlusTerm;
215 Z_Term *term = apt->term;
216 const char *sterm = 0;
220 ret = rpn2solr_attr(ct, apt->attributes, w);
225 lterm = term->u.general->len;
226 sterm = (const char *) term->u.general->buf;
229 wrbuf_printf(w, ODR_INT_PRINTF, *term->u.numeric);
231 case Z_Term_characterString:
232 sterm = term->u.characterString;
233 lterm = strlen(sterm);
237 solr_transform_set_error(ct, YAZ_BIB1_TERM_TYPE_UNSUPP, 0);
244 for (i = 0 ; i < lterm; i++)
249 /* Bug 2878: Check and add Truncation */
250 if (checkForLeftTruncation(apt->attributes))
252 wrbuf_write(w, sterm, lterm);
253 /* Bug 2878: Check and add Truncation */
254 if (checkForRightTruncation(apt->attributes))
260 pr(wrbuf_cstr(w), client_data);
266 static int rpn2solr_structure(solr_transform_t ct,
267 void (*pr)(const char *buf, void *client_data),
269 Z_RPNStructure *q, int nested,
272 if (q->which == Z_RPNStructure_simple)
273 return rpn2solr_simple(ct, pr, client_data, q->u.simple, w);
276 Z_Operator *op = q->u.complex->roperator;
280 pr("(", client_data);
282 r = rpn2solr_structure(ct, pr, client_data, q->u.complex->s1, 1, w);
288 pr(" AND ", client_data);
291 pr(" OR ", client_data);
293 case Z_Operator_and_not:
294 pr(" AND NOT ", client_data);
296 case Z_Operator_prox:
297 solr_transform_set_error(ct, YAZ_BIB1_UNSUPP_SEARCH, 0);
300 r = rpn2solr_structure(ct, pr, client_data, q->u.complex->s2, 1, w);
302 pr(")", client_data);
307 int solr_transform_rpn2solr_stream(solr_transform_t ct,
308 void (*pr)(const char *buf, void *client_data),
313 WRBUF w = wrbuf_alloc();
314 solr_transform_set_error(ct, 0, 0);
315 r = rpn2solr_structure(ct, pr, client_data, q->RPNStructure, 0, w);
321 int solr_transform_rpn2solr_wrbuf(solr_transform_t ct,
325 return solr_transform_rpn2solr_stream(ct, wrbuf_vputs, w, q);
331 * c-file-style: "Stroustrup"
332 * indent-tabs-mode: nil
334 * vim: shiftwidth=4 tabstop=8 expandtab