aboutsummaryrefslogtreecommitdiffstats
path: root/streamingvisitors/src/vespa/vsm/searcher/geo_pos_field_searcher.cpp
blob: bbeb3be986f8a6b06048d764b08d970e2b9db074 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "geo_pos_field_searcher.h"
#include <vespa/document/fieldvalue/arrayfieldvalue.h>
#include <vespa/document/fieldvalue/structfieldvalue.h>
#include <vespa/searchlib/common/geo_location_parser.h>
#include <vespa/vespalib/util/issue.h>
#include <vespa/vespalib/util/exception.h>

#include <vespa/log/log.h>
LOG_SETUP(".vsm.searcher.geo_pos_field_searcher");

using search::streaming::QueryTerm;
using search::streaming::QueryTermList;
using search::common::GeoLocation;
using search::common::GeoLocationParser;

namespace vsm {

std::unique_ptr<FieldSearcher> GeoPosFieldSearcher::duplicate() const {
    return std::make_unique<GeoPosFieldSearcher>(*this);
}

GeoPosFieldSearcher::GeoPosFieldSearcher(FieldIdT fId) :
    FieldSearcher(fId),
    _geoPosTerm()
{}

GeoPosFieldSearcher::~GeoPosFieldSearcher() {}

void GeoPosFieldSearcher::prepare(search::streaming::QueryTermList& qtl,
                                  const SharedSearcherBuf& buf,
                                  const vsm::FieldPathMapT& field_paths,
                                  search::fef::IQueryEnvironment& query_env)
{
    _geoPosTerm.clear();
    FieldSearcher::prepare(qtl, buf, field_paths, query_env);
    for (auto qt : qtl) {
        const vespalib::string & str = qt->getTermString();
        GeoLocationParser parser;
        bool valid = parser.parseNoField(str);
        if (! valid) {
            vespalib::Issue::report("invalid position in term: %s", str.c_str());
        }
        _geoPosTerm.emplace_back(parser.getGeoLocation());
    }
}

void GeoPosFieldSearcher::onValue(const document::FieldValue & fv) {
    LOG(spam, "ignore field value '%s'", fv.toString().c_str());
}

void GeoPosFieldSearcher::onStructValue(const document::StructFieldValue & fv) {
    size_t num_terms = _geoPosTerm.size();
    for (size_t j = 0; j < num_terms; ++j) {
        const GeoPosInfo & gpi = _geoPosTerm[j];
        if (gpi.valid() && gpi.cmp(fv)) {
            addHit(*_qtl[j], 0);
        }
    }
    set_element_length(1);
}

bool GeoPosFieldSearcher::GeoPosInfo::cmp(const document::StructFieldValue & sfv) const {
    try {
        auto xv = sfv.getValue("x");
        auto yv = sfv.getValue("y");
        if (xv && yv) {
            int32_t x = xv->getAsInt();
            int32_t y = yv->getAsInt();
            GeoLocation::Point p{x,y};
            if (inside_limit(p)) {
                return true;
            }
        }
    } catch (const vespalib::Exception &e) {
        vespalib::Issue::report("bad fieldvalue for GeoPosFieldSearcher: %s", e.getMessage().c_str());
    }
    return false;
}

}