From 54700f81a5e835bd995c66f82ffac262f32a2718 Mon Sep 17 00:00:00 2001 From: Tor Egge Date: Tue, 12 Jul 2022 16:20:12 +0200 Subject: Add DocumentIdDFW, used to print document id without using docsum blob. --- searchsummary/CMakeLists.txt | 1 + .../docsummary/document_id_dfw/CMakeLists.txt | 10 ++ .../document_id_dfw/document_id_dfw_test.cpp | 153 +++++++++++++++++++++ .../vespa/searchsummary/docsummary/CMakeLists.txt | 1 + .../docsummary/docsum_store_document.cpp | 11 ++ .../docsummary/docsum_store_document.h | 1 + .../searchsummary/docsummary/docsumconfig.cpp | 4 + .../searchsummary/docsummary/document_id_dfw.cpp | 31 +++++ .../searchsummary/docsummary/document_id_dfw.h | 22 +++ .../docsummary/i_docsum_store_document.h | 1 + 10 files changed, 235 insertions(+) create mode 100644 searchsummary/src/tests/docsummary/document_id_dfw/CMakeLists.txt create mode 100644 searchsummary/src/tests/docsummary/document_id_dfw/document_id_dfw_test.cpp create mode 100644 searchsummary/src/vespa/searchsummary/docsummary/document_id_dfw.cpp create mode 100644 searchsummary/src/vespa/searchsummary/docsummary/document_id_dfw.h (limited to 'searchsummary') diff --git a/searchsummary/CMakeLists.txt b/searchsummary/CMakeLists.txt index 17b46499317..8c34f1b2016 100644 --- a/searchsummary/CMakeLists.txt +++ b/searchsummary/CMakeLists.txt @@ -19,6 +19,7 @@ vespa_define_module( src/tests/docsummary src/tests/docsummary/attribute_combiner src/tests/docsummary/attributedfw + src/tests/docsummary/document_id_dfw src/tests/docsummary/matched_elements_filter src/tests/docsummary/slime_summary src/tests/docsummary/summary_field_converter diff --git a/searchsummary/src/tests/docsummary/document_id_dfw/CMakeLists.txt b/searchsummary/src/tests/docsummary/document_id_dfw/CMakeLists.txt new file mode 100644 index 00000000000..3591a36c8de --- /dev/null +++ b/searchsummary/src/tests/docsummary/document_id_dfw/CMakeLists.txt @@ -0,0 +1,10 @@ +# Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +vespa_add_executable(searchsummary_document_id_dfw_test_app TEST + SOURCES + document_id_dfw_test.cpp + DEPENDS + searchsummary + searchsummary_test + GTest::GTest +) +vespa_add_test(NAME searchsummary_document_id_dfw_test_app COMMAND searchsummary_document_id_dfw_test_app) diff --git a/searchsummary/src/tests/docsummary/document_id_dfw/document_id_dfw_test.cpp b/searchsummary/src/tests/docsummary/document_id_dfw/document_id_dfw_test.cpp new file mode 100644 index 00000000000..0ea78b722c0 --- /dev/null +++ b/searchsummary/src/tests/docsummary/document_id_dfw/document_id_dfw_test.cpp @@ -0,0 +1,153 @@ +// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using document::Document; +using document::DocumentId; +using document::DocumentType; +using document::DocumentTypeRepo; +using document::config_builder::DocumenttypesConfigBuilderHelper; +using document::config_builder::Struct; +using search::docsummary::DocsumBlobEntryFilter; +using search::docsummary::DocsumStoreDocument; +using search::docsummary::DocsumStoreValue; +using search::docsummary::DocumentIdDFW; +using search::docsummary::IDocsumStoreDocument; +using search::docsummary::ResultClass; +using search::docsummary::ResultConfig; +using search::docsummary::ResultPacker; +using search::docsummary::GeneralResult; +using vespalib::Slime; +using vespalib::slime::Cursor; +using vespalib::slime::ObjectInserter; +using vespalib::slime::SlimeInserter; + +namespace { + +const int32_t doc_type_id = 787121340; +const vespalib::string doc_type_name = "test"; +const vespalib::string header_name = doc_type_name + ".header"; +const vespalib::string body_name = doc_type_name + ".body"; + + +std::unique_ptr +make_doc_type_repo() +{ + DocumenttypesConfigBuilderHelper builder; + builder.document(doc_type_id, doc_type_name, + Struct(header_name), Struct(body_name)); + return std::unique_ptr(new DocumentTypeRepo(builder.config())); +} + +class DocumentIdDFWTest : public ::testing::Test +{ + vespalib::string _field_name; + vespalib::Memory _field_name_view; + DocsumBlobEntryFilter _docsum_blob_entry_filter; + std::unique_ptr _result_config; + std::unique_ptr _packer; // owns docsum blob + std::unique_ptr _repo; + const DocumentType* _document_type; + +protected: + DocumentIdDFWTest(); + ~DocumentIdDFWTest() override; + + std::unique_ptr make_docsum_store_document(const vespalib::string &id); + std::unique_ptr make_docsum_store_value(std::unique_ptr doc); + vespalib::Slime write(const DocsumStoreValue& value); + vespalib::Memory get_field_name_view() const noexcept { return _field_name_view; } +}; + +DocumentIdDFWTest::DocumentIdDFWTest() + : testing::Test(), + _field_name("documentid"), + _field_name_view(_field_name.data(), _field_name.size()), + _docsum_blob_entry_filter(DocsumBlobEntryFilter().add_skip(search::docsummary::RES_LONG_STRING)), + _result_config(std::make_unique(_docsum_blob_entry_filter)), + _packer(std::make_unique(_result_config.get())), + _repo(make_doc_type_repo()), + _document_type(_repo->getDocumentType(doc_type_name)) +{ + auto* cfg = _result_config->AddResultClass("default", 0); + cfg->AddConfigEntry(_field_name.c_str(), search::docsummary::RES_LONG_STRING); + _result_config->CreateEnumMaps(); +} + + +DocumentIdDFWTest::~DocumentIdDFWTest() = default; + + +std::unique_ptr +DocumentIdDFWTest::make_docsum_store_document(const vespalib::string& id) +{ + auto doc = std::make_unique(*_document_type, DocumentId(id)); + doc->setRepo(*_repo); + return std::make_unique(std::move(doc)); +} + +std::unique_ptr +DocumentIdDFWTest::make_docsum_store_value(std::unique_ptr doc) +{ + EXPECT_TRUE(_packer->Init(0)); + const char *ptr = nullptr; + uint32_t len = 0; + EXPECT_TRUE(_packer->GetDocsumBlob(&ptr, &len)); + return std::make_unique(ptr, len, std::move(doc)); +} + +vespalib::Slime +DocumentIdDFWTest::write(const DocsumStoreValue& value) +{ + auto result = std::make_unique(_result_config->LookupResultClass(0)); + EXPECT_TRUE(result->inplaceUnpack(value)); + Slime slime; + SlimeInserter top_inserter(slime); + Cursor & docsum = top_inserter.insertObject(); + ObjectInserter field_inserter(docsum, _field_name_view); + DocumentIdDFW writer; + writer.insertField(0, result.get(), nullptr, search::docsummary::RES_LONG_STRING, field_inserter); + return slime; +} + +TEST_F(DocumentIdDFWTest, insert_document_id) +{ + vespalib::string id("id::test::0"); + auto doc = make_docsum_store_document(id); + auto dsvalue = make_docsum_store_value(std::move(doc)); + auto slime = write(*dsvalue); + EXPECT_TRUE(slime.get()[get_field_name_view()].valid()); + EXPECT_EQ(id, slime.get()[get_field_name_view()].asString().make_string()); +} + +TEST_F(DocumentIdDFWTest, insert_document_id_no_document_doc) +{ + auto dsvalue = make_docsum_store_value(std::make_unique(std::unique_ptr())); + auto slime = write(*dsvalue); + EXPECT_FALSE(slime.get()[get_field_name_view()].valid()); +} + +TEST_F(DocumentIdDFWTest, insert_document_id_no_docsum_store_doc) +{ + auto dsvalue = make_docsum_store_value({}); + auto slime = write(*dsvalue); + EXPECT_FALSE(slime.get()[get_field_name_view()].valid()); +} + +} + +GTEST_MAIN_RUN_ALL_TESTS() diff --git a/searchsummary/src/vespa/searchsummary/docsummary/CMakeLists.txt b/searchsummary/src/vespa/searchsummary/docsummary/CMakeLists.txt index e3272fb36de..e262482cddb 100644 --- a/searchsummary/src/vespa/searchsummary/docsummary/CMakeLists.txt +++ b/searchsummary/src/vespa/searchsummary/docsummary/CMakeLists.txt @@ -13,6 +13,7 @@ vespa_add_library(searchsummary_docsummary OBJECT docsumstate.cpp docsumstorevalue.cpp docsumwriter.cpp + document_id_dfw.cpp dynamicteaserdfw.cpp empty_dfw.cpp general_result.cpp diff --git a/searchsummary/src/vespa/searchsummary/docsummary/docsum_store_document.cpp b/searchsummary/src/vespa/searchsummary/docsummary/docsum_store_document.cpp index e525989e972..e6c8d0b6ab8 100644 --- a/searchsummary/src/vespa/searchsummary/docsummary/docsum_store_document.cpp +++ b/searchsummary/src/vespa/searchsummary/docsummary/docsum_store_document.cpp @@ -5,6 +5,7 @@ #include "summaryfieldconverter.h" #include #include +#include namespace search::docsummary { @@ -39,4 +40,14 @@ DocsumStoreDocument::insert_summary_field(const vespalib::string& field_name, ve } } +void +DocsumStoreDocument::insert_document_id(vespalib::slime::Inserter& inserter) const +{ + if (_document) { + auto id = _document->getId().toString(); + vespalib::Memory id_view(id.data(), id.size()); + inserter.insertString(id_view); + } +} + } diff --git a/searchsummary/src/vespa/searchsummary/docsummary/docsum_store_document.h b/searchsummary/src/vespa/searchsummary/docsummary/docsum_store_document.h index 66a5a74fa8d..3b0bea6e721 100644 --- a/searchsummary/src/vespa/searchsummary/docsummary/docsum_store_document.h +++ b/searchsummary/src/vespa/searchsummary/docsummary/docsum_store_document.h @@ -19,6 +19,7 @@ public: ~DocsumStoreDocument() override; std::unique_ptr get_field_value(const vespalib::string& field_name) const override; void insert_summary_field(const vespalib::string& field_name, vespalib::slime::Inserter& inserter) const override; + void insert_document_id(vespalib::slime::Inserter& inserter) const override; }; } diff --git a/searchsummary/src/vespa/searchsummary/docsummary/docsumconfig.cpp b/searchsummary/src/vespa/searchsummary/docsummary/docsumconfig.cpp index 376d4f90204..fea11923858 100644 --- a/searchsummary/src/vespa/searchsummary/docsummary/docsumconfig.cpp +++ b/searchsummary/src/vespa/searchsummary/docsummary/docsumconfig.cpp @@ -4,6 +4,7 @@ #include "attribute_combiner_dfw.h" #include "copy_dfw.h" #include "docsumwriter.h" +#include "document_id_dfw.h" #include "empty_dfw.h" #include "geoposdfw.h" #include "idocsumenvironment.h" @@ -113,6 +114,9 @@ DynamicDocsumConfig::createFieldWriter(const string & fieldName, const string & *attr_ctx, matching_elems_fields); rc = static_cast(fieldWriter); } + } else if (overrideName == "documentid") { + fieldWriter = std::make_unique(); + rc = true; } else { throw IllegalArgumentException("unknown override operation '" + overrideName + "' for field '" + fieldName + "'."); } diff --git a/searchsummary/src/vespa/searchsummary/docsummary/document_id_dfw.cpp b/searchsummary/src/vespa/searchsummary/docsummary/document_id_dfw.cpp new file mode 100644 index 00000000000..8fcf7c95cdc --- /dev/null +++ b/searchsummary/src/vespa/searchsummary/docsummary/document_id_dfw.cpp @@ -0,0 +1,31 @@ +// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. + +#include "document_id_dfw.h" +#include "general_result.h" +#include "i_docsum_store_document.h" + +namespace search::docsummary { + +DocumentIdDFW::DocumentIdDFW() +{ +} + +DocumentIdDFW::~DocumentIdDFW() = default; + +bool +DocumentIdDFW::IsGenerated() const +{ + return false; +} + +void +DocumentIdDFW::insertField(uint32_t, GeneralResult *gres, GetDocsumsState *, ResType, + vespalib::slime::Inserter &target) +{ + const auto* document = gres->get_document(); + if (document != nullptr) { + document->insert_document_id(target); + } +} + +} diff --git a/searchsummary/src/vespa/searchsummary/docsummary/document_id_dfw.h b/searchsummary/src/vespa/searchsummary/docsummary/document_id_dfw.h new file mode 100644 index 00000000000..f6353912384 --- /dev/null +++ b/searchsummary/src/vespa/searchsummary/docsummary/document_id_dfw.h @@ -0,0 +1,22 @@ +// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. + +#pragma once + +#include "docsum_field_writer.h" + +namespace search::docsummary { + +/* + * Class for writing document id field. + */ +class DocumentIdDFW : public DocsumFieldWriter +{ +private: +public: + DocumentIdDFW(); + ~DocumentIdDFW() override; + bool IsGenerated() const override; + void insertField(uint32_t docid, GeneralResult *gres, GetDocsumsState *state, ResType type, vespalib::slime::Inserter &target) override; +}; + +} diff --git a/searchsummary/src/vespa/searchsummary/docsummary/i_docsum_store_document.h b/searchsummary/src/vespa/searchsummary/docsummary/i_docsum_store_document.h index f81412eb34c..c177568c467 100644 --- a/searchsummary/src/vespa/searchsummary/docsummary/i_docsum_store_document.h +++ b/searchsummary/src/vespa/searchsummary/docsummary/i_docsum_store_document.h @@ -22,6 +22,7 @@ public: virtual ~IDocsumStoreDocument() = default; virtual std::unique_ptr get_field_value(const vespalib::string& field_name) const = 0; virtual void insert_summary_field(const vespalib::string& field_name, vespalib::slime::Inserter& inserter) const = 0; + virtual void insert_document_id(vespalib::slime::Inserter& inserter) const = 0; }; } -- cgit v1.2.3