Skip to content

Commit a5174d5

Browse files
author
Ariel Shtul
authored
[LLAPI] getter functions for score, language and stopwords list (#2184) (#2186)
* [LLAPI] get score, language and stopwords list * change API per review * add score and language to RSIndexOptions * fix sanitiser (cherry picked from commit 3ddf90e)
1 parent 7a107c6 commit a5174d5

9 files changed

Lines changed: 99 additions & 3 deletions

File tree

src/document_basic.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ int Redis_SaveDocument(RedisSearchCtx *ctx, const AddDocumentOptions *opts, Quer
324324
arguments = array_append(arguments, opts->keyStr);
325325
arguments = array_ensure_append_n(arguments, opts->fieldsArray, opts->numFieldElems);
326326

327-
if (opts->score != 1.0 || (opts->options & DOCUMENT_ADD_PARTIAL)) {
327+
if (opts->score != DEFAULT_SCORE || (opts->options & DOCUMENT_ADD_PARTIAL)) {
328328
arguments = array_append(arguments, globalAddRSstrings[0]);
329329
arguments = array_append(arguments, opts->scoreStr);
330330
}

src/redisearch_api.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ IndexSpec* RediSearch_CreateIndex(const char* name, const RSIndexOptions* option
3232
spec->indexer = NewIndexer(spec);
3333
}
3434

35+
if (options->score || options->lang) {
36+
spec->rule = rm_calloc(1, sizeof *spec->rule);
37+
spec->rule->score_default = options->score ? options->score : DEFAULT_SCORE;
38+
spec->rule->lang_default = options->lang ? options->lang : DEFAULT_LANGUAGE;
39+
}
40+
3541
spec->getValue = options->gvcb;
3642
spec->getValueCtx = options->gvcbData;
3743
if (options->flags & RSIDXOPT_DOCTBLSIZE_UNLIMITED) {
@@ -54,6 +60,24 @@ void RediSearch_DropIndex(IndexSpec* sp) {
5460
RWLOCK_RELEASE();
5561
}
5662

63+
char **RediSearch_IndexGetStopwords(IndexSpec* sp, size_t *size) {
64+
return GetStopWordsList(sp->stopwords, size);
65+
}
66+
67+
double RediSearch_IndexGetScore(IndexSpec* sp) {
68+
if (sp->rule) {
69+
return sp->rule->score_default;
70+
}
71+
return DEFAULT_SCORE;
72+
}
73+
74+
const char *RediSearch_IndexGetLanguage(IndexSpec* sp) {
75+
if (sp->rule) {
76+
return RSLanguage_ToString(sp->rule->lang_default);
77+
}
78+
return RSLanguage_ToString(DEFAULT_LANGUAGE);
79+
}
80+
5781
RSFieldID RediSearch_CreateField(IndexSpec* sp, const char* name, unsigned types,
5882
unsigned options) {
5983
RS_LOG_ASSERT(types, "types should not be RSFLDTYPE_DEFAULT");

src/redisearch_api.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define SRC_REDISEARCH_API_H_
33

44
#include "redismodule.h"
5+
#include "stemmer.h"
56
#include <limits.h>
67

78
#ifdef __cplusplus
@@ -85,6 +86,8 @@ struct RSIdxOptions {
8586
int gcPolicy;
8687
char **stopwords;
8788
int stopwordsLen;
89+
double score;
90+
RSLanguage lang;
8891
};
8992

9093
/**
@@ -114,6 +117,11 @@ MODULE_API_FUNC(void, RediSearch_DropIndex)(RSIndex*);
114117
/** Handle Stopwords list */
115118
MODULE_API_FUNC(int, RediSearch_StopwordsList_Contains)(RSIndex* idx, const char *term, size_t len);
116119

120+
/** Getter functions */
121+
MODULE_API_FUNC(char **, RediSearch_IndexGetStopwords)(RSIndex*, size_t*);
122+
MODULE_API_FUNC(double, RediSearch_IndexGetScore)(RSIndex*);
123+
MODULE_API_FUNC(const char *, RediSearch_IndexGetLanguage)(RSIndex*);
124+
117125
/**
118126
* Create a new field in the index
119127
* @param idx the index
@@ -252,6 +260,9 @@ MODULE_API_FUNC(void, RediSearch_IndexOptionsSetGCPolicy)(RSIndexOptions* option
252260
X(FreeIndexOptions) \
253261
X(CreateIndex) \
254262
X(DropIndex) \
263+
X(IndexGetStopwords) \
264+
X(IndexGetScore) \
265+
X(IndexGetLanguage) \
255266
X(CreateField) \
256267
X(TextFieldSetWeight) \
257268
X(TagFieldSetSeparator) \

src/rules.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ SchemaRule *SchemaRule_Create(SchemaRuleArgs *args, IndexSpec *spec, QueryError
6868
}
6969
rule->score_default = score;
7070
} else {
71-
rule->score_default = 1.0;
71+
rule->score_default = DEFAULT_SCORE;
7272
}
7373

7474
if (args->lang_default) {

src/spec.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1862,7 +1862,7 @@ int IndexSpec_UpdateWithHash(IndexSpec *spec, RedisModuleCtx *ctx, RedisModuleSt
18621862

18631863
RedisSearchCtx sctx = SEARCH_CTX_STATIC(ctx, spec);
18641864
Document doc = {0};
1865-
Document_Init(&doc, key, 1.0, DEFAULT_LANGUAGE);
1865+
Document_Init(&doc, key, DEFAULT_SCORE, DEFAULT_LANGUAGE);
18661866
// if a key does not exit, is not a hash or has no fields in index schema
18671867
if (Document_LoadSchemaFields(&doc, &sctx) != REDISMODULE_OK) {
18681868
// if a document did not load properly, it is deleted

src/spec.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ struct DocumentIndexer;
5151
#define SPEC_ASYNC_STR "ASYNC"
5252
#define SPEC_SKIPINITIALSCAN_STR "SKIPINITIALSCAN"
5353

54+
#define DEFAULT_SCORE 1.0
55+
5456
#define SPEC_FOLLOW_HASH_ARGS_DEF(rule) \
5557
{.name = "PREFIX", .target = &rule_prefixes, .type = AC_ARGTYPE_SUBARGS}, \
5658
{.name = "FILTER", \

src/stopwords.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
#include "stopwords.h"
33
#include "dep/triemap/triemap.h"
44
#include "rmalloc.h"
5+
#include "util/strconv.h"
6+
#include "rmutil/rm_assert.h"
57
#include <ctype.h>
68

79
#define MAX_STOPWORDLIST_SIZE 1024
@@ -170,3 +172,27 @@ void ReplyWithStopWordsList(RedisModuleCtx *ctx, struct StopWordList *sl) {
170172
RedisModule_ReplySetArrayLength(ctx, i);
171173
TrieMapIterator_Free(it);
172174
}
175+
176+
char **GetStopWordsList(struct StopWordList *sl, size_t *size) {
177+
*size = sl->m->cardinality;
178+
if (*size == 0) {
179+
return NULL;
180+
}
181+
182+
char **list = rm_malloc((*size) * sizeof(*list));
183+
184+
TrieMapIterator *it = TrieMap_Iterate(sl->m, "", 0);
185+
char *str;
186+
tm_len_t len;
187+
void *ptr;
188+
size_t i = 0;
189+
190+
while (TrieMapIterator_Next(it, &str, &len, &ptr)) {
191+
list[i++] = rm_strndup(str, len);
192+
}
193+
194+
TrieMapIterator_Free(it);
195+
RS_LOG_ASSERT(i == *size, "actual size must equal expected size");
196+
197+
return list;
198+
}

src/stopwords.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ void StopWordList_Ref(struct StopWordList *sl);
4646

4747
void ReplyWithStopWordsList(RedisModuleCtx *ctx, struct StopWordList *sl);
4848

49+
/* Returns a NULL terminated list of stopwords */
50+
char **GetStopWordsList(struct StopWordList *sl, size_t *size);
51+
4952
#ifdef __cplusplus
5053
}
5154
#endif

tests/cpptests/test_cpp_llapi.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -843,6 +843,16 @@ TEST_F(LLApiTest, testStopwords) {
843843
ASSERT_EQ(RediSearch_StopwordsList_Contains(index, words[0], strlen(words[0])), 1);
844844
ASSERT_EQ(RediSearch_StopwordsList_Contains(index, words[1], strlen(words[1])), 1);
845845
ASSERT_EQ(RediSearch_StopwordsList_Contains(index, "RediSearch", strlen("RediSearch")), 0);
846+
847+
size_t size;
848+
char **list = RediSearch_IndexGetStopwords(index, &size);
849+
ASSERT_EQ(size, 2);
850+
ASSERT_STRCASEEQ(list[0], words[0]);
851+
ASSERT_STRCASEEQ(list[1], words[1]);
852+
rm_free(list[0]);
853+
rm_free(list[1]);
854+
rm_free(list);
855+
846856
RediSearch_FreeIndexOptions(options);
847857
RediSearch_DropIndex(index);
848858

@@ -856,3 +866,23 @@ TEST_F(LLApiTest, testStopwords) {
856866
RediSearch_FreeIndexOptions(options);
857867
RediSearch_DropIndex(index);
858868
}
869+
870+
TEST_F(LLApiTest, testGetters) {
871+
// test defaults
872+
RSIndex* index = RediSearch_CreateIndex("index", NULL);
873+
ASSERT_EQ(DEFAULT_SCORE, RediSearch_IndexGetScore(index));
874+
ASSERT_STREQ(RSLanguage_ToString(DEFAULT_LANGUAGE), RediSearch_IndexGetLanguage(index));
875+
RediSearch_DropIndex(index);
876+
877+
// test custom language and score
878+
RSIndexOptions *opt = RediSearch_CreateIndexOptions();
879+
opt->score = 0.42;
880+
opt->lang = RS_LANG_YIDDISH;
881+
882+
index = RediSearch_CreateIndex("index", opt);
883+
ASSERT_EQ(0.42, RediSearch_IndexGetScore(index));
884+
ASSERT_STREQ("yiddish", RediSearch_IndexGetLanguage(index));
885+
886+
RediSearch_FreeIndexOptions(opt);
887+
RediSearch_DropIndex(index);
888+
}

0 commit comments

Comments
 (0)