aboutsummaryrefslogtreecommitdiff
path: root/icing/query/query-processor.cc
blob: 5e0b696e5855c3521f19408914e73eb2e4a1fd67 (plain)
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
// Copyright (C) 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "icing/query/query-processor.h"

#include <deque>
#include <memory>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#include "icing/text_classifier/lib3/utils/base/statusor.h"
#include "icing/absl_ports/canonical_errors.h"
#include "icing/absl_ports/str_cat.h"
#include "icing/index/index.h"
#include "icing/index/iterator/doc-hit-info-iterator-all-document-id.h"
#include "icing/index/iterator/doc-hit-info-iterator-and.h"
#include "icing/index/iterator/doc-hit-info-iterator-filter.h"
#include "icing/index/iterator/doc-hit-info-iterator-not.h"
#include "icing/index/iterator/doc-hit-info-iterator-or.h"
#include "icing/index/iterator/doc-hit-info-iterator-section-restrict.h"
#include "icing/index/iterator/doc-hit-info-iterator.h"
#include "icing/proto/search.pb.h"
#include "icing/query/advanced_query_parser/abstract-syntax-tree.h"
#include "icing/query/advanced_query_parser/lexer.h"
#include "icing/query/advanced_query_parser/parser.h"
#include "icing/query/advanced_query_parser/query-visitor.h"
#include "icing/query/query-features.h"
#include "icing/query/query-processor.h"
#include "icing/query/query-results.h"
#include "icing/query/query-terms.h"
#include "icing/query/query-utils.h"
#include "icing/schema/schema-store.h"
#include "icing/schema/section.h"
#include "icing/store/document-id.h"
#include "icing/store/document-store.h"
#include "icing/tokenization/language-segmenter.h"
#include "icing/tokenization/raw-query-tokenizer.h"
#include "icing/tokenization/token.h"
#include "icing/tokenization/tokenizer-factory.h"
#include "icing/tokenization/tokenizer.h"
#include "icing/transform/normalizer.h"
#include "icing/util/status-macros.h"

namespace icing {
namespace lib {

namespace {

// State of the current query parser state. This is specific to how the raw
// query is parsed/stored.
struct ParserStateFrame {
  std::vector<std::unique_ptr<DocHitInfoIterator>> and_iterators;
  std::vector<std::unique_ptr<DocHitInfoIterator>> or_iterators;

  // If the last independent token was an OR, then we need to treat the next
  // resulting iterator as part of an or_iterator
  bool saw_or = false;

  // If the last independent token was an exclusion, then we need to treat the
  // next resulting iterator as being excluded.
  bool saw_exclude = false;

