aboutsummaryrefslogtreecommitdiffstats
path: root/client/src/main/java/ai/vespa/client/dsl/Sources.java
blob: a92cd736396718dedb0ac51c6c80b8878922e840 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package ai.vespa.client.dsl;


import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Sources {

    final Select select;
    private final List<String> targetDocTypes;

    Sources(Select select, List<String> searchDefinitions) {
        this.select = select;
        this.targetDocTypes = new ArrayList<>(searchDefinitions);
    }

    Sources(Select select, String searchDefinition) {
        this(select, Collections.singletonList(searchDefinition));
    }

    Sources(Select select, String searchDefinition, String... others) {
        this(select, Stream.concat(Stream.of(searchDefinition), Stream.of(others)).collect(Collectors.toList()));
    }

    @Override
    public String toString() {
        if (targetDocTypes.isEmpty() || targetDocTypes.size() == 1 && "*".equals(targetDocTypes.get(0))) {
            return "sources *";
        }

        if (targetDocTypes.size() == 1) {
            return targetDocTypes.get(0);
        }

        return "sources " + String.join(", ", targetDocTypes);
    }

    public Field where(String fieldName) {
        Field f = new Field(this, fieldName);
        f.setOp("and");
        return f;
    }

    public Query where(QueryChain userinput) {
        return whereReturnQuery(userinput);
    }

    public EndQuery where(Rank rank) {
        return whereReturnEndQuery(rank);
    }

    private Query whereReturnQuery(QueryChain qc) {
        return new Query(this, qc);
    }

    private EndQuery whereReturnEndQuery(Rank rank) {
        rank.setSources(this);
        return new EndQuery(rank);
    }

}