aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/test/java/com/yahoo/prelude/querytransform/test/LiteralBoostSearcherTestCase.java
blob: 5f9d0a53f0661c33239d72ea0085ce8165ba33e2 (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.prelude.querytransform.test;

import com.yahoo.prelude.Index;
import com.yahoo.prelude.IndexFacts;
import com.yahoo.prelude.IndexModel;
import com.yahoo.prelude.SearchDefinition;
import com.yahoo.search.Query;
import com.yahoo.prelude.querytransform.LiteralBoostSearcher;
import com.yahoo.search.searchchain.Execution;
import com.yahoo.search.test.QueryTestCase;
import org.junit.jupiter.api.Test;

import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertEquals;

/**
 * Tests the complete field match query transformer
 *
 * @author Steinar Knutsen
 */
public class LiteralBoostSearcherTestCase {

    @Test
    void testSimpleQueryWithBoost() {
        assertEquals("RANK (WEAKAND(100) abc) default_literal:abc",
                transformQuery("?query=abc&source=cluster1&restrict=type1"));
    }

    @Test
    void testSimpleQueryNoBoost() {
        assertEquals("WEAKAND(100) abc",
                transformQuery("?query=abc&source=cluster1&restrict=type2"));
    }

    @Test
    void testQueryWithExplicitIndex() {
        assertEquals("RANK (WEAKAND(100) absolute:abc) absolute_literal:abc",
                transformQuery("?query=absolute:abc&source=cluster1&restrict=type1"));
    }

    @Test
    void testQueryWithExplicitIndexNoBoost() {
        assertEquals("WEAKAND(100) absolute:abc",
                transformQuery("?query=absolute:abc&source=cluster1&restrict=type2"));
    }

    @Test
    void testQueryWithNegativeBranch() {
        assertEquals("RANK (+(AND abc def) -ghi) " +
                "default_literal:abc default_literal:def",
                transformQuery("?query=abc and def andnot ghi&type=adv&source=cluster1&restrict=type1"));
    }

    @Test
    void testJumbledQuery() {
        assertEquals
                ("RANK (OR (+(OR abc def) -ghi) jkl) " +
                        "default_literal:abc default_literal:def default_literal:jkl",
                        transformQuery("?query=abc or def andnot ghi or jkl&type=adv&source=cluster1&restrict=type1"));
    }

    @Test
    void testTermindexQuery() {
        assertEquals("RANK (+(WEAKAND(100) a b d) -c) default_literal:a " +
                "default_literal:b default_literal:d",
                transformQuery("?query=a b -c d&source=cluster1&restrict=type1"));
    }

    @Test
    void testQueryWithoutBoost() {
        assertEquals("RANK (AND nonexistant a nonexistant b) default_literal:nonexistant default_literal:a default_literal:nonexistant default_literal:b",
                transformQuery("?query=nonexistant:a nonexistant:b&source=cluster1&restrict=type1&type=all"));
    }

    private String transformQuery(String rawQuery) {
        Query query = new Query(QueryTestCase.httpEncode(rawQuery));
        new Execution(new LiteralBoostSearcher(), Execution.Context.createContextStub(createIndexFacts())).search(query);
        return query.getModel().getQueryTree().getRoot().toString();
    }

    private IndexFacts createIndexFacts() {
        Map<String, List<String>> clusters = new LinkedHashMap<>();
        clusters.put("cluster1", List.of("type1", "type2", "type3"));
        clusters.put("cluster2", List.of("type4", "type5"));
        Collection<SearchDefinition> searchDefs = List.of(
                createSearchDefinitionWithFields("type1", true),
                createSearchDefinitionWithFields("type2", false),
                new SearchDefinition("type3"),
                new SearchDefinition("type4"),
                new SearchDefinition("type5"));
        return new IndexFacts(new IndexModel(clusters, searchDefs));
    }

    private SearchDefinition createSearchDefinitionWithFields(String name, boolean literalBoost) {
        SearchDefinition type = new SearchDefinition(name);
        Index defaultIndex = new Index("default");
        defaultIndex.setLiteralBoost(literalBoost);
        type.addIndex(defaultIndex);
        Index absoluteIndex = new Index("absolute");
        absoluteIndex.setLiteralBoost(literalBoost);
        type.addIndex(absoluteIndex);
        return type;
    }

}