1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2013 Index Data
3 * See the file LICENSE for details.
7 * \brief Implements RPN to SOLR conversion
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>
22 static void wrbuf_vputs(const char *buf, void *client_data)
24 wrbuf_write((WRBUF) client_data, buf, strlen(buf));
27 static const char *lookup_index_from_string_attr(Z_AttributeList *attributes)
30 int server_choice = 1;
31 for (j = 0; j < attributes->num_attributes; j++)
33 Z_AttributeElement *ae = attributes->attributes[j];
34 if (*ae->attributeType == 1) /* use attribute */
36 if (ae->which == Z_AttributeValue_complex)
38 Z_ComplexAttribute *ca = ae->value.complex;
40 for (i = 0; i < ca->num_list; i++)
42 Z_StringOrNumeric *son = ca->list[i];
43 if (son->which == Z_StringOrNumeric_string)
47 server_choice = 0; /* not serverChoice because we have use attr */
51 return "solr.serverChoice";
55 static const char *lookup_relation_index_from_attr(Z_AttributeList *attributes)
58 for (j = 0; j < attributes->num_attributes; j++)
60 Z_AttributeElement *ae = attributes->attributes[j];
61 if (*ae->attributeType == 2) /* relation attribute */
63 if (ae->which == Z_AttributeValue_numeric)
65 /* Only support for numeric relation */
66 Odr_int *relation = ae->value.numeric;
67 /* map this numeric to representation in SOLR */
70 /* Unsure on whether this is the relation attribute constants? */
71 case Z_ProximityOperator_Prox_lessThan:
73 case Z_ProximityOperator_Prox_lessThanOrEqual:
75 case Z_ProximityOperator_Prox_equal:
77 case Z_ProximityOperator_Prox_greaterThanOrEqual:
79 case Z_ProximityOperator_Prox_greaterThan:
81 case Z_ProximityOperator_Prox_notEqual:
84 /* phonetic is not implemented*/
87 /* stem is not not implemented */
90 /* relevance is supported in SOLR, but not implemented yet */
93 /* Invalid relation */
98 /* Can we have a complex relation value?
99 Should we implement something?
107 static int rpn2solr_attr(solr_transform_t ct,
108 Z_AttributeList *attributes, WRBUF w)
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);
114 /* if transform (properties) do not match, we'll just use a USE string attribute (bug #2978) */
116 index = lookup_index_from_string_attr(attributes);
118 /* Attempt to fix bug #2978: Look for a relation attribute */
120 relation = lookup_relation_index_from_attr(attributes);
124 solr_transform_set_error(ct,
125 YAZ_BIB1_UNSUPP_USE_ATTRIBUTE, 0);
128 /* for serverChoice we omit index+relation+structure */
129 if (strcmp(index, "solr.serverChoice"))
131 wrbuf_puts(w, index);
134 if (!strcmp(relation, "exact"))
135 /* TODO Verify if a exact SOLR exists */
137 else if (!strcmp(relation, "eq"))
139 else if (!strcmp(relation, "le")) {
140 /* TODO Not support as such, but could perhaps be transformed into a range
141 relation = ":[ * to ";
145 else if (!strcmp(relation, "ge")) {
146 /* TODO Not support as such, but could perhaps be transformed into a range
148 relation = ":[ * to ";
152 /* Missing mapping of not equal, phonetic, stem and relevance */
153 wrbuf_puts(w, relation);
160 if (strcmp(structure, "*"))
163 wrbuf_puts(w, structure);
171 static Odr_int get_truncation(Z_AttributesPlusTerm *apt)
174 Z_AttributeList *attributes = apt->attributes;
175 for (j = 0; j < attributes->num_attributes; j++)
177 Z_AttributeElement *ae = attributes->attributes[j];
178 if (*ae->attributeType == 5) /* truncation attribute */
180 if (ae->which == Z_AttributeValue_numeric)
182 return *(ae->value.numeric);
184 else if (ae->which == Z_AttributeValue_complex) {
186 //yaz_log(YLOG_DEBUG, "Z_Attribute_complex");
187 /* Complex: Shouldn't happen */
191 /* No truncation given */
195 #define SOLR_SPECIAL "+-&|!(){}[]^\"~*?:\\"
197 static int rpn2solr_simple(solr_transform_t ct,
198 void (*pr)(const char *buf, void *client_data),
200 Z_Operand *q, WRBUF w)
203 if (q->which != Z_Operand_APT)
206 solr_transform_set_error(ct, YAZ_BIB1_RESULT_SET_UNSUPP_AS_A_SEARCH_TERM, 0);
210 Z_AttributesPlusTerm *apt = q->u.attributesPlusTerm;
211 Z_Term *term = apt->term;
212 const char *sterm = 0;
214 Odr_int trunc = get_truncation(apt);
217 ret = rpn2solr_attr(ct, apt->attributes, w);
219 if (trunc == 0 || trunc == 1 || trunc == 100 || trunc == 104)
223 solr_transform_set_error(ct, YAZ_BIB1_UNSUPP_TRUNCATION_ATTRIBUTE, 0);
229 lterm = term->u.general->len;
230 sterm = (const char *) term->u.general->buf;
233 wrbuf_printf(w, ODR_INT_PRINTF, *term->u.numeric);
235 case Z_Term_characterString:
236 sterm = term->u.characterString;
237 lterm = strlen(sterm);
241 solr_transform_set_error(ct, YAZ_BIB1_TERM_TYPE_UNSUPP, 0);
249 for (i = 0 ; i < lterm; i++)
254 for (i = 0 ; i < lterm; i++)
256 if (sterm[i] == '\\' && i < lterm - 1)
259 if (strchr(SOLR_SPECIAL, sterm[i]))
261 wrbuf_putc(w, sterm[i]);
263 else if (sterm[i] == '?' && trunc == 104)
267 else if (sterm[i] == '#' && trunc == 104)
271 else if (strchr(SOLR_SPECIAL, sterm[i]))
274 wrbuf_putc(w, sterm[i]);
277 wrbuf_putc(w, sterm[i]);
285 pr(wrbuf_cstr(w), client_data);
291 static int rpn2solr_structure(solr_transform_t ct,
292 void (*pr)(const char *buf, void *client_data),
294 Z_RPNStructure *q, int nested,
297 if (q->which == Z_RPNStructure_simple)
298 return rpn2solr_simple(ct, pr, client_data, q->u.simple, w);
301 Z_Operator *op = q->u.complex->roperator;
305 pr("(", client_data);
307 r = rpn2solr_structure(ct, pr, client_data, q->u.complex->s1, 1, w);
313 pr(" AND ", client_data);
316 pr(" OR ", client_data);
318 case Z_Operator_and_not:
319 pr(" AND NOT ", client_data);
321 case Z_Operator_prox:
322 solr_transform_set_error(ct, YAZ_BIB1_UNSUPP_SEARCH, 0);
325 r = rpn2solr_structure(ct, pr, client_data, q->u.complex->s2, 1, w);
327 pr(")", client_data);
332 int solr_transform_rpn2solr_stream(solr_transform_t ct,
333 void (*pr)(const char *buf, void *client_data),
338 WRBUF w = wrbuf_alloc();
339 solr_transform_set_error(ct, 0, 0);
340 r = rpn2solr_structure(ct, pr, client_data, q->RPNStructure, 0, w);
346 int solr_transform_rpn2solr_wrbuf(solr_transform_t ct,
350 return solr_transform_rpn2solr_stream(ct, wrbuf_vputs, w, q);
356 * c-file-style: "Stroustrup"
357 * indent-tabs-mode: nil
359 * vim: shiftwidth=4 tabstop=8 expandtab