Line data Source code
1 : /*-------------------------------------------------------------------------
2 : *
3 : * to_tsany.c
4 : * to_ts* function definitions
5 : *
6 : * Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group
7 : *
8 : *
9 : * IDENTIFICATION
10 : * src/backend/tsearch/to_tsany.c
11 : *
12 : *-------------------------------------------------------------------------
13 : */
14 : #include "postgres.h"
15 :
16 : #include "common/jsonapi.h"
17 : #include "tsearch/ts_cache.h"
18 : #include "tsearch/ts_utils.h"
19 : #include "utils/builtins.h"
20 : #include "utils/jsonfuncs.h"
21 :
22 :
23 : /*
24 : * Opaque data structure, which is passed by parse_tsquery() to pushval_morph().
25 : */
26 : typedef struct MorphOpaque
27 : {
28 : Oid cfg_id;
29 :
30 : /*
31 : * Single tsquery morph could be parsed into multiple words. When these
32 : * words reside in adjacent positions, they are connected using this
33 : * operator. Usually, that is OP_PHRASE, which requires word positions of
34 : * a complex morph to exactly match the tsvector.
35 : */
36 : int qoperator;
37 : } MorphOpaque;
38 :
39 : typedef struct TSVectorBuildState
40 : {
41 : ParsedText *prs;
42 : Oid cfgId;
43 : } TSVectorBuildState;
44 :
45 : static void add_to_tsvector(void *_state, char *elem_value, int elem_len);
46 :
47 :
48 : Datum
49 0 : get_current_ts_config(PG_FUNCTION_ARGS)
50 : {
51 0 : PG_RETURN_OID(getTSCurrentConfig(true));
52 : }
53 :
54 : /*
55 : * to_tsvector
56 : */
57 : static int
58 12458 : compareWORD(const void *a, const void *b)
59 : {
60 : int res;
61 :
62 12458 : res = tsCompareString(((const ParsedWord *) a)->word, ((const ParsedWord *) a)->len,
63 12458 : ((const ParsedWord *) b)->word, ((const ParsedWord *) b)->len,
64 : false);
65 :
66 12458 : if (res == 0)
67 : {
68 1050 : if (((const ParsedWord *) a)->pos.pos == ((const ParsedWord *) b)->pos.pos)
69 24 : return 0;
70 :
71 1026 : res = (((const ParsedWord *) a)->pos.pos > ((const ParsedWord *) b)->pos.pos) ? 1 : -1;
72 : }
73 :
74 12434 : return res;
75 : }
76 :
77 : static int
78 676 : uniqueWORD(ParsedWord *a, int32 l)
79 : {
80 : ParsedWord *ptr,
81 : *res;
82 : int tmppos;
83 :
84 676 : if (l == 1)
85 : {
86 26 : tmppos = LIMITPOS(a->pos.pos);
87 26 : a->alen = 2;
88 26 : a->pos.apos = (uint16 *) palloc(sizeof(uint16) * a->alen);
89 26 : a->pos.apos[0] = 1;
90 26 : a->pos.apos[1] = tmppos;
91 26 : return l;
92 : }
93 :
94 650 : res = a;
95 650 : ptr = a + 1;
96 :
97 : /*
98 : * Sort words with its positions
99 : */
100 650 : qsort(a, l, sizeof(ParsedWord), compareWORD);
101 :
102 : /*
103 : * Initialize first word and its first position
104 : */
105 650 : tmppos = LIMITPOS(a->pos.pos);
106 650 : a->alen = 2;
107 650 : a->pos.apos = (uint16 *) palloc(sizeof(uint16) * a->alen);
108 650 : a->pos.apos[0] = 1;
109 650 : a->pos.apos[1] = tmppos;
110 :
111 : /*
112 : * Summarize position information for each word
113 : */
114 4192 : while (ptr - a < l)
115 : {
116 3542 : if (!(ptr->len == res->len &&
117 2030 : strncmp(ptr->word, res->word, res->len) == 0))
118 : {
119 : /*
120 : * Got a new word, so put it in result
121 : */
122 2888 : res++;
123 2888 : res->len = ptr->len;
124 2888 : res->word = ptr->word;
125 2888 : tmppos = LIMITPOS(ptr->pos.pos);
126 2888 : res->alen = 2;
127 2888 : res->pos.apos = (uint16 *) palloc(sizeof(uint16) * res->alen);
128 2888 : res->pos.apos[0] = 1;
129 2888 : res->pos.apos[1] = tmppos;
130 : }
131 : else
132 : {
133 : /*
134 : * The word already exists, so adjust position information. But
135 : * before we should check size of position's array, max allowed
136 : * value for position and uniqueness of position
137 : */
138 654 : pfree(ptr->word);
139 654 : if (res->pos.apos[0] < MAXNUMPOS - 1 && res->pos.apos[res->pos.apos[0]] != MAXENTRYPOS - 1 &&
140 654 : res->pos.apos[res->pos.apos[0]] != LIMITPOS(ptr->pos.pos))
141 : {
142 630 : if (res->pos.apos[0] + 1 >= res->alen)
143 : {
144 504 : res->alen *= 2;
145 504 : res->pos.apos = (uint16 *) repalloc(res->pos.apos, sizeof(uint16) * res->alen);
146 : }
147 630 : if (res->pos.apos[0] == 0 || res->pos.apos[res->pos.apos[0]] != LIMITPOS(ptr->pos.pos))
148 : {
149 630 : res->pos.apos[res->pos.apos[0] + 1] = LIMITPOS(ptr->pos.pos);
150 630 : res->pos.apos[0]++;
151 : }
152 : }
153 : }
154 3542 : ptr++;
155 : }
156 :
157 650 : return res + 1 - a;
158 : }
159 :
160 : /*
161 : * make value of tsvector, given parsed text
162 : *
163 : * Note: frees prs->words and subsidiary data.
164 : */
165 : TSVector
166 796 : make_tsvector(ParsedText *prs)
167 : {
168 : int i,
169 : j,
170 796 : lenstr = 0,
171 : totallen;
172 : TSVector in;
173 : WordEntry *ptr;
174 : char *str;
175 : int stroff;
176 :
177 : /* Merge duplicate words */
178 796 : if (prs->curwords > 0)
179 676 : prs->curwords = uniqueWORD(prs->words, prs->curwords);
180 :
181 : /* Determine space needed */
182 4360 : for (i = 0; i < prs->curwords; i++)
183 : {
184 3564 : lenstr += prs->words[i].len;
185 3564 : if (prs->words[i].alen)
186 : {
187 3564 : lenstr = SHORTALIGN(lenstr);
188 3564 : lenstr += sizeof(uint16) + prs->words[i].pos.apos[0] * sizeof(WordEntryPos);
189 : }
190 : }
191 :
192 796 : if (lenstr > MAXSTRPOS)
193 0 : ereport(ERROR,
194 : (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
195 : errmsg("string is too long for tsvector (%d bytes, max %d bytes)", lenstr, MAXSTRPOS)));
196 :
197 796 : totallen = CALCDATASIZE(prs->curwords, lenstr);
198 796 : in = (TSVector) palloc0(totallen);
199 796 : SET_VARSIZE(in, totallen);
200 796 : in->size = prs->curwords;
201 :
202 796 : ptr = ARRPTR(in);
203 796 : str = STRPTR(in);
204 796 : stroff = 0;
205 4360 : for (i = 0; i < prs->curwords; i++)
206 : {
207 3564 : ptr->len = prs->words[i].len;
208 3564 : ptr->pos = stroff;
209 3564 : memcpy(str + stroff, prs->words[i].word, prs->words[i].len);
210 3564 : stroff += prs->words[i].len;
211 3564 : pfree(prs->words[i].word);
212 3564 : if (prs->words[i].alen)
213 : {
214 3564 : int k = prs->words[i].pos.apos[0];
215 : WordEntryPos *wptr;
216 :
217 3564 : if (k > 0xFFFF)
218 0 : elog(ERROR, "positions array too long");
219 :
220 3564 : ptr->haspos = 1;
221 3564 : stroff = SHORTALIGN(stroff);
222 3564 : *(uint16 *) (str + stroff) = (uint16) k;
223 3564 : wptr = POSDATAPTR(in, ptr);
224 7758 : for (j = 0; j < k; j++)
225 : {
226 4194 : WEP_SETWEIGHT(wptr[j], 0);
227 4194 : WEP_SETPOS(wptr[j], prs->words[i].pos.apos[j + 1]);
228 : }
229 3564 : stroff += sizeof(uint16) + k * sizeof(WordEntryPos);
230 3564 : pfree(prs->words[i].pos.apos);
231 : }
232 : else
233 0 : ptr->haspos = 0;
234 3564 : ptr++;
235 : }
236 :
237 796 : if (prs->words)
238 712 : pfree(prs->words);
239 :
240 796 : return in;
241 : }
242 :
243 : Datum
244 478 : to_tsvector_byid(PG_FUNCTION_ARGS)
245 : {
246 478 : Oid cfgId = PG_GETARG_OID(0);
247 478 : text *in = PG_GETARG_TEXT_PP(1);
248 : ParsedText prs;
249 : TSVector out;
250 :
251 478 : prs.lenwords = VARSIZE_ANY_EXHDR(in) / 6; /* just estimation of word's
252 : * number */
253 478 : if (prs.lenwords < 2)
254 338 : prs.lenwords = 2;
255 478 : prs.curwords = 0;
256 478 : prs.pos = 0;
257 478 : prs.words = (ParsedWord *) palloc(sizeof(ParsedWord) * prs.lenwords);
258 :
259 478 : parsetext(cfgId, &prs, VARDATA_ANY(in), VARSIZE_ANY_EXHDR(in));
260 :
261 478 : PG_FREE_IF_COPY(in, 1);
262 :
263 478 : out = make_tsvector(&prs);
264 :
265 478 : PG_RETURN_TSVECTOR(out);
266 : }
267 :
268 : Datum
269 54 : to_tsvector(PG_FUNCTION_ARGS)
270 : {
271 54 : text *in = PG_GETARG_TEXT_PP(0);
272 : Oid cfgId;
273 :
274 54 : cfgId = getTSCurrentConfig(true);
275 54 : PG_RETURN_DATUM(DirectFunctionCall2(to_tsvector_byid,
276 : ObjectIdGetDatum(cfgId),
277 : PointerGetDatum(in)));
278 : }
279 :
280 : /*
281 : * Worker function for jsonb(_string)_to_tsvector(_byid)
282 : */
283 : static TSVector
284 150 : jsonb_to_tsvector_worker(Oid cfgId, Jsonb *jb, uint32 flags)
285 : {
286 : TSVectorBuildState state;
287 : ParsedText prs;
288 :
289 150 : prs.words = NULL;
290 150 : prs.curwords = 0;
291 150 : state.prs = &prs;
292 150 : state.cfgId = cfgId;
293 :
294 150 : iterate_jsonb_values(jb, flags, &state, add_to_tsvector);
295 :
296 150 : return make_tsvector(&prs);
297 : }
298 :
299 : Datum
300 18 : jsonb_string_to_tsvector_byid(PG_FUNCTION_ARGS)
301 : {
302 18 : Oid cfgId = PG_GETARG_OID(0);
303 18 : Jsonb *jb = PG_GETARG_JSONB_P(1);
304 : TSVector result;
305 :
306 18 : result = jsonb_to_tsvector_worker(cfgId, jb, jtiString);
307 18 : PG_FREE_IF_COPY(jb, 1);
308 :
309 18 : PG_RETURN_TSVECTOR(result);
310 : }
311 :
312 : Datum
313 30 : jsonb_string_to_tsvector(PG_FUNCTION_ARGS)
314 : {
315 30 : Jsonb *jb = PG_GETARG_JSONB_P(0);
316 : Oid cfgId;
317 : TSVector result;
318 :
319 30 : cfgId = getTSCurrentConfig(true);
320 30 : result = jsonb_to_tsvector_worker(cfgId, jb, jtiString);
321 30 : PG_FREE_IF_COPY(jb, 0);
322 :
323 30 : PG_RETURN_TSVECTOR(result);
324 : }
325 :
326 : Datum
327 102 : jsonb_to_tsvector_byid(PG_FUNCTION_ARGS)
328 : {
329 102 : Oid cfgId = PG_GETARG_OID(0);
330 102 : Jsonb *jb = PG_GETARG_JSONB_P(1);
331 102 : Jsonb *jbFlags = PG_GETARG_JSONB_P(2);
332 : TSVector result;
333 102 : uint32 flags = parse_jsonb_index_flags(jbFlags);
334 :
335 78 : result = jsonb_to_tsvector_worker(cfgId, jb, flags);
336 78 : PG_FREE_IF_COPY(jb, 1);
337 78 : PG_FREE_IF_COPY(jbFlags, 2);
338 :
339 78 : PG_RETURN_TSVECTOR(result);
340 : }
341 :
342 : Datum
343 24 : jsonb_to_tsvector(PG_FUNCTION_ARGS)
344 : {
345 24 : Jsonb *jb = PG_GETARG_JSONB_P(0);
346 24 : Jsonb *jbFlags = PG_GETARG_JSONB_P(1);
347 : Oid cfgId;
348 : TSVector result;
349 24 : uint32 flags = parse_jsonb_index_flags(jbFlags);
350 :
351 24 : cfgId = getTSCurrentConfig(true);
352 24 : result = jsonb_to_tsvector_worker(cfgId, jb, flags);
353 24 : PG_FREE_IF_COPY(jb, 0);
354 24 : PG_FREE_IF_COPY(jbFlags, 1);
355 :
356 24 : PG_RETURN_TSVECTOR(result);
357 : }
358 :
359 : /*
360 : * Worker function for json(_string)_to_tsvector(_byid)
361 : */
362 : static TSVector
363 150 : json_to_tsvector_worker(Oid cfgId, text *json, uint32 flags)
364 : {
365 : TSVectorBuildState state;
366 : ParsedText prs;
367 :
368 150 : prs.words = NULL;
369 150 : prs.curwords = 0;
370 150 : state.prs = &prs;
371 150 : state.cfgId = cfgId;
372 :
373 150 : iterate_json_values(json, flags, &state, add_to_tsvector);
374 :
375 150 : return make_tsvector(&prs);
376 : }
377 :
378 : Datum
379 18 : json_string_to_tsvector_byid(PG_FUNCTION_ARGS)
380 : {
381 18 : Oid cfgId = PG_GETARG_OID(0);
382 18 : text *json = PG_GETARG_TEXT_P(1);
383 : TSVector result;
384 :
385 18 : result = json_to_tsvector_worker(cfgId, json, jtiString);
386 18 : PG_FREE_IF_COPY(json, 1);
387 :
388 18 : PG_RETURN_TSVECTOR(result);
389 : }
390 :
391 : Datum
392 30 : json_string_to_tsvector(PG_FUNCTION_ARGS)
393 : {
394 30 : text *json = PG_GETARG_TEXT_P(0);
395 : Oid cfgId;
396 : TSVector result;
397 :
398 30 : cfgId = getTSCurrentConfig(true);
399 30 : result = json_to_tsvector_worker(cfgId, json, jtiString);
400 30 : PG_FREE_IF_COPY(json, 0);
401 :
402 30 : PG_RETURN_TSVECTOR(result);
403 : }
404 :
405 : Datum
406 102 : json_to_tsvector_byid(PG_FUNCTION_ARGS)
407 : {
408 102 : Oid cfgId = PG_GETARG_OID(0);
409 102 : text *json = PG_GETARG_TEXT_P(1);
410 102 : Jsonb *jbFlags = PG_GETARG_JSONB_P(2);
411 : TSVector result;
412 102 : uint32 flags = parse_jsonb_index_flags(jbFlags);
413 :
414 78 : result = json_to_tsvector_worker(cfgId, json, flags);
415 78 : PG_FREE_IF_COPY(json, 1);
416 78 : PG_FREE_IF_COPY(jbFlags, 2);
417 :
418 78 : PG_RETURN_TSVECTOR(result);
419 : }
420 :
421 : Datum
422 24 : json_to_tsvector(PG_FUNCTION_ARGS)
423 : {
424 24 : text *json = PG_GETARG_TEXT_P(0);
425 24 : Jsonb *jbFlags = PG_GETARG_JSONB_P(1);
426 : Oid cfgId;
427 : TSVector result;
428 24 : uint32 flags = parse_jsonb_index_flags(jbFlags);
429 :
430 24 : cfgId = getTSCurrentConfig(true);
431 24 : result = json_to_tsvector_worker(cfgId, json, flags);
432 24 : PG_FREE_IF_COPY(json, 0);
433 24 : PG_FREE_IF_COPY(jbFlags, 1);
434 :
435 24 : PG_RETURN_TSVECTOR(result);
436 : }
437 :
438 : /*
439 : * Parse lexemes in an element of a json(b) value, add to TSVectorBuildState.
440 : */
441 : static void
442 744 : add_to_tsvector(void *_state, char *elem_value, int elem_len)
443 : {
444 744 : TSVectorBuildState *state = (TSVectorBuildState *) _state;
445 744 : ParsedText *prs = state->prs;
446 : int32 prevwords;
447 :
448 744 : if (prs->words == NULL)
449 : {
450 : /*
451 : * First time through: initialize words array to a reasonable size.
452 : * (parsetext() will realloc it bigger as needed.)
453 : */
454 216 : prs->lenwords = 16;
455 216 : prs->words = (ParsedWord *) palloc(sizeof(ParsedWord) * prs->lenwords);
456 216 : prs->curwords = 0;
457 216 : prs->pos = 0;
458 : }
459 :
460 744 : prevwords = prs->curwords;
461 :
462 744 : parsetext(state->cfgId, prs, elem_value, elem_len);
463 :
464 : /*
465 : * If we extracted any words from this JSON element, advance pos to create
466 : * an artificial break between elements. This is because we don't want
467 : * phrase searches to think that the last word in this element is adjacent
468 : * to the first word in the next one.
469 : */
470 744 : if (prs->curwords > prevwords)
471 672 : prs->pos += 1;
472 744 : }
473 :
474 :
475 : /*
476 : * to_tsquery
477 : */
478 :
479 :
480 : /*
481 : * This function is used for morph parsing.
482 : *
483 : * The value is passed to parsetext which will call the right dictionary to
484 : * lexize the word. If it turns out to be a stopword, we push a QI_VALSTOP
485 : * to the stack.
486 : *
487 : * All words belonging to the same variant are pushed as an ANDed list,
488 : * and different variants are ORed together.
489 : */
490 : static void
491 3088 : pushval_morph(Datum opaque, TSQueryParserState state, char *strval, int lenval, int16 weight, bool prefix)
492 : {
493 3088 : int32 count = 0;
494 : ParsedText prs;
495 : uint32 variant,
496 3088 : pos = 0,
497 3088 : cntvar = 0,
498 3088 : cntpos = 0,
499 3088 : cnt = 0;
500 3088 : MorphOpaque *data = (MorphOpaque *) DatumGetPointer(opaque);
501 :
502 3088 : prs.lenwords = 4;
503 3088 : prs.curwords = 0;
504 3088 : prs.pos = 0;
505 3088 : prs.words = (ParsedWord *) palloc(sizeof(ParsedWord) * prs.lenwords);
506 :
507 3088 : parsetext(data->cfg_id, &prs, strval, lenval);
508 :
509 3088 : if (prs.curwords > 0)
510 : {
511 5348 : while (count < prs.curwords)
512 : {
513 : /*
514 : * Were any stop words removed? If so, fill empty positions with
515 : * placeholders linked by an appropriate operator.
516 : */
517 2878 : if (pos > 0 && pos + 1 < prs.words[count].pos.pos)
518 : {
519 78 : while (pos + 1 < prs.words[count].pos.pos)
520 : {
521 : /* put placeholders for each missing stop word */
522 48 : pushStop(state);
523 48 : if (cntpos)
524 48 : pushOperator(state, data->qoperator, 1);
525 48 : cntpos++;
526 48 : pos++;
527 : }
528 : }
529 :
530 : /* save current word's position */
531 2878 : pos = prs.words[count].pos.pos;
532 :
533 : /* Go through all variants obtained from this token */
534 2878 : cntvar = 0;
535 5828 : while (count < prs.curwords && pos == prs.words[count].pos.pos)
536 : {
537 2950 : variant = prs.words[count].nvariant;
538 :
539 : /* Push all words belonging to the same variant */
540 2950 : cnt = 0;
541 6044 : while (count < prs.curwords &&
542 3574 : pos == prs.words[count].pos.pos &&
543 3166 : variant == prs.words[count].nvariant)
544 : {
545 3094 : pushValue(state,
546 3094 : prs.words[count].word,
547 3094 : prs.words[count].len,
548 : weight,
549 3094 : ((prs.words[count].flags & TSL_PREFIX) || prefix));
550 3094 : pfree(prs.words[count].word);
551 3094 : if (cnt)
552 144 : pushOperator(state, OP_AND, 0);
553 3094 : cnt++;
554 3094 : count++;
555 : }
556 :
557 2950 : if (cntvar)
558 72 : pushOperator(state, OP_OR, 0);
559 2950 : cntvar++;
560 : }
561 :
562 2878 : if (cntpos)
563 : {
564 : /* distance may be useful */
565 408 : pushOperator(state, data->qoperator, 1);
566 : }
567 :
568 2878 : cntpos++;
569 : }
570 :
571 2470 : pfree(prs.words);
572 : }
573 : else
574 618 : pushStop(state);
575 3088 : }
576 :
577 : Datum
578 754 : to_tsquery_byid(PG_FUNCTION_ARGS)
579 : {
580 754 : text *in = PG_GETARG_TEXT_PP(1);
581 : TSQuery query;
582 : MorphOpaque data;
583 :
584 754 : data.cfg_id = PG_GETARG_OID(0);
585 :
586 : /*
587 : * Passing OP_PHRASE as a qoperator makes tsquery require matching of word
588 : * positions of a complex morph exactly match the tsvector. Also, when
589 : * the complex morphs are connected with OP_PHRASE operator, we connect
590 : * all their words into the OP_PHRASE sequence.
591 : */
592 754 : data.qoperator = OP_PHRASE;
593 :
594 754 : query = parse_tsquery(text_to_cstring(in),
595 : pushval_morph,
596 : PointerGetDatum(&data),
597 : 0,
598 : NULL);
599 :
600 754 : PG_RETURN_TSQUERY(query);
601 : }
602 :
603 : Datum
604 132 : to_tsquery(PG_FUNCTION_ARGS)
605 : {
606 132 : text *in = PG_GETARG_TEXT_PP(0);
607 : Oid cfgId;
608 :
609 132 : cfgId = getTSCurrentConfig(true);
610 132 : PG_RETURN_DATUM(DirectFunctionCall2(to_tsquery_byid,
611 : ObjectIdGetDatum(cfgId),
612 : PointerGetDatum(in)));
613 : }
614 :
615 : Datum
616 60 : plainto_tsquery_byid(PG_FUNCTION_ARGS)
617 : {
618 60 : text *in = PG_GETARG_TEXT_PP(1);
619 : TSQuery query;
620 : MorphOpaque data;
621 :
622 60 : data.cfg_id = PG_GETARG_OID(0);
623 :
624 : /*
625 : * parse_tsquery() with P_TSQ_PLAIN flag takes the whole input text as a
626 : * single morph. Passing OP_PHRASE as a qoperator makes tsquery require
627 : * matching of all words independently on their positions.
628 : */
629 60 : data.qoperator = OP_AND;
630 :
631 60 : query = parse_tsquery(text_to_cstring(in),
632 : pushval_morph,
633 : PointerGetDatum(&data),
634 : P_TSQ_PLAIN,
635 : NULL);
636 :
637 60 : PG_RETURN_POINTER(query);
638 : }
639 :
640 : Datum
641 12 : plainto_tsquery(PG_FUNCTION_ARGS)
642 : {
643 12 : text *in = PG_GETARG_TEXT_PP(0);
644 : Oid cfgId;
645 :
646 12 : cfgId = getTSCurrentConfig(true);
647 12 : PG_RETURN_DATUM(DirectFunctionCall2(plainto_tsquery_byid,
648 : ObjectIdGetDatum(cfgId),
649 : PointerGetDatum(in)));
650 : }
651 :
652 :
653 : Datum
654 48 : phraseto_tsquery_byid(PG_FUNCTION_ARGS)
655 : {
656 48 : text *in = PG_GETARG_TEXT_PP(1);
657 : TSQuery query;
658 : MorphOpaque data;
659 :
660 48 : data.cfg_id = PG_GETARG_OID(0);
661 :
662 : /*
663 : * parse_tsquery() with P_TSQ_PLAIN flag takes the whole input text as a
664 : * single morph. Passing OP_PHRASE as a qoperator makes tsquery require
665 : * matching of word positions.
666 : */
667 48 : data.qoperator = OP_PHRASE;
668 :
669 48 : query = parse_tsquery(text_to_cstring(in),
670 : pushval_morph,
671 : PointerGetDatum(&data),
672 : P_TSQ_PLAIN,
673 : NULL);
674 :
675 48 : PG_RETURN_TSQUERY(query);
676 : }
677 :
678 : Datum
679 0 : phraseto_tsquery(PG_FUNCTION_ARGS)
680 : {
681 0 : text *in = PG_GETARG_TEXT_PP(0);
682 : Oid cfgId;
683 :
684 0 : cfgId = getTSCurrentConfig(true);
685 0 : PG_RETURN_DATUM(DirectFunctionCall2(phraseto_tsquery_byid,
686 : ObjectIdGetDatum(cfgId),
687 : PointerGetDatum(in)));
688 : }
689 :
690 : Datum
691 408 : websearch_to_tsquery_byid(PG_FUNCTION_ARGS)
692 : {
693 408 : text *in = PG_GETARG_TEXT_PP(1);
694 : MorphOpaque data;
695 408 : TSQuery query = NULL;
696 :
697 408 : data.cfg_id = PG_GETARG_OID(0);
698 :
699 : /*
700 : * Passing OP_PHRASE as a qoperator makes tsquery require matching of word
701 : * positions of a complex morph exactly match the tsvector. Also, when
702 : * the complex morphs are given in quotes, we connect all their words into
703 : * the OP_PHRASE sequence.
704 : */
705 408 : data.qoperator = OP_PHRASE;
706 :
707 408 : query = parse_tsquery(text_to_cstring(in),
708 : pushval_morph,
709 : PointerGetDatum(&data),
710 : P_TSQ_WEB,
711 : NULL);
712 :
713 408 : PG_RETURN_TSQUERY(query);
714 : }
715 :
716 : Datum
717 24 : websearch_to_tsquery(PG_FUNCTION_ARGS)
718 : {
719 24 : text *in = PG_GETARG_TEXT_PP(0);
720 : Oid cfgId;
721 :
722 24 : cfgId = getTSCurrentConfig(true);
723 24 : PG_RETURN_DATUM(DirectFunctionCall2(websearch_to_tsquery_byid,
724 : ObjectIdGetDatum(cfgId),
725 : PointerGetDatum(in)));
726 : }
|