1 #ifndef __QUERY_NODE_H__
2 #define __QUERY_NODE_H__
3 #include <stdlib.h>
4 #include "redisearch.h"
5 #include "query_error.h"
6 //#include "numeric_index.h"
7 
8 struct RSQueryNode;
9 struct numericFilter;
10 struct geoFilter;
11 struct idFilter;
12 
13 /* The types of query nodes */
14 typedef enum {
15   /* Phrase (AND) node, exact or not */
16   QN_PHRASE = 1,
17   /* Union (OR) Node */
18   QN_UNION,
19   /* Single token node */
20   QN_TOKEN,
21   /* Numeric filter node */
22   QN_NUMERIC,
23 
24   /* NOT operator node */
25   QN_NOT,
26 
27   /* OPTIONAL (should match) node */
28   QN_OPTIONAL,
29 
30   /* Geo filter node */
31   QN_GEO,
32 
33   /* Prefix selection node */
34   QN_PREFX,
35 
36   /* Id Filter node */
37   QN_IDS,
38 
39   /* Wildcard node, used only in conjunction with negative root node to allow negative queries */
40   QN_WILDCARD,
41 
42   /* Tag node, a list of tags for a specific tag field */
43   QN_TAG,
44 
45   /* Fuzzy term - expand with levenshtein distance */
46   QN_FUZZY,
47 
48   /* Lexical range */
49   QN_LEXRANGE,
50 
51   /* Null term - take no action */
52   QN_NULL
53 } QueryNodeType;
54 
55 /* A prhase node represents a list of nodes with intersection between them, or a phrase in the case
56  * of several token nodes. */
57 typedef struct {
58   int exact;
59 } QueryPhraseNode;
60 
61 /**
62  * Query node used when the query is effectively null but not invalid. This
63  * might happen as a result of a query containing only stopwords.
64  */
65 typedef struct {
66   int dummy;
67 } QueryNullNode;
68 
69 typedef struct {
70   const char *fieldName;
71   size_t len;
72 } QueryTagNode;
73 
74 /* A token node is a terminal, single term/token node. An expansion of synonyms is represented by a
75  * Union node with several token nodes. A token can have private metadata written by expanders or
76  * tokenizers. Later this gets passed to scoring functions in a Term object. See RSIndexRecord */
77 typedef RSToken QueryTokenNode;
78 
79 typedef RSToken QueryPrefixNode;
80 
81 typedef struct {
82   RSToken tok;
83   int maxDist;
84 } QueryFuzzyNode;
85 
86 /* A node with a numeric filter */
87 typedef struct {
88   struct NumericFilter *nf;
89 } QueryNumericNode;
90 
91 typedef struct {
92   const struct GeoFilter *gf;
93 } QueryGeofilterNode;
94 
95 typedef struct {
96   t_docId *ids;
97   size_t len;
98 } QueryIdFilterNode;
99 
100 typedef struct {
101   char *begin;
102   bool includeBegin;
103   char *end;
104   bool includeEnd;
105 } QueryLexRangeNode;
106 
107 typedef enum {
108   QueryNode_Verbatim = 0x01,
109 } QueryNodeFlags;
110 
111 /* Query attribute is a dynamic attribute that can be applied to any query node.
112  * Currently supported are weight, slop, and inorder
113  */
114 typedef struct {
115   const char *name;
116   size_t namelen;
117   const char *value;
118   size_t vallen;
119 } QueryAttribute;
120 
121 #define PHONETIC_ENABLED 1
122 #define PHONETIC_DISABLED 2
123 #define PHONETIC_DEFAULT 0
124 
125 /* Various modifiers and options that can apply to the entire query or any sub-query of it */
126 typedef struct {
127   QueryNodeFlags flags;
128   t_fieldMask fieldMask;
129   int maxSlop;
130   int inOrder;
131   double weight;
132   int phonetic;
133 } QueryNodeOptions;
134 
135 typedef QueryNullNode QueryUnionNode, QueryNotNode, QueryOptionalNode;
136 
137 /* QueryNode reqresents any query node in the query tree. It has a type to resolve which node it
138  * is, and a union of all possible nodes  */
139 typedef struct RSQueryNode {
140   union {
141     QueryPhraseNode pn;
142     QueryTokenNode tn;
143     QueryUnionNode un;
144     QueryNumericNode nn;
145     QueryGeofilterNode gn;
146     QueryIdFilterNode fn;
147     QueryNotNode inverted;
148     QueryOptionalNode opt;
149     QueryPrefixNode pfx;
150     QueryTagNode tag;
151     QueryFuzzyNode fz;
152     QueryLexRangeNode lxrng;
153   };
154 
155   /* The node type, for resolving the union access */
156   QueryNodeType type;
157   QueryNodeOptions opts;
158   struct RSQueryNode **children;
159 } QueryNode;
160 
161 int QueryNode_ApplyAttributes(QueryNode *qn, QueryAttribute *attr, size_t len, QueryError *status);
162 
163 void QueryNode_AddChildren(QueryNode *parent, QueryNode **children, size_t n);
164 void QueryNode_AddChild(QueryNode *parent, QueryNode *child);
165 void QueryNode_ClearChildren(QueryNode *parent, int shouldFree);
166 
167 #define QueryNode_NumChildren(qn) ((qn)->children ? array_len((qn)->children) : 0)
168 #define QueryNode_GetChild(qn, ix) (QueryNode_NumChildren(qn) > ix ? (qn)->children[ix] : NULL)
169 
170 typedef int (*QueryNode_ForEachCallback)(QueryNode *node, QueryNode *q, void *ctx);
171 int QueryNode_ForEach(QueryNode *q, QueryNode_ForEachCallback callback, void *ctx, int reverse);
172 
173 #endif
174