  // If the last independent token was a property/section filter, then we need
  // to save the section name so we can create a section filter iterator.
  std::string section_restrict;
};

// Combines any OR and AND iterators together into one iterator.
std::unique_ptr<DocHitInfoIterator> ProcessParserStateFrame(
    ParserStateFrame parser_state_frame,
    const DocumentId last_added_document_id) {
  if (parser_state_frame.and_iterators.empty() &&
      parser_state_frame.or_iterators.empty()) {
    // No terms specified, treat an empty query as retrieving all documents.
    //
    // We don't use the index_.last_added_document_id here because it's possible
    // that documents exist in the DocumentStore, but were not successfully
    // indexed. So to return *all* documents and not just *all indexed*
    // documents, we use the DocumentStore's last_added_document_id
    return std::make_unique<DocHitInfoIteratorAllDocumentId>(
        last_added_document_id);
  }

  if (!parser_state_frame.or_iterators.empty()) {
    // Combine all the ORs first since they have higher priority, then add it to
    // the ANDs.
    parser_state_frame.and_iterators.push_back(
        CreateOrIterator(std::move(parser_state_frame.or_iterators)));
  }
  return CreateAndIterator(std::move(parser_state_frame.and_iterators));
}

}  // namespace

libtextclassifier3::StatusOr<std::unique_ptr<QueryProcessor>>
QueryProcessor::Create(Index* index, const NumericIndex<int64_t>* numeric_index,
                       const LanguageSegmenter* language_segmenter,
                       const Normalizer* normalizer,
                       const DocumentStore* document_store,
                       const SchemaStore* schema_store) {
  ICING_RETURN_ERROR_IF_NULL(index);
  ICING_RETURN_ERROR_IF_NULL(numeric_index);
  ICING_RETURN_ERROR_IF_NULL(language_segmenter);
  ICING_RETURN_ERROR_IF_NULL(normalizer);
  ICING_RETURN_ERROR_IF_NULL(document_store);
  ICING_RETURN_ERROR_IF_NULL(schema_store);

  return std::unique_ptr<QueryProcessor>(
      new QueryProcessor(index, numeric_index, language_segmenter, normalizer,
                         document_store, schema_store));
}

QueryProcessor::QueryProcessor(Index* index,
                               const NumericIndex<int64_t>* numeric_index,
                               const LanguageSegmenter* language_segmenter,
                               const Normalizer* normalizer,
                               const DocumentStore* document_store,
                               const SchemaStore* schema_store)
    : index_(*index),
      numeric_index_(*numeric_index),
      language_segmenter_(*language_segmenter),
      normalizer_(*normalizer),
      document_store_(*document_store),
      schema_store_(*schema_store) {}

libtextclassifier3::StatusOr<QueryResults> QueryProcessor::ParseSearch(
    const SearchSpecProto& search_spec,
    ScoringSpecProto::RankingStrategy::Code ranking_strategy,
    int64_t current_time_ms) {
  if (search_spec.search_type() == SearchSpecProto::SearchType::UNDEFINED) {
    return absl_ports::InvalidArgumentError(absl_ports::StrCat(
        "Search type ",
        SearchSpecProto::SearchType::Code_Name(search_spec.search_type()),
        " is not supported."));
  }
  QueryResults results;
  if (search_spec.search_type() ==
      SearchSpecProto::SearchType::EXPERIMENTAL_ICING_ADVANCED_QUERY) {
    ICING_VLOG(1) << "Using EXPERIMENTAL_ICING_ADVANCED_QUERY parser!";
    ICING_ASSIGN_OR_RETURN(
        results,
        ParseAdvancedQuery(search_spec, ranking_strategy, current_time_ms));
  } else {
    ICING_ASSIGN_OR_RETURN(
        results, ParseRawQuery(search_spec, ranking_strategy, current_time_ms));
  }

  // Check that all new features used in the search have been enabled in the
  // SearchSpec.
  const std::unordered_set<Feature> enabled_features(
      search_spec.enabled_features().begin(),
      search_spec.enabled_features().end());
  for (const Feature feature : results.features_in_use) {
    if (enabled_features.find(feature) == enabled_features.end()) {
      return absl_ports::InvalidArgumentError(
          absl_ports::StrCat("Attempted use of unenabled feature ", feature));
    }
  }

  DocHitInfoIteratorFilter::Options options = GetFilterOptions(search_spec);
  results.root_iterator = std::make_unique<DocHitInfoIteratorFilter>(
      std::move(results.root_iterator), &document_store_, &schema_store_,
      options, current_time_ms);
  return results;
}

libtextclassifier3::StatusOr<QueryResults> QueryProcessor::ParseAdvancedQuery(
    const SearchSpecProto& search_spec,
    ScoringSpecProto::RankingStrategy::Code ranking_strategy,
    int64_t current_time_ms) const {
  QueryResults results;
  Lexer lexer(search_spec.query(), Lexer::Language::QUERY);
  ICING_ASSIGN_OR_RETURN(std::vector<Lexer::LexerToken> lexer_tokens,
                         lexer.ExtractTokens());

  Parser parser = Parser::Create(std::move(lexer_tokens));
  ICING_ASSIGN_OR_RETURN(std::unique_ptr<Node> tree_root,
                         parser.ConsumeQuery());

  if (tree_root == nullptr) {
    results.root_iterator = std::make_unique<DocHitInfoIteratorAllDocumentId>(
        document_store_.last_added_document_id());
    return results;
  }
  ICING_ASSIGN_OR_RETURN(
      std::unique_ptr<Tokenizer> plain_tokenizer,
      tokenizer_factory::CreateIndexingTokenizer(
          StringIndexingConfig::TokenizerType::PLAIN, &language_segmenter_));
  DocHitInfoIteratorFilter::Options options = GetFilterOptions(search_spec);
  bool needs_term_frequency_info =
      ranking_strategy == ScoringSpecProto::RankingStrategy::RELEVANCE_SCORE;
  QueryVisitor query_visitor(&index_, &numeric_index_, &document_store_,
                             &schema_store_, &normalizer_,
                             plain_tokenizer.get(), search_spec.query(),
                             std::move(options), search_spec.term_match_type(),
                             needs_term_frequency_info, current_time_ms);
  tree_root->Accept(&query_visitor);
  return std::move(query_visitor).ConsumeResults();
}

// TODO(cassiewang): Collect query stats to populate the SearchResultsProto
libtextclassifier3::StatusOr<QueryResults> QueryProcessor::ParseRawQuery(
    const SearchSpecProto& search_spec,
    ScoringSpecProto::RankingStrategy::Code ranking_strategy,
    int64_t current_time_ms) {
  DocHitInfoIteratorFilter::Options options = GetFilterOptions(search_spec);

  // Tokenize the incoming raw query
  //
  // TODO(cassiewang): Consider caching/creating a tokenizer factory that will
  // cache the n most recently used tokenizers. So we don't have to recreate
  // this on every new query, if they'll all be raw queries.
  ICING_ASSIGN_OR_RETURN(
      std::unique_ptr<Tokenizer> raw_query_tokenizer,
      tokenizer_factory::CreateQueryTokenizer(tokenizer_factory::RAW_QUERY,
                                              &language_segmenter_));

  ICING_ASSIGN_OR_RETURN(std::vector<Token> tokens,
                         raw_query_tokenizer->TokenizeAll(search_spec.query()));

  std::stack<ParserStateFrame> frames;
  frames.emplace();
  QueryResults results;
  // Process all the tokens
  for (int i = 0; i < tokens.size(); i++) {
    const Token& token = tokens.at(i);
    std::unique_ptr<DocHitInfoIterator> result_iterator;

    // TODO(b/202076890): Handle negation tokens
    switch (token.type) {
      case Token::Type::QUERY_LEFT_PARENTHESES: {
        frames.emplace(ParserStateFrame());
        break;
      }
      case Token::Type::QUERY_RIGHT_PARENTHESES: {
        if (frames.empty()) {
          return absl_ports::InternalError(
              "Encountered empty stack of ParserStateFrames");
        }
        result_iterator = ProcessParserStateFrame(
            std::move(frames.top()), document_store_.last_added_document_id());
        frames.pop();
        break;
      }
      case Token::Type::QUERY_EXCLUSION: {
        if (frames.empty()) {
          return absl_ports::InternalError(
              "Encountered empty stack of ParserStateFrames");
        }
        frames.top().saw_exclude = true;
        break;
      }
      case Token::Type::QUERY_OR: {
        if (frames.empty()) {
          return absl_ports::InternalError(
              "Encountered empty stack of ParserStateFrames");
        }
        frames.top().saw_or = true;
        break;
      }
      case Token::Type::QUERY_PROPERTY: {
        if (frames.empty()) {
          return absl_ports::InternalError(
              "Encountered empty stack of ParserStateFrames");
        }

        frames.top().section_restrict = std::string(token.text);
        break;
      }
      case Token::Type::REGULAR: {
        if (frames.empty()) {
          return absl_ports::InternalError(
              "Encountered empty stack of ParserStateFrames");
        }

        std::string normalized_text = normalizer_.NormalizeTerm(token.text);

        // TODO(cassiewang): Consider removing the use of a section mask in the
        // term iterator, or constructing a best-effort SectionIdMask based on
        // the section filter. For some combination of schema type filters and
        // section filters, we can't encapsulate the perfect
        // SchemaTypeId-SectionId sets with just a SectionIdMask. So we
        // over-retrieve hits and have to do a post-filter anyways. With a
        // SectionIdMask, we might be able to narrow down our SectionIds, but
        // we'll still over-retrieve hits a bit. So at that point, it's a
        // tradeoff of
        //
        //   1.1 Go to SchemaStore and iterate over the schema to calculate a
        //   SectionIdMask
        //   1.2 Use SectionIdMask and save some hit buffer memory
        //   1.3 Do a post-filter to double check SchemaTypeId-SectionId combo
        //
        // vs
        //
        //   2.1 Use SectionIdMaskAll and use more hit buffer memory
        //   2.2 Do a post-filter to double check SchemaTypeId-SectionId combo
        //
        // We do the same amount of disk reads, so it may be dependent on how
        // big the schema is and/or how popular schema type filtering and
        // section filtering is.
        ICING_ASSIGN_OR_RETURN(
            result_iterator,
            index_.GetIterator(
                normalized_text,
                token.text.data() - search_spec.query().c_str(),
                token.text.length(), kSectionIdMaskAll,
                search_spec.term_match_type(),
                /*need_hit_term_frequency=*/ranking_strategy ==
                    ScoringSpecProto::RankingStrategy::RELEVANCE_SCORE));

        // Add term iterator and terms to match if this is not a negation term.
        // WARNING: setting query terms at this point is not compatible with
        // group-level excludes, group-level sections restricts or excluded
        // section restricts. Those are not currently supported. If they became
        // supported, this handling for query terms would need to be altered.
        if (!frames.top().saw_exclude) {
          if (ranking_strategy ==
              ScoringSpecProto::RankingStrategy::RELEVANCE_SCORE) {
            ICING_ASSIGN_OR_RETURN(
                std::unique_ptr<DocHitInfoIterator> term_iterator,
                index_.GetIterator(
                    normalized_text,
                    token.text.data() - search_spec.query().c_str(),
                    token.text.length(), kSectionIdMaskAll,
                    search_spec.term_match_type(),
                    /*need_hit_term_frequency=*/ranking_strategy ==
                        ScoringSpecProto::RankingStrategy::RELEVANCE_SCORE));
            results.query_term_iterators[normalized_text] =
                std::make_unique<DocHitInfoIteratorFilter>(
                    std::move(term_iterator), &document_store_, &schema_store_,
                    options, current_time_ms);
          }
          results.query_terms[frames.top().section_restrict].insert(
              std::move(normalized_text));
        }
        break;
      }
      case Token::Type::INVALID:
        [[fallthrough]];
      default:
        // This wouldn't happen if tokenizer and query processor both work
        // correctly. An unknown token indicates inconsistency between tokenizer
        // and query processor, so we return an internal error here.
        return absl_ports::InternalError(absl_ports::StrCat(
            "Encountered unknown token while processing query: ", token.text));
    }

    // Did we get an iterator out of this token?
    if (result_iterator) {
      if (frames.empty()) {
        return absl_ports::InternalError(
            "Encountered empty stack of ParserStateFrames");
      }

      // NOTE: Order matters!! We must apply the section restrict first, then
      // the NOT operator.
      //
      // Imagine a query like [-subject:foo] which means we
      // want to get documents that don't have the term 'foo' in section
      // 'subject'.
      //
      // Assume some Document_0:
      //   { "subject": "foo" }
      //
      // And assume some Document_1:
      //   { "subject": "bar" }
      //
      // If we use the IteratorNot first, then we'll get DocHitInfos that
      // represent DocumentIds without any section hits like
      // DocHitInfo(document_id_1, kSectionIdMaskNone). Then, when we try to
      // apply the IteratorSectionRestrict, no SectionIds in the mask will match
      // the SectionId of 'subject' and we won't return any results.
      //
      // If we use the IteratorSectionRestrict first, then we'll get a
      // DocHitInfo for Document_0. Then with the IteratorNot, we can get the
      // rest of the Documents excluding Document_0, and get Document_1 as a
      // correct result.
      //
      // TODO(cassiewang): The point is a bit moot right now since we don't even
      // support this functionality. But add tests for this once we do support
      // more advanced section restricts with grouping, negation, etc.
      if (!frames.top().section_restrict.empty()) {
        // We saw a section restrict earlier, wrap the result iterator in
        // the section restrict
        std::set<std::string> section_restricts;
        section_restricts.insert(std::move(frames.top().section_restrict));
        result_iterator = std::make_unique<DocHitInfoIteratorSectionRestrict>(
            std::move(result_iterator), &document_store_, &schema_store_,
            std::move(section_restricts), current_time_ms);

        frames.top().section_restrict = "";
      }

      // Check if we need to NOT/exclude this iterator
      if (frames.top().saw_exclude) {
        result_iterator = std::make_unique<DocHitInfoIteratorNot>(
            std::move(result_iterator),
            document_store_.last_added_document_id());
        frames.top().saw_exclude = false;
      }

      if (i < tokens.size() - 1 &&
          tokens.at(i + 1).type == Token::Type::QUERY_OR) {
        // This isn't the last token, and the next token is an OR. Then we
        // should OR this iterator with the next iterator, (e.g. if the query
        // was "A OR B", we would be processing "A" right now)
        frames.top().or_iterators.push_back(std::move(result_iterator));
      } else if (frames.top().saw_or) {
        // This isn't the first token, and the previous token was an OR. Then
        // we should OR this iterator with the previous iterator (e.g. if the
        // query was "A OR (B C)", we would be processing the iterator for "(B
        // C)" right now)
        frames.top().or_iterators.push_back(std::move(result_iterator));
        frames.top().saw_or = false;
      } else {
        // If we're not trying to OR this iterator, we AND everything else.
        if (!frames.top().or_iterators.empty()) {
          // Accumulate the previous OR iterators if there were any.
          frames.top().and_iterators.push_back(
              CreateOrIterator(std::move(frames.top().or_iterators)));
          frames.top().or_iterators =
              std::vector<std::unique_ptr<DocHitInfoIterator>>();
        }
        frames.top().and_iterators.push_back(std::move(result_iterator));
      }
    }
  }

  // Guaranteed that we have some iterators to return. Need to do one last
  // combining since we could have ORs and ANDs.
  if (frames.size() != 1) {
    return absl_ports::InternalError(
        "Encountered invalid state of ParserStateFrames stack");
  }
  results.root_iterator = ProcessParserStateFrame(
      std::move(frames.top()), document_store_.last_added_document_id());
  return results;
}

}  // namespace lib
}  // namespace icing