1
  
2
  
3
  
4
  
5
  
6
  
7
  
8
  
9
  
10
  
11
  
12
  
13
  
14
  
15
  
16
  
17
  
18
  
19
  
20
  
21
  
22
  
23
  
24
  
25
  
26
  
27
  
28
  
29
  
30
  
31
  
32
  
33
  
34
  
35
  
36
  
37
  
38
  
39
  
40
  
41
  
42
  
43
  
44
  
45
  
46
  
47
  
48
  
49
  
50
  
51
  
52
  
53
  
54
  
55
  
56
  
57
  
58
  
59
  
60
  
61
  
62
  
63
  
64
  
65
  
66
  
67
  
68
  
69
  
70
  
71
  
72
  
73
  
74
  
75
  
76
  
77
  
78
  
79
  
80
  
81
  
82
  
83
  
84
  
85
  
86
  
87
  
88
  
89
  
90
  
91
  
92
  
93
  
94
  
95
  
96
  
97
  
98
  
99
  
100
  
101
  
102
  
103
  
104
  
105
  
106
  
107
  
108
  
109
  
110
  
111
  
112
  
113
  
114
  
115
  
116
  
117
  
118
  
119
  
120
  
121
  
122
  
123
  
124
  
125
  
126
  
127
  
128
  
129
  
130
  
131
  
132
  
133
  
134
  
135
  
136
  
137
  
138
  
139
  
140
  
141
  
142
  
143
  
144
  
145
  
146
  
147
  
148
  
149
  
150
  
151
  
152
  
153
  
154
  
155
  
156
  
157
  
158
  
159
  
160
  
161
  
162
  
163
  
164
  
165
  
166
  
167
  
168
  
169
  
170
  
171
  
172
  
173
  
174
  
175
  
176
  
177
  
mapping blob_done=([]); 
 
static function(int:string) blobfeeder(Search.Database.Base db, array word_ids) 
{ 
  mapping state = mkmapping(word_ids,allocate(sizeof(word_ids))); 
  return lambda( int word ) 
         { 
           return db->get_blob(word, state[word]++); 
         }; 
} 
 
 
Search.ResultSet do_query_or(Search.Database.Base db, 
                             array(string) words, 
                             Search.RankingProfile ranking) 
{ 
  array(int) word_ids=map(Array.uniq(words), db->hash_word); 
  return _WhiteFish.do_query_or(word_ids, 
                                ranking->field_ranking, 
                                ranking->prox_ranking, 
                                ranking->cutoff, 
                                blobfeeder(db, word_ids)); 
} 
 
Search.ResultSet do_query_and(Search.Database.Base db, 
                              array(string) words, 
                              Search.RankingProfile ranking) 
{ 
  array(int) word_ids=map(Array.uniq(words), db->hash_word); 
  return _WhiteFish.do_query_or(word_ids, 
                                ranking->field_ranking, 
                                ranking->prox_ranking, 
                                ranking->cutoff, 
                                blobfeeder(db, word_ids)); 
} 
 
Search.ResultSet do_query_phrase(Search.Database.Base db, 
                                 array(string) words, 
                                 Search.RankingProfile ranking) 
{ 
  array(int) word_ids=map(words, db->hash_word); 
  return _WhiteFish.do_query_phrase(word_ids, 
                                    ranking->field_ranking, 
                                    //    ranking->cutoff, 
                                    blobfeeder(db, word_ids)); 
} 
 
 
 
// Execute a parsed, optimized and validated query by a parser 
 
static void lowlevel(string s, mixed ... args) { 
  werror(s, @args); 
  werror("\n"); 
} 
 
static constant ParseNode = Search.Grammar.ParseNode; 
 
void execute(ParseNode q) { 
  switch (q->op) { 
    case "and": 
      { 
      int first = 1; 
      foreach (q->children, ParseNode child) 
        if (child->op != "date") { 
          execute(child); 
          if (!first) 
            lowlevel("AND"); 
          else 
            first = 0; 
        } 
      foreach (q->children, ParseNode child) 
        if (child->op == "date") 
          execute(child); 
      } 
      break; 
    case "or": 
      int first = 1; 
      foreach (q->children, ParseNode child) { 
        execute(child); 
        if (!first) 
          lowlevel("OR"); 
        else 
          first = 0; 
      } 
      break; 
    case "date": 
      lowlevel("DATE_FILTER %O", q->date); 
      break; 
    case "text": 
      { 
      int hasPlus = sizeof(q->plusWords) || sizeof(q->plusPhrases); 
      int hasOrdinary = sizeof(q->words) || sizeof(q->phrases); 
      int hasMinus = sizeof(q->minusWords) || sizeof(q->minusPhrases); 
      if (hasPlus) { 
        int first = 1; 
        if (sizeof(q->plusWords)) { 
          lowlevel("QUERY_AND     field:%O %{ %O%}", q->field, q->plusWords); 
          first = 0; 
        } 
        foreach (q->plusPhrases, array(string) ph) { 
          lowlevel("QUERY_PHRASE  field:%O %{ %O%}", q->field, ph); 
          if (first) 
            first = 0; 
          else 
            lowlevel("AND"); 
        } 
      } 
      if (hasOrdinary) { 
        int first = 1; 
        if (sizeof(q->words)) { 
          lowlevel("QUERY_OR      field:%O %{ %O%}", q->field, q->words); 
          first = 0; 
        } 
        foreach (q->phrases, array(string) ph) { 
          lowlevel("QUERY_PHRASE  field:%O %{ %O%}", q->field, ph); 
          if (first) 
            first = 0; 
          else 
            lowlevel("OR"); 
        } 
      } 
 
      if (hasPlus && hasOrdinary) 
        lowlevel("UPRANK");          // the XXX operation :) 
 
      if (hasMinus) { 
        int first = 1; 
        if (sizeof(q->minusWords)) { 
          lowlevel("QUERY_OR      field:%O %{ %O%}", q->field, q->minusWords); 
          first = 0; 
        } 
        foreach (q->minusPhrases, array(string) ph) { 
          lowlevel("QUERY_PHRASE  field:%O %{ %O%}", q->field, ph); 
          if (first) 
            first = 0; 
          else 
            lowlevel("OR"); 
        } 
        lowlevel("SUB"); // Only - words are not allowed 
      } 
 
      break; 
      } 
  } // switch (q->op) 
} 
 
 
/* Test stuff */ 
Search.ResultSet test_query(Search.Database.Base db, array(string) words) 
{ 
  array(int) field_ranking=allocate(66); 
  field_ranking[0]=17; 
  field_ranking[2]=147; 
 
  array(int) prox_ranking=allocate(8); 
  for(int i=0; i<8; i++) 
    prox_ranking[i]=8-i; 
 
  return _WhiteFish.do_query_and(map(words, hash), 
                                   field_ranking, 
                                   prox_ranking, 
                                   8, 
                                   blobfeeder(db, map(words,hash) )); 
} 
 
_WhiteFish.ResultSet test_query2(Search.Database.Base db, array(string) words) 
{ 
  array(int) field_ranking=allocate(66); 
  field_ranking[0]=17; 
  field_ranking[2]=47; 
  return _WhiteFish.do_query_phrase(map(words, hash), 
                                    field_ranking, 
                                    blobfeeder(db, map(words,hash) )); 
}