aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/search/querytransform/DefaultPositionSearcher.java
blob: 0523ca1a7b6df84cd599ed32f1262a3e0c117c01 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.querytransform;

import static com.yahoo.prelude.searcher.PosSearcher.POSITION_PARSING;

import com.yahoo.component.chain.dependencies.After;
import com.yahoo.component.chain.dependencies.Before;
import com.yahoo.document.config.DocumentmanagerConfig;
import com.yahoo.prelude.IndexFacts;
import com.yahoo.prelude.Location;
import com.yahoo.prelude.query.GeoLocationItem;
import com.yahoo.search.Query;
import com.yahoo.search.Searcher;
import com.yahoo.search.searchchain.Execution;
import com.yahoo.search.searchchain.PhaseNames;

import com.yahoo.component.annotation.Inject;
import java.util.List;

/**
 * If position attribute has not been set, it will be set here.
 *
 * @author baldersheim
 */
@After({PhaseNames.RAW_QUERY, POSITION_PARSING})
@Before(PhaseNames.TRANSFORMED_QUERY)
public class DefaultPositionSearcher extends Searcher {

    private final boolean useV8GeoPositions;

    @Inject
    public DefaultPositionSearcher(DocumentmanagerConfig cfg) {
        this.useV8GeoPositions = cfg.usev8geopositions();
    }

    DefaultPositionSearcher() {
        this.useV8GeoPositions = false;
    }

    @Override
    public com.yahoo.search.Result search(Query query, Execution execution) {
        Location location = query.getRanking().getLocation();
        if (location != null && (location.getAttribute() == null)) {
            IndexFacts facts = execution.context().getIndexFacts();
            List<String> search = facts.newSession(query.getModel().getSources(), query.getModel().getRestrict()).documentTypes();

            for (String sd : search) {
                String defaultPosition = facts.getDefaultPosition(sd);
                if (defaultPosition != null) {
                    location.setAttribute(defaultPosition);
                }
            }
            if (location.getAttribute() == null) {
                location.setAttribute(facts.getDefaultPosition(null));
            }
        }
        if (useV8GeoPositions && (location != null) && (location.getAttribute() != null)) {
            var geoLoc = new GeoLocationItem(location);
            if (location.isGeoCircle() && location.degRadius() < 0) {
                query.getModel().getQueryTree().withRank(geoLoc);
            } else {
                query.getModel().getQueryTree().and(geoLoc);
            }
            location = null;
            query.getRanking().setLocation(location);
        }
        return execution.search(query);
    }

}