1 #ifndef SRC_FIELD_SPEC_H_
2 #define SRC_FIELD_SPEC_H_
3 
4 #include "redisearch.h"
5 #include "value.h"
6 
7 #ifdef __cplusplus
8 #define RS_ENUM_BITWISE_HELPER(T)   \
9   inline T operator|=(T a, int b) { \
10     return (T)((int)a | b);         \
11   }
12 #else
13 #define RS_ENUM_BITWISE_HELPER(T)
14 #endif
15 
16 typedef enum {
17   // Newline
18   INDEXFLD_T_FULLTEXT = 0x01,
19   INDEXFLD_T_NUMERIC = 0x02,
20   INDEXFLD_T_GEO = 0x04,
21   INDEXFLD_T_TAG = 0x08
22 } FieldType;
23 
24 #define INDEXFLD_NUM_TYPES 4
25 
26 // clang-format off
27 // otherwise, it looks h o r r i b l e
28 #define INDEXTYPE_TO_POS(T)           \
29   (T == INDEXFLD_T_FULLTEXT   ? 0 : \
30   (T == INDEXFLD_T_NUMERIC    ? 1 : \
31   (T == INDEXFLD_T_GEO        ? 2 : \
32   (T == INDEXFLD_T_TAG        ? 3 : -1))))
33 
34 #define INDEXTYPE_FROM_POS(P) (1<<(P))
35 // clang-format on
36 
37 #define IXFLDPOS_FULLTEXT INDEXTYPE_TO_POS(INDEXFLD_T_FULLTEXT)
38 #define IXFLDPOS_NUMERIC INDEXTYPE_TO_POS(INDEXFLD_T_NUMERIC)
39 #define IXFLDPOS_GEO INDEXTYPE_TO_POS(INDEXFLD_T_GEO)
40 #define IXFLDPOS_TAG INDEXTYPE_TO_POS(INDEXFLD_T_TAG)
41 
42 RS_ENUM_BITWISE_HELPER(FieldType)
43 
44 typedef enum {
45   FieldSpec_Sortable = 0x01,
46   FieldSpec_NoStemming = 0x02,
47   FieldSpec_NotIndexable = 0x04,
48   FieldSpec_Phonetics = 0x08,
49   FieldSpec_Dynamic = 0x10,
50   FieldSpec_UNF = 0x20,
51 } FieldSpecOptions;
52 
53 RS_ENUM_BITWISE_HELPER(FieldSpecOptions)
54 
55 // Flags for tag fields
56 typedef enum {
57   TagField_CaseSensitive = 0x01,
58   TagField_TrimSpace = 0x02,
59   TagField_RemoveAccents = 0x04,
60 } TagFieldFlags;
61 
62 RS_ENUM_BITWISE_HELPER(TagFieldFlags)
63 
64 /* The fieldSpec represents a single field in the document's field spec.
65 Each field has a unique id that's a power of two, so we can filter fields
66 by a bit mask.
67 Each field has a type, allowing us to add non text fields in the future */
68 typedef struct FieldSpec {
69   char* name;
70   FieldType types : 8;
71   FieldSpecOptions options : 8;
72 
73   /** If this field is sortable, the sortable index */
74   int16_t sortIdx;
75 
76   /** Unique field index. Each field has a unique index regardless of its type */
77   uint16_t index;
78 
79   // Flags for tag options
80   TagFieldFlags tagFlags : 16;
81   char tagSep;
82 
83   // weight in frequency calculations
84   double ftWeight;
85   // ID used to identify the field within the field mask
86   t_fieldId ftId;
87 
88   // TODO: More options here..
89 } FieldSpec;
90 
91 #define FIELD_IS(f, t) (((f)->types) & t)
92 #define FIELD_CHKIDX(fmask, ix) (fmask & ix)
93 
94 #define TAG_FIELD_DEFAULT_FLAGS (TagFieldFlags)(TagField_TrimSpace | TagField_RemoveAccents);
95 #define TAG_FIELD_DEFAULT_SEP ','
96 
97 #define FieldSpec_IsSortable(fs) ((fs)->options & FieldSpec_Sortable)
98 #define FieldSpec_IsNoStem(fs) ((fs)->options & FieldSpec_NoStemming)
99 #define FieldSpec_IsPhonetics(fs) ((fs)->options & FieldSpec_Phonetics)
100 #define FieldSpec_IsIndexable(fs) (0 == ((fs)->options & FieldSpec_NotIndexable))
101 
102 void FieldSpec_SetSortable(FieldSpec* fs);
103 void FieldSpec_Cleanup(FieldSpec* fs);
104 
105 RSValueType fieldTypeToValueType(FieldType ft);
106 
107 #endif /* SRC_FIELD_SPEC_H_ */
108