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

import com.yahoo.prelude.fastsearch.FastHit;
import com.yahoo.prelude.hitfield.JSONString;
import com.yahoo.search.Query;
import com.yahoo.search.Result;
import com.yahoo.search.Searcher;
import com.yahoo.processing.request.CompoundName;
import com.yahoo.search.result.Hit;
import com.yahoo.search.result.FeatureData;
import com.yahoo.search.result.StructuredData;
import com.yahoo.search.searchchain.Execution;

import java.util.Iterator;

/**
 * Save the query in the incoming state to a meta hit in the result.
 *
 * @author Steinar Knutsen
 */
public class JSONDebugSearcher extends Searcher {

    public static final String JSON_FIELD = "JSON field: ";
    public static final String STRUCT_FIELD = "Structured data field (as json): ";
    public static final String FEATURE_FIELD = "Feature data field (as json): ";

    private static final CompoundName PROPERTYNAME = CompoundName.from("dumpjson");

    @Override
    public Result search(Query query, Execution execution) {
        Result r = execution.search(query);
        String propertyName = query.properties().getString(PROPERTYNAME);
        if (propertyName != null) {
            execution.fill(r);
            for (Iterator<Hit> i = r.hits().deepIterator(); i.hasNext();) {
                Hit h = i.next();
                if (h instanceof FastHit) {
                    FastHit hit = (FastHit) h;
                    Object o = hit.getField(propertyName);
                    if (o instanceof JSONString) {
                        JSONString j = (JSONString) o;
                        r.getQuery().trace(JSON_FIELD + j.getContent(), false, 5);
                    }
                    if (o instanceof StructuredData) {
                        StructuredData d = (StructuredData) o;
                        r.getQuery().trace(STRUCT_FIELD + d.toJson(), false, 5);
                    }
                    if (o instanceof FeatureData) {
                        FeatureData d = (FeatureData) o;
                        r.getQuery().trace(FEATURE_FIELD + d.toJson(), false, 5);
                    }
                }
            }
        }
        return r;
    }

}