aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/prelude/searcher/JuniperSearcher.java
blob: 4fc3f7919cfab5864b6f74020c2f8db10c1b5c12 (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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.prelude.searcher;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

import com.yahoo.component.annotation.Inject;
import com.yahoo.component.ComponentId;
import com.yahoo.component.chain.dependencies.After;
import com.yahoo.component.chain.dependencies.Before;
import com.yahoo.component.chain.dependencies.Provides;
import com.yahoo.data.access.ArrayTraverser;
import com.yahoo.data.access.Inspectable;
import com.yahoo.data.access.Inspector;
import com.yahoo.data.access.Type;
import com.yahoo.data.access.simple.Value;
import com.yahoo.prelude.Index;
import com.yahoo.prelude.IndexFacts;
import com.yahoo.search.Searcher;
import com.yahoo.container.QrSearchersConfig;
import com.yahoo.prelude.fastsearch.FastHit;
import com.yahoo.prelude.hitfield.BoldCloseFieldPart;
import com.yahoo.prelude.hitfield.BoldOpenFieldPart;
import com.yahoo.prelude.hitfield.FieldPart;
import com.yahoo.prelude.hitfield.HitField;
import com.yahoo.prelude.hitfield.SeparatorFieldPart;
import com.yahoo.prelude.hitfield.StringFieldPart;
import com.yahoo.search.Query;
import com.yahoo.search.Result;
import com.yahoo.search.result.Hit;
import com.yahoo.search.searchchain.Execution;
import com.yahoo.search.searchchain.PhaseNames;

/**
 * Converts juniper highlighting to XML style
 * <p>
 * Note: This searcher only converts backend binary highlighting and separators
 * to the configured highlighting and separator tags.
 *
 * @author Steinar Knutsen
 */
@After(PhaseNames.RAW_QUERY)
@Before(PhaseNames.TRANSFORMED_QUERY)
@Provides(JuniperSearcher.JUNIPER_TAG_REPLACING)
public class JuniperSearcher extends Searcher {

    public final static char RAW_HIGHLIGHT_CHAR = '\u001F';
    public final static char RAW_SEPARATOR_CHAR = '\u001E';

    private static final String ELLIPSIS = "...";

    public static final String JUNIPER_TAG_REPLACING = "JuniperTagReplacing";

    private final String boldOpenTag;
    private final String boldCloseTag;
    private final String separatorTag;

    @Inject
    public JuniperSearcher(ComponentId id, QrSearchersConfig config) {
        super(id);

        boldOpenTag = config.tag().bold().open();
        boldCloseTag = config.tag().bold().close();
        separatorTag = config.tag().separator();
    }

    /**
     * Convert Juniper style property highlighting to XML style.
     */
    @Override
    public Result search(Query query, Execution execution) {
        Result result = execution.search(query);
        highlight(query.getPresentation().getBolding(), result.hits().deepIterator(), null,
                  execution.context().getIndexFacts().newSession(query));
        return result;
    }

    @Override
    public void fill(Result result, String summaryClass, Execution execution) {
        int worstCase = result.getHitCount();
        List<Hit> hits = new ArrayList<>(worstCase);
        for (Iterator<Hit> i = result.hits().deepIterator(); i.hasNext();) {
            Hit hit = i.next();
            if ( ! (hit instanceof FastHit)) continue;

            FastHit fastHit = (FastHit)hit;
            if (fastHit.isFilled(summaryClass)) continue;

            hits.add(fastHit);
        }
        execution.fill(result, summaryClass);
        highlight(result.getQuery().getPresentation().getBolding(), hits.iterator(), summaryClass,
                  execution.context().getIndexFacts().newSession(result.getQuery()));
    }

    private void highlight(boolean bolding, Iterator<Hit> hitsToHighlight,
                           String summaryClass, IndexFacts.Session indexFacts) {
        while (hitsToHighlight.hasNext()) {
            Hit hit = hitsToHighlight.next();
            if ( ! (hit instanceof FastHit)) continue;

            FastHit fastHit = (FastHit) hit;
            if (summaryClass != null &&  ! fastHit.isFilled(summaryClass)) continue;

            Object searchDefinitionField = fastHit.getField(Hit.SDDOCNAME_FIELD);
            if (searchDefinitionField == null) continue;

            for (Index index : indexFacts.getIndexes(searchDefinitionField.toString())) {
                if (index.getDynamicSummary() || index.getHighlightSummary()) {
                    var field = fastHit.getField(index.getName());
                    if (StringArrayConverter.shouldHandleField(field)) {
                        new StringArrayConverter(fastHit, index, field, bolding);
                    } else {
                        HitField fieldValue = fastHit.buildHitField(index.getName(), true);
                        if (fieldValue != null) {
                            insertTags(fieldValue, bolding, index.getDynamicSummary());
                        }
                    }
                }
            }
        }
    }

    private class StringArrayConverter implements ArrayTraverser {

        private Index index;
        private boolean bolding;
        private Value.ArrayValue convertedField = new Value.ArrayValue();

        /**
         * This converts the backend binary highlighting of each item in an array of string field,
         * and creates a new field that replaces the original.
         */
        StringArrayConverter(FastHit hit, Index index, Object field, boolean bolding) {
            this.index = index;
            this.bolding = bolding;
            ((Inspectable)field).inspect().traverse(this);
            hit.setField(index.getName(), convertedField);
        }

        static boolean shouldHandleField(Object field) {
            return (field instanceof Inspectable) &&
                    (((Inspectable)field).inspect().type() == Type.ARRAY);
        }

        @Override
        public void entry(int idx, Inspector inspector) {
            // This is how HitField is instantiated in Hit.buildHitField() when forceNoPreTokenize=true.
            var hitField = new HitField(index.getName(), inspector.asString(), false);
            insertTags(hitField, bolding, index.getDynamicSummary());
            convertedField.add(hitField.getContent());
        }
    }

    private void insertTags(HitField field, boolean bolding, boolean dynteaser) {
        boolean insideHighlight = false;
        for (ListIterator<FieldPart> i = field.listIterator(); i.hasNext();) {
            FieldPart f = i.next();
            if (f instanceof SeparatorFieldPart)
                setSeparatorString(bolding, (SeparatorFieldPart) f);
            if (f.isFinal()) continue;

            String toQuote = f.getContent();
            List<FieldPart> newFieldParts = null;
            int previous = 0;
            for (int j = 0; j < toQuote.length(); j++) {
                char key = toQuote.charAt(j);
                switch (key) {
                    case RAW_HIGHLIGHT_CHAR:
                        newFieldParts = initFieldParts(newFieldParts);
                        addBolding(bolding, insideHighlight, f, toQuote, newFieldParts, previous, j);
                        previous = j + 1;
                        insideHighlight = !insideHighlight;
                        break;
                    case RAW_SEPARATOR_CHAR:
                        newFieldParts = initFieldParts(newFieldParts);
                        addSeparator(bolding, dynteaser, f, toQuote, newFieldParts, previous, j);
                        previous = j + 1;
                        break;
                    default:
                        // no action
                        break;
                }
            }
            if (previous > 0 && previous < toQuote.length()) {
                newFieldParts.add(new StringFieldPart(toQuote.substring(previous), f.isToken()));
            }
            if (newFieldParts != null) {
                i.remove();
                for (Iterator<FieldPart> j = newFieldParts.iterator(); j.hasNext();) {
                    i.add(j.next());
                }
            }
        }
    }

    private void setSeparatorString(boolean bolding, SeparatorFieldPart f) {
        if (bolding)
            f.setContent(separatorTag);
        else
            f.setContent(ELLIPSIS);
    }

    private void addSeparator(boolean bolding, boolean dynteaser, FieldPart f, String toQuote,
                              List<FieldPart> newFieldParts, int previous, int j) {
        if (previous != j)
            newFieldParts.add(new StringFieldPart(toQuote.substring(previous, j), f.isToken()));
        if (dynteaser)
            newFieldParts.add(bolding ? new SeparatorFieldPart(separatorTag) : new SeparatorFieldPart(ELLIPSIS));
    }

    private void addBolding(boolean bolding, boolean insideHighlight, FieldPart f, String toQuote,
                            List<FieldPart> newFieldParts, int previous, int j) {
        if (previous != j) {
            newFieldParts.add(new StringFieldPart(toQuote.substring(previous, j), f.isToken()));
        }
        if (bolding) {
            if (insideHighlight) {
                newFieldParts.add(new BoldCloseFieldPart(boldCloseTag));
            } else {
                if (newFieldParts.size() > 0
                        && newFieldParts.get(newFieldParts.size() - 1) instanceof BoldCloseFieldPart) {
                    newFieldParts.remove(newFieldParts.size() - 1);
                } else {
                    newFieldParts.add(new BoldOpenFieldPart(boldOpenTag));
                }
            }
        }
    }

    private List<FieldPart> initFieldParts(List<FieldPart> newFieldParts) {
        if (newFieldParts == null)
            newFieldParts = new ArrayList<>();
        return newFieldParts;
    }

}