1 // Copyright (C) 2019 Google LLC 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #ifndef ICING_SNIPPET_RETRIEVER_H_ 16 #define ICING_SNIPPET_RETRIEVER_H_ 17 18 #include <memory> 19 20 #include "icing/text_classifier/lib3/utils/base/statusor.h" 21 #include "icing/proto/document.pb.h" 22 #include "icing/proto/search.pb.h" 23 #include "icing/proto/term.pb.h" 24 #include "icing/query/query-terms.h" 25 #include "icing/schema/schema-store.h" 26 #include "icing/schema/section.h" 27 #include "icing/tokenization/language-segmenter.h" 28 #include "icing/transform/normalizer.h" 29 30 namespace icing { 31 namespace lib { 32 33 // This class provides functions to retrieve snippets from documents. Snippets 34 // are retrieved anywhere that content in the document matches query_terms 35 // according to match_type. The behavior of snippet population is determined by 36 // the SnippetSpecProto. 37 // 38 // This class does not take ownership of any of the provided pointers. The only 39 // constraint for the lifecycle of this class is that it must be shorter than 40 // that of the provided pointers. 41 class SnippetRetriever { 42 public: 43 // Factory function to create a SnippetRetriever which does not take ownership 44 // of any input components, and all pointers must refer to valid objects that 45 // outlive the created SnippetRetriever instance. 46 // 47 // Returns: 48 // A SnippetRetriever on success 49 // FAILED_PRECONDITION on any null pointer input 50 static libtextclassifier3::StatusOr<std::unique_ptr<SnippetRetriever>> Create( 51 const SchemaStore* schema_store, 52 const LanguageSegmenter* language_segmenter, 53 const Normalizer* normalizer); 54 55 // Retrieve the snippet information for content in document. terms in 56 // query_terms are matched to content in document according to match_type. 57 // Only sections identified in section_id_mask are considered. 58 // 59 // Returns an empty SnippetProto if no snippets were found. 60 SnippetProto RetrieveSnippet( 61 const SectionRestrictQueryTermsMap& query_terms, 62 TermMatchType::Code match_type, 63 const ResultSpecProto::SnippetSpecProto& snippet_spec, 64 const DocumentProto& document, SectionIdMask section_id_mask) const; 65 66 private: SnippetRetriever(const SchemaStore * schema_store,const LanguageSegmenter * language_segmenter,const Normalizer * normalizer)67 explicit SnippetRetriever(const SchemaStore* schema_store, 68 const LanguageSegmenter* language_segmenter, 69 const Normalizer* normalizer) 70 : schema_store_(*schema_store), 71 language_segmenter_(*language_segmenter), 72 normalizer_(*normalizer) {} 73 74 const SchemaStore& schema_store_; 75 const LanguageSegmenter& language_segmenter_; 76 const Normalizer& normalizer_; 77 }; 78 79 } // namespace lib 80 } // namespace icing 81 82 #endif // ICING_SNIPPET_RETRIEVER_H_ 83