aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/prelude/searcher/JuniperSearcher.java
blob: ca87c0c1d468a8a963181076539d383530caeff5 (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
// 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 java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

import com.google.inject.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.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 = "...";

    // The name of the field containing document type
    private static final String MAGIC_FIELD = Hit.SDDOCNAME_FIELD;

    public static final String JUNIPER_TAG_REPLACING = "JuniperTagReplacing";

    private String boldOpenTag;
    private String boldCloseTag;
    private 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) {
        Result workResult = result;
        final int worstCase = workResult.getHitCount();
        final List<Hit> hits = new ArrayList<>(worstCase);
        for (final Iterator<Hit> i = workResult.hits().deepIterator(); i.hasNext();) {
            final Hit sniffHit = i.next();
            if ( ! (sniffHit instanceof FastHit)) continue;

            final FastHit hit = (FastHit) sniffHit;
            if (hit.isFilled(summaryClass)) continue;

            hits.add(hit);
        }
        execution.fill(workResult, summaryClass);
        highlight(workResult.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 sniffHit = hitsToHighlight.next();
            if ( ! (sniffHit instanceof FastHit)) continue;

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

            Object searchDefinitionField = hit.getField(MAGIC_FIELD);
            if (searchDefinitionField == null) continue;
            String searchDefinitionName = searchDefinitionField.toString();

            // TODO: Switch to iterate over indexes in the outer loop:
            //for (Index index : indexFacts.getIndexes(searchDefinitionName())) {
            //    if (index.getDynamicSummary() || index.getHighlightSummary()) {
            //        insertTags(hit.buildHitField(index.getName(), true, true), bolding, index.getDynamicSummary());
            //    }
            //}
            for (String fieldName : hit.fields().keySet()) {
                Index index = indexFacts.getIndex(fieldName, searchDefinitionName);
                if (index.getDynamicSummary() || index.getHighlightSummary())
                    insertTags(hit.buildHitField(fieldName, true, true), bolding, index.getDynamicSummary());
            }
        }
    }

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

            final String toQuote = f.getContent();
            List<FieldPart> newFieldParts = null;
            int previous = 0;
            for (int j = 0; j < toQuote.length(); j++) {
                final 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 (final Iterator<FieldPart> j = newFieldParts.iterator(); j.hasNext();) {
                    i.add(j.next());
                }
            }
        }
    }

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

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

    private void addBolding(final boolean bolding,
            final boolean insideHighlight, final FieldPart f,
            final String toQuote, final List<FieldPart> newFieldParts,
            final int previous, final 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;
    }

}