aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/prelude/searcher/DocumentSourceSearcher.java
blob: bc3c6665ae2c848afc09555396544d461285c911 (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
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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.prelude.searcher;

import com.yahoo.search.Query;
import com.yahoo.search.Result;
import com.yahoo.search.Searcher;
import com.yahoo.search.result.Hit;
import com.yahoo.search.searchchain.Execution;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;


/**
 * <p>Implements a document source.  You pass in a query and a Result
 * set.  When this Searcher is called with that query it will return
 * that result set.</p>
 *
 * <p>This supports multi-phase search.</p>
 *
 * <p>To avoid having to add type information for the fields, a quck hack is used to
 * support testing of attribute prefetching.
 * Any field in the configured hits which has a name starting by attribute
 * will be returned when attribute prefetch filling is requested.</p>
 *
 * @author  bratseth
 * @deprecated use {@link com.yahoo.search.searchchain.testutil.DocumentSourceSearcher}
 */
@SuppressWarnings({"rawtypes"})
// TODO: Remove on Vespa 7
@Deprecated // OK
public class DocumentSourceSearcher extends Searcher {

    // as for the SuppressWarnings annotation above, we are inside
    // com.yahoo.prelude, this is old stuff, really no point firing off those
    // warnings here...

    private Result defaultFilledResult;
    private Map<Query, Result> completelyFilledResults = new HashMap<>();
    private Map<Query, Result> attributeFilledResults = new HashMap<>();
    private Map<Query, Result> unFilledResults = new HashMap<>();

    /** Time (in ms) at which the index of this searcher was last modified */
    long editionTimeStamp=0;

    private int queryCount;

    public DocumentSourceSearcher() {
        addDefaultResults();
    }

    /**
     * Adds a result which can be returned either as empty,
     * filled or attribute only filled later.
     * Summary fields starting by "a" are attributes, others are not.
     *
     * @return true when replacing an existing &lt;query, result&gt; pair.
     */
    public boolean addResultSet(Query query, Result fullResult) {
        Result emptyResult = new Result(query.clone());
        Result attributeResult = new Result(query.clone());
        emptyResult.setTotalHitCount(fullResult.getTotalHitCount());
        attributeResult.setTotalHitCount(fullResult.getTotalHitCount());
        int counter=0;
        for (Iterator i = fullResult.hits().deepIterator();i.hasNext();) {
            Hit fullHit = (Hit)i.next();

            Hit emptyHit = fullHit.clone();
            emptyHit.clearFields();
            emptyHit.setFillable();
            emptyHit.setRelevance(fullHit.getRelevance());

            Hit attributeHit = fullHit.clone();
            removePropertiesNotStartingByA(attributeHit);
            attributeHit.setFillable();
            attributeHit.setRelevance(fullHit.getRelevance());
            for (Object propertyKeyObject : fullHit.fields().keySet()) {
                String propertyKey=propertyKeyObject.toString();
                if (propertyKey.startsWith("attribute"))
                    attributeHit.setField(propertyKey, fullHit.getField(propertyKey));
            }
            if (fullHit.getField(Hit.SDDOCNAME_FIELD)!=null)
                attributeHit.setField(Hit.SDDOCNAME_FIELD, fullHit.getField(Hit.SDDOCNAME_FIELD));

            // A simple summary lookup mechanism, similar to FastSearch's
            emptyHit.setField("summaryid", String.valueOf(counter));
            attributeHit.setField("summaryid", String.valueOf(counter));
            fullHit.setField("summaryid", String.valueOf(counter));

            counter++;
            emptyResult.hits().add(emptyHit);
            attributeResult.hits().add(attributeHit);
        }
        unFilledResults.put(getQueryKeyClone(query), emptyResult);
        attributeFilledResults.put(getQueryKeyClone(query), attributeResult);
        if (completelyFilledResults.put(getQueryKeyClone(query), fullResult.clone()) != null) {
            setEditionTimeStamp(System.currentTimeMillis());
            return true;
        }
        return false;
    }

    /**
     * Returns a query clone which has source, offset and hits set to null. This is used by access to
     * the maps using the query as key to achieve lookup independent of offset/hits value
     */
    private Query getQueryKeyClone(Query query) {
        Query key = query.clone();
        key.setWindow(0,0);
        key.getModel().setSources("");
        return key;
    }

    private void removePropertiesNotStartingByA(Hit hit) {
        List<String> toRemove=new java.util.ArrayList<>();
        for (Iterator i= ((Set) hit.fields().keySet()).iterator(); i.hasNext(); ) {
            String key=(String)i.next();
            if (!key.startsWith("a"))
                toRemove.add(key);
        }
        for (Iterator<String> i=toRemove.iterator(); i.hasNext(); ) {
            String propertyName=i.next();
            hit.removeField(propertyName);
        }
    }

    private void addDefaultResults() {
        Query q = new Query("?query=default");
        Result r = new Result(q);
        r.hits().add(new Hit("http://default-1.html"));
        r.hits().add(new Hit("http://default-2.html"));
        r.hits().add(new Hit("http://default-3.html"));
        r.hits().add(new Hit("http://default-4.html"));
        defaultFilledResult = r;
        addResultSet(q, r);
    }

    public long getEditionTimeStamp(){
        long myEditionTime;
        synchronized(this){
            myEditionTime=this.editionTimeStamp;
        }
        return myEditionTime;
    }

    public void setEditionTimeStamp(long editionTime) {
        synchronized(this){
            this.editionTimeStamp=editionTime;
        }
    }

    public Result search(com.yahoo.search.Query query, Execution execution)  {
        queryCount++;
        Result r;
        r = unFilledResults.get(getQueryKeyClone(query));
        if (r == null) {
            r = defaultFilledResult.clone();
        } else {
            r = r.clone();
        }
        r.setQuery(query);
        r.hits().trim(query.getOffset(), query.getHits());
        return r;
    }

    @Override
    public void fill(com.yahoo.search.Result result, String summaryClass, Execution execution) {
        Result filledResult;
        if ("attributeprefetch".equals(summaryClass))
            filledResult=attributeFilledResults.get(getQueryKeyClone(result.getQuery()));
        else
            filledResult = completelyFilledResults.get(getQueryKeyClone(result.getQuery()));

        if (filledResult == null) {
            filledResult = defaultFilledResult;
        }
        fillHits(filledResult,result,summaryClass);
    }

    private void fillHits(Result source,Result target,String summaryClass) {
        for (Iterator hitsToFill= target.hits().deepIterator() ; hitsToFill.hasNext();) {
            Hit hitToFill = (Hit) hitsToFill.next();
            String summaryId= (String) hitToFill.getField("summaryid");
            if (summaryId==null) continue; // Can not fill this
            Hit filledHit = lookupBySummaryId(source,summaryId);
            if (filledHit==null)
                throw new RuntimeException("Can't fill hit with summaryid '" + summaryId + "', not present");

            for (Iterator props= filledHit.fieldIterator();props.hasNext();) {
                Map.Entry propertyEntry = (Map.Entry)props.next();
                hitToFill.setField(propertyEntry.getKey().toString(),
                                   propertyEntry.getValue());
            }
            hitToFill.setFilled(summaryClass);
        }
        target.analyzeHits();
    }

    private Hit lookupBySummaryId(Result result,String summaryId) {
        for (Iterator i= result.hits().deepIterator(); i.hasNext(); ) {
            Hit hit=(Hit)i.next();
            if (summaryId.equals(hit.getField("summaryid"))) {
                return hit;
            }
        }
        return null;
    }

    /**
     * Returns the number of queries made to this searcher since the last
     * reset. For testing - not reliable if multiple threads makes
     * queries simultaneously
     */
    public int getQueryCount() {
        return queryCount;
    }

    public void resetQueryCount() {
        queryCount=0;
    }

}