diff options
author | Ian Boros <puppyofkosh@gmail.com> | 2019-05-15 19:05:00 -0400 |
---|---|---|
committer | Ian Boros <puppyofkosh@gmail.com> | 2019-05-20 21:38:07 -0400 |
commit | d7c2ad76b418912c16d4250bdbe74f8a9b3f51ca (patch) | |
tree | 4ff2622d3a38fbf67d73f848e2deaf3c6c5e2cc5 /src/mongo/db/pipeline/document_value_test.cpp | |
parent | cb7eff01f675d854d7484bd68ce298a8817d2af2 (diff) | |
download | mongo-d7c2ad76b418912c16d4250bdbe74f8a9b3f51ca.tar.gz |
SERVER-40555 add searchSnippet metadata
Diffstat (limited to 'src/mongo/db/pipeline/document_value_test.cpp')
-rw-r--r-- | src/mongo/db/pipeline/document_value_test.cpp | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/src/mongo/db/pipeline/document_value_test.cpp b/src/mongo/db/pipeline/document_value_test.cpp index 438a980c10f..e758dadf2a3 100644 --- a/src/mongo/db/pipeline/document_value_test.cpp +++ b/src/mongo/db/pipeline/document_value_test.cpp @@ -523,6 +523,51 @@ TEST(MetaFields, RandValBasics) { ASSERT_EQ(2.0, doc2.getRandMetaField()); } +TEST(MetaFields, SearchScoreBasic) { + // Documents should not have a search score until it is set. + ASSERT_FALSE(Document().hasSearchScore()); + + // Setting the search score field should work as expected. + MutableDocument docBuilder; + docBuilder.setSearchScore(1.23); + Document doc = docBuilder.freeze(); + ASSERT_TRUE(doc.hasSearchScore()); + ASSERT_EQ(1.23, doc.getSearchScore()); + + // Setting the searchScore twice should keep the second value. + MutableDocument docBuilder2; + docBuilder2.setSearchScore(1.0); + docBuilder2.setSearchScore(2.0); + Document doc2 = docBuilder2.freeze(); + ASSERT_TRUE(doc2.hasSearchScore()); + ASSERT_EQ(2.0, doc2.getSearchScore()); +} + +TEST(MetaFields, SearchHighlightsBasic) { + // Documents should not have a search highlights until it is set. + ASSERT_FALSE(Document().hasSearchHighlights()); + + // Setting the search highlights field should work as expected. + MutableDocument docBuilder; + Value highlights = DOC_ARRAY("a"_sd + << "b"_sd); + docBuilder.setSearchHighlights(highlights); + Document doc = docBuilder.freeze(); + ASSERT_TRUE(doc.hasSearchHighlights()); + ASSERT_VALUE_EQ(doc.getSearchHighlights(), highlights); + + // Setting the searchHighlights twice should keep the second value. + MutableDocument docBuilder2; + Value otherHighlights = DOC_ARRAY("snippet1"_sd + << "snippet2"_sd + << "snippet3"_sd); + docBuilder2.setSearchHighlights(highlights); + docBuilder2.setSearchHighlights(otherHighlights); + Document doc2 = docBuilder2.freeze(); + ASSERT_TRUE(doc2.hasSearchHighlights()); + ASSERT_VALUE_EQ(doc2.getSearchHighlights(), otherHighlights); +} + class SerializationTest : public unittest::Test { protected: Document roundTrip(const Document& input) { @@ -538,10 +583,16 @@ protected: ASSERT_DOCUMENT_EQ(output, input); ASSERT_EQ(output.hasTextScore(), input.hasTextScore()); ASSERT_EQ(output.hasRandMetaField(), input.hasRandMetaField()); + ASSERT_EQ(output.hasSearchScore(), input.hasSearchScore()); + ASSERT_EQ(output.hasSearchHighlights(), input.hasSearchHighlights()); if (input.hasTextScore()) ASSERT_EQ(output.getTextScore(), input.getTextScore()); if (input.hasRandMetaField()) ASSERT_EQ(output.getRandMetaField(), input.getRandMetaField()); + if (input.hasSearchScore()) + ASSERT_EQ(output.getSearchScore(), input.getSearchScore()); + if (input.hasSearchHighlights()) + ASSERT_VALUE_EQ(output.getSearchHighlights(), input.getSearchHighlights()); ASSERT(output.toBson().binaryEqual(input.toBson())); } @@ -551,6 +602,9 @@ TEST_F(SerializationTest, MetaSerializationNoVals) { MutableDocument docBuilder; docBuilder.setTextScore(10.0); docBuilder.setRandMetaField(20.0); + docBuilder.setSearchScore(30.0); + docBuilder.setSearchHighlights(DOC_ARRAY("abc"_sd + << "def"_sd)); assertRoundTrips(docBuilder.freeze()); } @@ -559,6 +613,19 @@ TEST_F(SerializationTest, MetaSerializationWithVals) { MutableDocument docBuilder(DOC("foo" << 10)); docBuilder.setTextScore(10.0); docBuilder.setRandMetaField(20.0); + docBuilder.setSearchScore(30.0); + docBuilder.setSearchHighlights(DOC_ARRAY("abc"_sd + << "def"_sd)); + assertRoundTrips(docBuilder.freeze()); +} + +TEST_F(SerializationTest, MetaSerializationSearchHighlightsNonArray) { + MutableDocument docBuilder; + docBuilder.setTextScore(10.0); + docBuilder.setRandMetaField(20.0); + docBuilder.setSearchScore(30.0); + // Everything should still round trip even if the searchHighlights metadata isn't an array. + docBuilder.setSearchHighlights(Value(1.23)); assertRoundTrips(docBuilder.freeze()); } @@ -566,10 +633,17 @@ TEST(MetaFields, ToAndFromBson) { MutableDocument docBuilder; docBuilder.setTextScore(10.0); docBuilder.setRandMetaField(20.0); + docBuilder.setSearchScore(30.0); + docBuilder.setSearchHighlights(DOC_ARRAY("abc"_sd + << "def"_sd)); Document doc = docBuilder.freeze(); BSONObj obj = doc.toBsonWithMetaData(); ASSERT_EQ(10.0, obj[Document::metaFieldTextScore].Double()); ASSERT_EQ(20, obj[Document::metaFieldRandVal].numberLong()); + ASSERT_EQ(30.0, obj[Document::metaFieldSearchScore].Double()); + ASSERT_BSONOBJ_EQ(obj[Document::metaFieldSearchHighlights].embeddedObject(), + BSON_ARRAY("abc"_sd + << "def"_sd)); Document fromBson = Document::fromBsonWithMetaData(obj); ASSERT_TRUE(fromBson.hasTextScore()); ASSERT_TRUE(fromBson.hasRandMetaField()); @@ -577,6 +651,28 @@ TEST(MetaFields, ToAndFromBson) { ASSERT_EQ(20, fromBson.getRandMetaField()); } +TEST(MetaFields, MetaFieldsIncludedInDocumentApproximateSize) { + MutableDocument docBuilder; + docBuilder.setSearchHighlights(DOC_ARRAY("abc"_sd + << "def"_sd)); + const size_t smallMetadataDocSize = docBuilder.freeze().getApproximateSize(); + + // The second document has a larger "search highlights" object. + MutableDocument docBuilder2; + docBuilder2.setSearchHighlights(DOC_ARRAY("abc"_sd + << "def"_sd + << "ghijklmnop"_sd)); + Document doc2 = docBuilder2.freeze(); + const size_t bigMetadataDocSize = doc2.getApproximateSize(); + ASSERT_GT(bigMetadataDocSize, smallMetadataDocSize); + + // Do a sanity check on the amount of space taken by metadata in document 2. + ASSERT_LT(doc2.getMetadataApproximateSize(), 200U); + + Document emptyDoc; + ASSERT_LT(emptyDoc.getMetadataApproximateSize(), 100U); +} + TEST(MetaFields, BadSerialization) { // Write an unrecognized option to the buffer. BufBuilder bb; |