aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/test/java/com/yahoo/search/dispatch/rpc/ProtobufSerializationTest.java
blob: be8f99a4ef41c6ba99b0650ffc10d5cf3ff9e3f0 (plain) (blame)
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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

package com.yahoo.search.dispatch.rpc;

import ai.vespa.searchlib.searchprotocol.protobuf.SearchProtocol;
import com.google.protobuf.ByteString;
import com.yahoo.document.GlobalId;
import com.yahoo.document.idstring.IdString;
import com.yahoo.prelude.fastsearch.FastHit;
import com.yahoo.search.Query;
import com.yahoo.search.dispatch.InvokerResult;
import com.yahoo.search.dispatch.LeanHit;
import com.yahoo.search.query.profile.compiled.CompiledQueryProfileRegistry;
import com.yahoo.search.query.profile.config.QueryProfileXMLReader;
import org.junit.jupiter.api.Test;

import java.util.Collections;
import java.util.List;
import java.util.Set;

import static org.junit.jupiter.api.Assertions.*;

/**
 * @author ollivir
 */
public class ProtobufSerializationTest {

    static final double DELTA = 0.000000000001;

    @Test
    void testQuerySerialization() {
        CompiledQueryProfileRegistry registry = new QueryProfileXMLReader().read("src/test/java/com/yahoo/search/query/profile/config/test/tensortypes").compile();
        Query query = new Query.Builder().setQueryProfile(registry.getComponent("profile1"))
                .setRequest("?query=test&ranking.features.query(tensor_1)=[1.200]")
                .build();

        SearchProtocol.SearchRequest request1 = ProtobufSerialization.convertFromQuery(query, 9, "serverId", 0.5);
        assertEquals(9, request1.getHits());
        assertEquals(0, request1.getRankPropertiesCount());
        assertEquals(0, request1.getTensorRankPropertiesCount());
        assertEquals(0, request1.getFeatureOverridesCount());
        assertEquals(2, request1.getTensorFeatureOverridesCount());
        assertEquals("\"\\001\\001\\003key\\001\\rpre_key1_post?\\360\\000\\000\\000\\000\\000\\000\"",
                contentsOf(request1.getTensorFeatureOverrides(0).getValue()));
        assertEquals("\"\\006\\001\\001\\001x\\001?\\231\\231\\232\"",
                contentsOf(request1.getTensorFeatureOverrides(1).getValue()));
        assertFalse(request1.hasProfiling());

        query.prepare(); // calling prepare() moves "overrides" to "features" - content stays the same
        SearchProtocol.SearchRequest request2 = ProtobufSerialization.convertFromQuery(query, 9, "serverId", 0.5);
        assertEquals(9, request2.getHits());
        assertEquals(0, request2.getRankPropertiesCount());
        assertEquals(2, request2.getTensorRankPropertiesCount());
        assertEquals("\"\\001\\001\\003key\\001\\rpre_key1_post?\\360\\000\\000\\000\\000\\000\\000\"",
                contentsOf(request2.getTensorRankProperties(0).getValue()));
        assertEquals("\"\\006\\001\\001\\001x\\001?\\231\\231\\232\"",
                contentsOf(request2.getTensorRankProperties(1).getValue()));
        assertEquals(0, request2.getFeatureOverridesCount());
        assertEquals(0, request2.getTensorFeatureOverridesCount());
    }

    @Test
    void testDocsumSerialization() {
        Query q = new Query("search/?query=test&hits=10&offset=3");
        var builder = ProtobufSerialization.createDocsumRequestBuilder(q, "server", "summary", Set.of("f1", "f2"),true, 0.5);
        builder.setTimeout(0);
        var hit = new FastHit();
        hit.setGlobalId(new GlobalId(IdString.createIdString("id:ns:type::id")).getRawId());
        var bytes = ProtobufSerialization.serializeDocsumRequest(builder, Collections.singletonList(hit));

        assertEquals(56, bytes.length);
    }

