aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/main/java/com/yahoo/searchlib/ranking/features/fieldmatch/Query.java
blob: 7c027835ce8c36b1976d998a480ebaed8a158a95 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.searchlib.ranking.features.fieldmatch;

import com.yahoo.searchlib.ranking.features.fieldmatch.QueryTerm;

import java.util.Arrays;

/**
 * A query: An array of the QueryTerms which searches the field we are calculating for,
 * <p>
 * In addition the sum of the term weights of <i>all</i> the query terms can be set
 * explicitly. This allows us to model the matchWeight rank feature of a field as dependent of
 * the weights of all the terms in the query.
 *
 * @author  bratseth
 */
public class Query {

    private QueryTerm[] terms;

    private int totalTermWeight=0;

    private float totalSignificance=0;

    public Query(String query) {
        this(splitQuery(query));
    }

    /** Creates a query with a list of query terms. The query terms are not, and must not be subsequently modified */
    public Query(QueryTerm[] terms) {
        this.terms=terms;

        for (QueryTerm term : terms) {
            totalTermWeight+=term.getWeight();
            totalSignificance+=term.getSignificance();
        }
    }

    private static QueryTerm[] splitQuery(String queryString) {
        String[] queryTerms=queryString.split(" ");
        QueryTerm[] query=new QueryTerm[queryTerms.length];
        for (int i=0; i<query.length; i++)
            query[i]=new QueryTerm(queryTerms[i]);
        return query;
    }

    /** Returns the query terms we are calculating features of */
    public QueryTerm[] getTerms() { return terms; }

    /**
     * Returns the total term weight for this query.
     * This is the sum of the weights of the terms if not set explicitly, or if set explicitly a higher
     * number which also models a query which also has terms going to other indexes.
     */
    public int getTotalTermWeight() { return totalTermWeight; }

    public void setTotalTermWeight(int totalTermWeight) { this.totalTermWeight=totalTermWeight; }

    /**
     * Returns the total term significance for this query.
     * This is the sum of the significance of the terms if not set explicitly, or if set explicitly a higher
     * number which also models a query which also has terms going to other indexes.
     */
    public float getTotalSignificance() { return totalSignificance; }

    public void setTotalSignificance(float totalSignificance) { this.totalSignificance=totalSignificance; }

    public String toString() {
        return "query: " + Arrays.toString(terms);
    }

}