MCPcopy Create free account
hub / github.com/documentdb/documentdb / EvaluateMetaTextScore

Function EvaluateMetaTextScore

pg_documentdb/src/opclass/bson_text_gin.c:385–441  ·  view source on GitHub ↗

* Evaluates the ts_rank function for a given document for a persisted global * state tsquery and index options (from bson_dollar_text_meta_qual). * Note: this is currently a hack until $let support is added. */

Source from the content-addressed store, hash-verified

383 * Note: this is currently a hack until $let support is added.
384 */
385double
386EvaluateMetaTextScore(pgbson *document)
387{
388 if (QueryTextData == NULL)
389 {
390 ereport(ERROR, (errcode(ERRCODE_DOCUMENTDB_LOCATION40218),
391 errmsg(
392 "The query needs text score metadata, but this required information is currently unavailable.")));
393 }
394
395 if (QueryTextData->indexOptions == NULL ||
396 QueryTextData->query == (Datum) 0)
397 {
398 bool isDatumQueryNull = QueryTextData->query == (Datum) 0;
399 ereport(ERROR, (errcode(ERRCODE_DOCUMENTDB_INTERNALERROR),
400 errmsg(
401 "query text data is provided, but required properties are null."),
402 errdetail_log(
403 "query text data is provided, but required properties are null - indexOptions %d, query %d",
404 QueryTextData->indexOptions == NULL,
405 isDatumQueryNull)));
406 }
407
408 BsonGinTextPathOptions *textOptions =
409 (BsonGinTextPathOptions *) QueryTextData->indexOptions;
410 TSVector vector = GenerateTsVectorWithOptions(document, textOptions);
411
412 if (vector == NULL)
413 {
414 ereport(ERROR, (errmsg("Unexpected, text vector is not valid for $meta query")));
415 }
416
417 /* Now call ts_rank with the vector and query */
418 const char *pathSpecBytes = NULL;
419
420#pragma GCC diagnostic push
421#pragma GCC diagnostic ignored "-Wunused-but-set-variable"
422 uint32_t pathCount;
423 Get_Index_Path_Option(textOptions, weights, pathSpecBytes, pathCount);
424#pragma GCC diagnostic pop
425
426 /* The datum array is right after the path count.
427 * Use memcpy to avoid undefined behavior from misaligned access
428 * the reloption storage is not guaranteed to be Datum (4 or 8-bytes) aligned. */
429 Datum weightDatumAligned[4];
430 memcpy(weightDatumAligned, pathSpecBytes, sizeof(Datum) * 4);
431 ArrayType *rankArray = construct_array(weightDatumAligned, 4, FLOAT4OID,
432 sizeof(float), true,
433 TYPALIGN_INT);
434
435 Datum result = OidFunctionCall3(TsRankFunctionId(),
436 PointerGetDatum(rankArray),
437 PointerGetDatum(vector),
438 QueryTextData->query);
439
440 return (double) DatumGetFloat4(result);
441}
442

Callers 2

BsonOrderbyCoreFunction · 0.85

Calls 2

TsRankFunctionIdFunction · 0.85

Tested by

no test coverage detected