    private String contentsOf(ByteString property) {
        String string = property.toString();
        int contentIndex = string.indexOf("contents=");
        return string.substring(contentIndex + "contents=".length(), string.length() - 1);
    }

    SearchProtocol.SearchReply createSearchReply(int numHits, boolean useSorting) {
        SearchProtocol.SearchReply.Builder reply = SearchProtocol.SearchReply.newBuilder();
        reply.setTotalHitCount(7);

        for (int i = 0; i < numHits; i++) {
            SearchProtocol.Hit.Builder hit = SearchProtocol.Hit.newBuilder();
            byte [] gid = {'a','a','a','a','a','a','a','a','a','a','a', (byte)i};
            hit.setGlobalId(ByteString.copyFrom(gid));
            if (useSorting) {
                gid[0] = 'b';
                hit.setSortData(ByteString.copyFrom(gid));
            } else {
                hit.setRelevance(numHits - i);
            }
            reply.addHits(hit);
        }
        return reply.build();
    }

    @Test
    void testSearchReplyDecodingWithRelevance() {
        Query q = new Query("search/?query=test");
        InvokerResult result = ProtobufSerialization.convertToResult(q, createSearchReply(5, false), null, 1, 2);
        assertEquals(result.getResult().getTotalHitCount(), 7);
        List<LeanHit> hits = result.getLeanHits();
        assertEquals(5, hits.size());
        double expectedRelevance = 5;
        int hitNum = 0;
        for (LeanHit hit : hits) {
            assertEquals('a', hit.getGid()[0]);
            assertEquals(hitNum, hit.getGid()[11]);
            assertEquals(expectedRelevance--, hit.getRelevance(), DELTA);
            assertEquals(1, hit.getPartId());
            assertEquals(2, hit.getDistributionKey());
            assertFalse(hit.hasSortData());
            hitNum++;
        }
    }

    @Test
    void testSearchReplyDecodingWithSortData() {
        Query q = new Query("search/?query=test");
        InvokerResult result = ProtobufSerialization.convertToResult(q, createSearchReply(5, true), null, 1, 2);
        assertEquals(result.getResult().getTotalHitCount(), 7);
        List<LeanHit> hits = result.getLeanHits();
        assertEquals(5, hits.size());
        int hitNum = 0;
        for (LeanHit hit : hits) {
            assertEquals('a', hit.getGid()[0]);
            assertEquals(hitNum, hit.getGid()[11]);
            assertEquals(0.0, hit.getRelevance(), DELTA);
            assertEquals(1, hit.getPartId());
            assertEquals(2, hit.getDistributionKey());
            assertTrue(hit.hasSortData());
            assertEquals('b', hit.getSortData()[0]);
            assertEquals(hitNum, hit.getSortData()[11]);
            hitNum++;
        }
    }

    @Test
    void profiling_parameters_are_serialized_in_search_request() {
        var q = new Query("?query=test&trace.level=1&" +
                "trace.profiling.matching.depth=3&" +
                "trace.profiling.firstPhaseRanking.depth=5&" +
                "trace.profiling.secondPhaseRanking.depth=-7");
        var req = ProtobufSerialization.convertFromQuery(q, 1, "serverId", 0.5);
        assertEquals(3, req.getProfiling().getMatch().getDepth());
        assertEquals(5, req.getProfiling().getFirstPhase().getDepth());
        assertEquals(-7, req.getProfiling().getSecondPhase().getDepth());
    }

    @Test
    void only_set_profiling_parameters_are_serialized_in_search_request() {
        var q = new Query("?query=test&trace.level=1&" +
                "trace.profiling.matching.depth=3");
        var req = ProtobufSerialization.convertFromQuery(q, 1, "serverId", 0.5);
        assertEquals(3, req.getProfiling().getMatch().getDepth());
        assertFalse(req.getProfiling().hasFirstPhase());
        assertFalse(req.getProfiling().hasSecondPhase());
    }

}